xref: /dokuwiki/bin/indexer.php (revision 3f4a342befbede62946481d36e02a2a7b211c309)
1#!/usr/bin/php
2<?php
3
4use splitbrain\phpcli\CLI;
5use splitbrain\phpcli\Options;
6use dokuwiki\Search\PageIndex;
7
8if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
9define('NOSESSION', 1);
10require_once(DOKU_INC . 'inc/init.php');
11
12/**
13 * Update the Search Index from command line
14 */
15class IndexerCLI extends CLI {
16
17    private $quiet = false;
18    private $clear = false;
19
20    /**
21     * Register options and arguments on the given $options object
22     *
23     * @param Options $options
24     * @return void
25     */
26    protected function setup(Options $options) {
27        $options->setHelp(
28            'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
29            'given the index is cleared first.'
30        );
31
32        $options->registerOption(
33            'clear',
34            'clear the index before updating',
35            'c'
36        );
37        $options->registerOption(
38            'quiet',
39            'don\'t produce any output',
40            'q'
41        );
42    }
43
44    /**
45     * Your main program
46     *
47     * Arguments and options have been parsed when this is run
48     *
49     * @param Options $options
50     * @return void
51     */
52    protected function main(Options $options) {
53        $this->clear = $options->getOpt('clear');
54        $this->quiet = $options->getOpt('quiet');
55
56        if($this->clear) $this->clearindex();
57
58        $this->update();
59    }
60
61    /**
62     * Update the index
63     */
64    protected function update() {
65        global $conf;
66        $data = array();
67        $this->quietecho("Searching pages... ");
68        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
69        $this->quietecho(count($data) . " pages found.\n");
70
71        foreach($data as $val) {
72            $this->index($val['id']);
73        }
74    }
75
76    /**
77     * Index the given page
78     *
79     * @param string $id
80     */
81    protected function index($id) {
82        $this->quietecho("$id... ");
83        $Indexer = PageIndex::getInstance();
84        $Indexer->addPage($id, !$this->quiet, $this->clear);
85        $this->quietecho("done.\n");
86    }
87
88    /**
89     * Clear all index files
90     */
91    protected function clearindex() {
92        $this->quietecho("Clearing index... ");
93        $Indexer = PageIndex::getInstance();
94        $Indexer->clear();
95        $this->quietecho("done.\n");
96    }
97
98    /**
99     * Print message if not supressed
100     *
101     * @param string $msg
102     */
103    protected function quietecho($msg) {
104        if(!$this->quiet) echo $msg;
105    }
106}
107
108// Main
109$cli = new IndexerCLI();
110$cli->run();
111