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