xref: /dokuwiki/bin/indexer.php (revision cbfa4829d9bcd40d1cc3b9220fe78fa37c385c02)
1*cbfa4829SPhy#!/usr/bin/env php
224c3e0d2SAndreas Gohr<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6cbeaa4a0SAndreas Gohr
724c3e0d2SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
8b0b7909bSAndreas Gohrdefine('NOSESSION', 1);
924c3e0d2SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
1024c3e0d2SAndreas Gohr
111c36b3d8SAndreas Gohr/**
121c36b3d8SAndreas Gohr * Update the Search Index from command line
131c36b3d8SAndreas Gohr */
14cbeaa4a0SAndreas Gohrclass IndexerCLI extends CLI {
15b0b7909bSAndreas Gohr
16b0b7909bSAndreas Gohr    private $quiet = false;
17b0b7909bSAndreas Gohr    private $clear = false;
18b0b7909bSAndreas Gohr
19b0b7909bSAndreas Gohr    /**
20b0b7909bSAndreas Gohr     * Register options and arguments on the given $options object
21b0b7909bSAndreas Gohr     *
22cbeaa4a0SAndreas Gohr     * @param Options $options
23b0b7909bSAndreas Gohr     * @return void
24b0b7909bSAndreas Gohr     */
25cbeaa4a0SAndreas Gohr    protected function setup(Options $options) {
26b0b7909bSAndreas Gohr        $options->setHelp(
27b0b7909bSAndreas Gohr            'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
28b0b7909bSAndreas Gohr            'given the index is cleared first.'
29b0b7909bSAndreas Gohr        );
30b0b7909bSAndreas Gohr
31b0b7909bSAndreas Gohr        $options->registerOption(
32b0b7909bSAndreas Gohr            'clear',
33b0b7909bSAndreas Gohr            'clear the index before updating',
34b0b7909bSAndreas Gohr            'c'
35b0b7909bSAndreas Gohr        );
36b0b7909bSAndreas Gohr        $options->registerOption(
37b0b7909bSAndreas Gohr            'quiet',
38b0b7909bSAndreas Gohr            'don\'t produce any output',
39b0b7909bSAndreas Gohr            'q'
40b0b7909bSAndreas Gohr        );
4124c3e0d2SAndreas Gohr    }
4224c3e0d2SAndreas Gohr
43b0b7909bSAndreas Gohr    /**
44b0b7909bSAndreas Gohr     * Your main program
45b0b7909bSAndreas Gohr     *
46b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
47b0b7909bSAndreas Gohr     *
48cbeaa4a0SAndreas Gohr     * @param Options $options
49b0b7909bSAndreas Gohr     * @return void
50b0b7909bSAndreas Gohr     */
51cbeaa4a0SAndreas Gohr    protected function main(Options $options) {
52b0b7909bSAndreas Gohr        $this->clear = $options->getOpt('clear');
53b0b7909bSAndreas Gohr        $this->quiet = $options->getOpt('quiet');
5424c3e0d2SAndreas Gohr
55b0b7909bSAndreas Gohr        if($this->clear) $this->clearindex();
5624c3e0d2SAndreas Gohr
57b0b7909bSAndreas Gohr        $this->update();
5824c3e0d2SAndreas Gohr    }
5924c3e0d2SAndreas Gohr
60b0b7909bSAndreas Gohr    /**
61b0b7909bSAndreas Gohr     * Update the index
62b0b7909bSAndreas Gohr     */
632b2d0ba9SAndreas Gohr    protected function update() {
6424c3e0d2SAndreas Gohr        global $conf;
6524c3e0d2SAndreas Gohr        $data = array();
66b0b7909bSAndreas Gohr        $this->quietecho("Searching pages... ");
679df39f14SRoland Alder        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
68b0b7909bSAndreas Gohr        $this->quietecho(count($data) . " pages found.\n");
6924c3e0d2SAndreas Gohr
7024c3e0d2SAndreas Gohr        foreach($data as $val) {
71b0b7909bSAndreas Gohr            $this->index($val['id']);
7224c3e0d2SAndreas Gohr        }
7324c3e0d2SAndreas Gohr    }
7424c3e0d2SAndreas Gohr
75b0b7909bSAndreas Gohr    /**
76b0b7909bSAndreas Gohr     * Index the given page
77b0b7909bSAndreas Gohr     *
78b0b7909bSAndreas Gohr     * @param string $id
79b0b7909bSAndreas Gohr     */
802b2d0ba9SAndreas Gohr    protected function index($id) {
81b0b7909bSAndreas Gohr        $this->quietecho("$id... ");
82b0b7909bSAndreas Gohr        idx_addPage($id, !$this->quiet, $this->clear);
83b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
8424c3e0d2SAndreas Gohr    }
8524c3e0d2SAndreas Gohr
8624c3e0d2SAndreas Gohr    /**
8724c3e0d2SAndreas Gohr     * Clear all index files
8824c3e0d2SAndreas Gohr     */
892b2d0ba9SAndreas Gohr    protected function clearindex() {
90b0b7909bSAndreas Gohr        $this->quietecho("Clearing index... ");
913cf3c7d6SMichael Hamann        idx_get_indexer()->clear();
92b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
9324c3e0d2SAndreas Gohr    }
9424c3e0d2SAndreas Gohr
95b0b7909bSAndreas Gohr    /**
96b0b7909bSAndreas Gohr     * Print message if not supressed
97b0b7909bSAndreas Gohr     *
98b0b7909bSAndreas Gohr     * @param string $msg
99b0b7909bSAndreas Gohr     */
1002b2d0ba9SAndreas Gohr    protected function quietecho($msg) {
101b0b7909bSAndreas Gohr        if(!$this->quiet) echo $msg;
102b0b7909bSAndreas Gohr    }
103981f048aSGabriel Birke}
104981f048aSGabriel Birke
105b0b7909bSAndreas Gohr// Main
106b0b7909bSAndreas Gohr$cli = new IndexerCLI();
107b0b7909bSAndreas Gohr$cli->run();
108