xref: /dokuwiki/bin/indexer.php (revision b0b7909bdd454e9614f4ffe34f384b0da0ce4585)
124c3e0d2SAndreas Gohr#!/usr/bin/php
224c3e0d2SAndreas Gohr<?php
324c3e0d2SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
4*b0b7909bSAndreas Gohrdefine('NOSESSION', 1);
524c3e0d2SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
624c3e0d2SAndreas Gohr
7*b0b7909bSAndreas Gohrclass IndexerCLI extends DokuCLI {
8*b0b7909bSAndreas Gohr
9*b0b7909bSAndreas Gohr    private $quiet = false;
10*b0b7909bSAndreas Gohr    private $clear = false;
11*b0b7909bSAndreas Gohr
12*b0b7909bSAndreas Gohr    /**
13*b0b7909bSAndreas Gohr     * Register options and arguments on the given $options object
14*b0b7909bSAndreas Gohr     *
15*b0b7909bSAndreas Gohr     * @param DokuCLI_Options $options
16*b0b7909bSAndreas Gohr     * @return void
17*b0b7909bSAndreas Gohr     */
18*b0b7909bSAndreas Gohr    protected function setup(DokuCLI_Options $options) {
19*b0b7909bSAndreas Gohr        $options->setHelp(
20*b0b7909bSAndreas Gohr                'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
21*b0b7909bSAndreas Gohr                'given the index is cleared first.'
22*b0b7909bSAndreas Gohr        );
23*b0b7909bSAndreas Gohr
24*b0b7909bSAndreas Gohr        $options->registerOption(
25*b0b7909bSAndreas Gohr                'clear',
26*b0b7909bSAndreas Gohr                'clear the index before updating',
27*b0b7909bSAndreas Gohr                'c'
28*b0b7909bSAndreas Gohr        );
29*b0b7909bSAndreas Gohr        $options->registerOption(
30*b0b7909bSAndreas Gohr                'quiet',
31*b0b7909bSAndreas Gohr                'don\'t produce any output',
32*b0b7909bSAndreas Gohr                'q'
33*b0b7909bSAndreas Gohr        );
3424c3e0d2SAndreas Gohr    }
3524c3e0d2SAndreas Gohr
36*b0b7909bSAndreas Gohr    /**
37*b0b7909bSAndreas Gohr     * Your main program
38*b0b7909bSAndreas Gohr     *
39*b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
40*b0b7909bSAndreas Gohr     *
41*b0b7909bSAndreas Gohr     * @param DokuCLI_Options $options
42*b0b7909bSAndreas Gohr     * @return void
43*b0b7909bSAndreas Gohr     */
44*b0b7909bSAndreas Gohr    protected function main(DokuCLI_Options $options) {
45*b0b7909bSAndreas Gohr        $this->clear = $options->getOpt('clear');
46*b0b7909bSAndreas Gohr        $this->quiet = $options->getOpt('quiet');
4724c3e0d2SAndreas Gohr
48*b0b7909bSAndreas Gohr        if($this->clear) $this->clearindex();
4924c3e0d2SAndreas Gohr
50*b0b7909bSAndreas Gohr        $this->update();
5124c3e0d2SAndreas Gohr    }
5224c3e0d2SAndreas Gohr
53*b0b7909bSAndreas Gohr    /**
54*b0b7909bSAndreas Gohr     * Update the index
55*b0b7909bSAndreas Gohr     */
56*b0b7909bSAndreas Gohr    function update() {
5724c3e0d2SAndreas Gohr        global $conf;
5824c3e0d2SAndreas Gohr        $data = array();
59*b0b7909bSAndreas Gohr        $this->quietecho("Searching pages... ");
609df39f14SRoland Alder        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
61*b0b7909bSAndreas Gohr        $this->quietecho(count($data) . " pages found.\n");
6224c3e0d2SAndreas Gohr
6324c3e0d2SAndreas Gohr        foreach($data as $val) {
64*b0b7909bSAndreas Gohr            $this->index($val['id']);
6524c3e0d2SAndreas Gohr        }
6624c3e0d2SAndreas Gohr    }
6724c3e0d2SAndreas Gohr
68*b0b7909bSAndreas Gohr    /**
69*b0b7909bSAndreas Gohr     * Index the given page
70*b0b7909bSAndreas Gohr     *
71*b0b7909bSAndreas Gohr     * @param string $id
72*b0b7909bSAndreas Gohr     */
73*b0b7909bSAndreas Gohr    function index($id) {
74*b0b7909bSAndreas Gohr        $this->quietecho("$id... ");
75*b0b7909bSAndreas Gohr        idx_addPage($id, !$this->quiet, $this->clear);
76*b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
7724c3e0d2SAndreas Gohr    }
7824c3e0d2SAndreas Gohr
7924c3e0d2SAndreas Gohr    /**
8024c3e0d2SAndreas Gohr     * Clear all index files
8124c3e0d2SAndreas Gohr     */
82*b0b7909bSAndreas Gohr    function clearindex() {
83*b0b7909bSAndreas Gohr        $this->quietecho("Clearing index... ");
843cf3c7d6SMichael Hamann        idx_get_indexer()->clear();
85*b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
8624c3e0d2SAndreas Gohr    }
8724c3e0d2SAndreas Gohr
88*b0b7909bSAndreas Gohr    /**
89*b0b7909bSAndreas Gohr     * Print message if not supressed
90*b0b7909bSAndreas Gohr     *
91*b0b7909bSAndreas Gohr     * @param string $msg
92*b0b7909bSAndreas Gohr     */
93*b0b7909bSAndreas Gohr    function quietecho($msg) {
94*b0b7909bSAndreas Gohr        if(!$this->quiet) echo $msg;
95*b0b7909bSAndreas Gohr    }
96981f048aSGabriel Birke}
97981f048aSGabriel Birke
98*b0b7909bSAndreas Gohr// Main
99*b0b7909bSAndreas Gohr$cli = new IndexerCLI();
100*b0b7909bSAndreas Gohr$cli->run();