xref: /dokuwiki/bin/indexer.php (revision 1c36b3d86f90185519cadaad85251922dc771fe1)
124c3e0d2SAndreas Gohr#!/usr/bin/php
224c3e0d2SAndreas Gohr<?php
324c3e0d2SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/');
4b0b7909bSAndreas Gohrdefine('NOSESSION', 1);
524c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
624c3e0d2SAndreas Gohr
7*1c36b3d8SAndreas Gohr/**
8*1c36b3d8SAndreas Gohr * Update the Search Index from command line
9*1c36b3d8SAndreas Gohr */
10b0b7909bSAndreas Gohrclass IndexerCLI extends DokuCLI {
11b0b7909bSAndreas Gohr
12b0b7909bSAndreas Gohr    private $quiet = false;
13b0b7909bSAndreas Gohr    private $clear = false;
14b0b7909bSAndreas Gohr
15b0b7909bSAndreas Gohr    /**
16b0b7909bSAndreas Gohr     * Register options and arguments on the given $options object
17b0b7909bSAndreas Gohr     *
18b0b7909bSAndreas Gohr     * @param DokuCLI_Options $options
19b0b7909bSAndreas Gohr     * @return void
20b0b7909bSAndreas Gohr     */
21b0b7909bSAndreas Gohr    protected function setup(DokuCLI_Options $options) {
22b0b7909bSAndreas Gohr        $options->setHelp(
23b0b7909bSAndreas Gohr            'Updates the searchindex by indexing all new or changed pages. When the -c option is '.
24b0b7909bSAndreas Gohr            'given the index is cleared first.'
25b0b7909bSAndreas Gohr        );
26b0b7909bSAndreas Gohr
27b0b7909bSAndreas Gohr        $options->registerOption(
28b0b7909bSAndreas Gohr            'clear',
29b0b7909bSAndreas Gohr            'clear the index before updating',
30b0b7909bSAndreas Gohr            'c'
31b0b7909bSAndreas Gohr        );
32b0b7909bSAndreas Gohr        $options->registerOption(
33b0b7909bSAndreas Gohr            'quiet',
34b0b7909bSAndreas Gohr            'don\'t produce any output',
35b0b7909bSAndreas Gohr            'q'
36b0b7909bSAndreas Gohr        );
3724c3e0d2SAndreas Gohr    }
3824c3e0d2SAndreas Gohr
39b0b7909bSAndreas Gohr    /**
40b0b7909bSAndreas Gohr     * Your main program
41b0b7909bSAndreas Gohr     *
42b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
43b0b7909bSAndreas Gohr     *
44b0b7909bSAndreas Gohr     * @param DokuCLI_Options $options
45b0b7909bSAndreas Gohr     * @return void
46b0b7909bSAndreas Gohr     */
47b0b7909bSAndreas Gohr    protected function main(DokuCLI_Options $options) {
48b0b7909bSAndreas Gohr        $this->clear = $options->getOpt('clear');
49b0b7909bSAndreas Gohr        $this->quiet = $options->getOpt('quiet');
5024c3e0d2SAndreas Gohr
51b0b7909bSAndreas Gohr        if($this->clear) $this->clearindex();
5224c3e0d2SAndreas Gohr
53b0b7909bSAndreas Gohr        $this->update();
5424c3e0d2SAndreas Gohr    }
5524c3e0d2SAndreas Gohr
56b0b7909bSAndreas Gohr    /**
57b0b7909bSAndreas Gohr     * Update the index
58b0b7909bSAndreas Gohr     */
59b0b7909bSAndreas Gohr    function update() {
6024c3e0d2SAndreas Gohr        global $conf;
6124c3e0d2SAndreas Gohr        $data = array();
62b0b7909bSAndreas Gohr        $this->quietecho("Searching pages... ");
639df39f14SRoland Alder        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
64b0b7909bSAndreas Gohr        $this->quietecho(count($data)." pages found.\n");
6524c3e0d2SAndreas Gohr
6624c3e0d2SAndreas Gohr        foreach($data as $val) {
67b0b7909bSAndreas Gohr            $this->index($val['id']);
6824c3e0d2SAndreas Gohr        }
6924c3e0d2SAndreas Gohr    }
7024c3e0d2SAndreas Gohr
71b0b7909bSAndreas Gohr    /**
72b0b7909bSAndreas Gohr     * Index the given page
73b0b7909bSAndreas Gohr     *
74b0b7909bSAndreas Gohr     * @param string $id
75b0b7909bSAndreas Gohr     */
76b0b7909bSAndreas Gohr    function index($id) {
77b0b7909bSAndreas Gohr        $this->quietecho("$id... ");
78b0b7909bSAndreas Gohr        idx_addPage($id, !$this->quiet, $this->clear);
79b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
8024c3e0d2SAndreas Gohr    }
8124c3e0d2SAndreas Gohr
8224c3e0d2SAndreas Gohr    /**
8324c3e0d2SAndreas Gohr     * Clear all index files
8424c3e0d2SAndreas Gohr     */
85b0b7909bSAndreas Gohr    function clearindex() {
86b0b7909bSAndreas Gohr        $this->quietecho("Clearing index... ");
873cf3c7d6SMichael Hamann        idx_get_indexer()->clear();
88b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
8924c3e0d2SAndreas Gohr    }
9024c3e0d2SAndreas Gohr
91b0b7909bSAndreas Gohr    /**
92b0b7909bSAndreas Gohr     * Print message if not supressed
93b0b7909bSAndreas Gohr     *
94b0b7909bSAndreas Gohr     * @param string $msg
95b0b7909bSAndreas Gohr     */
96b0b7909bSAndreas Gohr    function quietecho($msg) {
97b0b7909bSAndreas Gohr        if(!$this->quiet) echo $msg;
98b0b7909bSAndreas Gohr    }
99981f048aSGabriel Birke}
100981f048aSGabriel Birke
101b0b7909bSAndreas Gohr// Main
102b0b7909bSAndreas Gohr$cli = new IndexerCLI();
103b0b7909bSAndreas Gohr$cli->run();