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