xref: /dokuwiki/bin/indexer.php (revision cbeaa4a0479ce7023201d4032978899ffaceb187)
124c3e0d2SAndreas Gohr#!/usr/bin/php
224c3e0d2SAndreas Gohr<?php
3*cbeaa4a0SAndreas Gohr
4*cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5*cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6*cbeaa4a0SAndreas Gohr
724c3e0d2SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
8b0b7909bSAndreas Gohrdefine('NOSESSION', 1);
924c3e0d2SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
1024c3e0d2SAndreas Gohr
111c36b3d8SAndreas Gohr/**
121c36b3d8SAndreas Gohr * Update the Search Index from command line
131c36b3d8SAndreas Gohr */
14*cbeaa4a0SAndreas Gohrclass IndexerCLI extends CLI {
15b0b7909bSAndreas Gohr
16b0b7909bSAndreas Gohr    private $quiet = false;
17b0b7909bSAndreas Gohr    private $clear = false;
18b0b7909bSAndreas Gohr
19b0b7909bSAndreas Gohr    /**
20b0b7909bSAndreas Gohr     * Register options and arguments on the given $options object
21b0b7909bSAndreas Gohr     *
22*cbeaa4a0SAndreas Gohr     * @param Options $options
23b0b7909bSAndreas Gohr     * @return void
24b0b7909bSAndreas Gohr     */
25*cbeaa4a0SAndreas Gohr    protected function setup(Options $options) {
26b0b7909bSAndreas Gohr        $options->setHelp(
27b0b7909bSAndreas Gohr            'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
28b0b7909bSAndreas Gohr            'given the index is cleared first.'
29b0b7909bSAndreas Gohr        );
30b0b7909bSAndreas Gohr
31b0b7909bSAndreas Gohr        $options->registerOption(
32b0b7909bSAndreas Gohr            'clear',
33b0b7909bSAndreas Gohr            'clear the index before updating',
34b0b7909bSAndreas Gohr            'c'
35b0b7909bSAndreas Gohr        );
36b0b7909bSAndreas Gohr        $options->registerOption(
37b0b7909bSAndreas Gohr            'quiet',
38b0b7909bSAndreas Gohr            'don\'t produce any output',
39b0b7909bSAndreas Gohr            'q'
40b0b7909bSAndreas Gohr        );
4124c3e0d2SAndreas Gohr    }
4224c3e0d2SAndreas Gohr
43b0b7909bSAndreas Gohr    /**
44b0b7909bSAndreas Gohr     * Your main program
45b0b7909bSAndreas Gohr     *
46b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
47b0b7909bSAndreas Gohr     *
48*cbeaa4a0SAndreas Gohr     * @param Options $options
49b0b7909bSAndreas Gohr     * @return void
50b0b7909bSAndreas Gohr     */
51*cbeaa4a0SAndreas Gohr    protected function main(Options $options) {
52b0b7909bSAndreas Gohr        $this->clear = $options->getOpt('clear');
53b0b7909bSAndreas Gohr        $this->quiet = $options->getOpt('quiet');
5424c3e0d2SAndreas Gohr
55b0b7909bSAndreas Gohr        if($this->clear) $this->clearindex();
5624c3e0d2SAndreas Gohr
57b0b7909bSAndreas Gohr        $this->update();
5824c3e0d2SAndreas Gohr    }
5924c3e0d2SAndreas Gohr
60b0b7909bSAndreas Gohr    /**
61b0b7909bSAndreas Gohr     * Update the index
62b0b7909bSAndreas Gohr     */
63b0b7909bSAndreas Gohr    function update() {
6424c3e0d2SAndreas Gohr        global $conf;
6524c3e0d2SAndreas Gohr        $data = array();
66b0b7909bSAndreas Gohr        $this->quietecho("Searching pages... ");
679df39f14SRoland Alder        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
68b0b7909bSAndreas Gohr        $this->quietecho(count($data) . " pages found.\n");
6924c3e0d2SAndreas Gohr
7024c3e0d2SAndreas Gohr        foreach($data as $val) {
71b0b7909bSAndreas Gohr            $this->index($val['id']);
7224c3e0d2SAndreas Gohr        }
7324c3e0d2SAndreas Gohr    }
7424c3e0d2SAndreas Gohr
75b0b7909bSAndreas Gohr    /**
76b0b7909bSAndreas Gohr     * Index the given page
77b0b7909bSAndreas Gohr     *
78b0b7909bSAndreas Gohr     * @param string $id
79b0b7909bSAndreas Gohr     */
80b0b7909bSAndreas Gohr    function index($id) {
81b0b7909bSAndreas Gohr        $this->quietecho("$id... ");
82b0b7909bSAndreas Gohr        idx_addPage($id, !$this->quiet, $this->clear);
83b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
8424c3e0d2SAndreas Gohr    }
8524c3e0d2SAndreas Gohr
8624c3e0d2SAndreas Gohr    /**
8724c3e0d2SAndreas Gohr     * Clear all index files
8824c3e0d2SAndreas Gohr     */
89b0b7909bSAndreas Gohr    function clearindex() {
90b0b7909bSAndreas Gohr        $this->quietecho("Clearing index... ");
913cf3c7d6SMichael Hamann        idx_get_indexer()->clear();
92b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
9324c3e0d2SAndreas Gohr    }
9424c3e0d2SAndreas Gohr
95b0b7909bSAndreas Gohr    /**
96b0b7909bSAndreas Gohr     * Print message if not supressed
97b0b7909bSAndreas Gohr     *
98b0b7909bSAndreas Gohr     * @param string $msg
99b0b7909bSAndreas Gohr     */
100b0b7909bSAndreas Gohr    function quietecho($msg) {
101b0b7909bSAndreas Gohr        if(!$this->quiet) echo $msg;
102b0b7909bSAndreas Gohr    }
103981f048aSGabriel Birke}
104981f048aSGabriel Birke
105b0b7909bSAndreas Gohr// Main
106b0b7909bSAndreas Gohr$cli = new IndexerCLI();
107b0b7909bSAndreas Gohr$cli->run();
108