xref: /dokuwiki/bin/indexer.php (revision 4027a91aac0d8a078226f0e5beb2158d508a1897)
124c3e0d2SAndreas Gohr#!/usr/bin/php
224c3e0d2SAndreas Gohr<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6*4027a91aSSatoshi Saharause dokuwiki\Search\Indexer;
7cbeaa4a0SAndreas Gohr
824c3e0d2SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
9b0b7909bSAndreas Gohrdefine('NOSESSION', 1);
1024c3e0d2SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
1124c3e0d2SAndreas Gohr
121c36b3d8SAndreas Gohr/**
131c36b3d8SAndreas Gohr * Update the Search Index from command line
141c36b3d8SAndreas Gohr */
15*4027a91aSSatoshi Saharaclass IndexerCLI extends CLI
16*4027a91aSSatoshi Sahara{
17b0b7909bSAndreas Gohr    private $quiet = false;
18b0b7909bSAndreas Gohr    private $clear = false;
19b0b7909bSAndreas Gohr
20b0b7909bSAndreas Gohr    /**
21b0b7909bSAndreas Gohr     * Register options and arguments on the given $options object
22b0b7909bSAndreas Gohr     *
23cbeaa4a0SAndreas Gohr     * @param Options $options
24b0b7909bSAndreas Gohr     * @return void
25b0b7909bSAndreas Gohr     */
26*4027a91aSSatoshi Sahara    protected function setup(Options $options)
27*4027a91aSSatoshi Sahara    {
28b0b7909bSAndreas Gohr        $options->setHelp(
29b0b7909bSAndreas Gohr            'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
30b0b7909bSAndreas Gohr            'given the index is cleared first.'
31b0b7909bSAndreas Gohr        );
32b0b7909bSAndreas Gohr
33b0b7909bSAndreas Gohr        $options->registerOption(
34b0b7909bSAndreas Gohr            'clear',
35b0b7909bSAndreas Gohr            'clear the index before updating',
36b0b7909bSAndreas Gohr            'c'
37b0b7909bSAndreas Gohr        );
38b0b7909bSAndreas Gohr        $options->registerOption(
39b0b7909bSAndreas Gohr            'quiet',
40b0b7909bSAndreas Gohr            'don\'t produce any output',
41b0b7909bSAndreas Gohr            'q'
42b0b7909bSAndreas Gohr        );
4324c3e0d2SAndreas Gohr    }
4424c3e0d2SAndreas Gohr
45b0b7909bSAndreas Gohr    /**
46b0b7909bSAndreas Gohr     * Your main program
47b0b7909bSAndreas Gohr     *
48b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
49b0b7909bSAndreas Gohr     *
50cbeaa4a0SAndreas Gohr     * @param Options $options
51b0b7909bSAndreas Gohr     * @return void
52b0b7909bSAndreas Gohr     */
53*4027a91aSSatoshi Sahara    protected function main(Options $options)
54*4027a91aSSatoshi Sahara    {
55b0b7909bSAndreas Gohr        $this->clear = $options->getOpt('clear');
56b0b7909bSAndreas Gohr        $this->quiet = $options->getOpt('quiet');
5724c3e0d2SAndreas Gohr
58b0b7909bSAndreas Gohr        if($this->clear) $this->clearindex();
5924c3e0d2SAndreas Gohr
60b0b7909bSAndreas Gohr        $this->update();
6124c3e0d2SAndreas Gohr    }
6224c3e0d2SAndreas Gohr
63b0b7909bSAndreas Gohr    /**
64b0b7909bSAndreas Gohr     * Update the index
65b0b7909bSAndreas Gohr     */
66*4027a91aSSatoshi Sahara    protected function update()
67*4027a91aSSatoshi Sahara    {
6824c3e0d2SAndreas Gohr        global $conf;
6924c3e0d2SAndreas Gohr        $data = array();
70b0b7909bSAndreas Gohr        $this->quietecho("Searching pages... ");
719df39f14SRoland Alder        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
72b0b7909bSAndreas Gohr        $this->quietecho(count($data) . " pages found.\n");
7324c3e0d2SAndreas Gohr
7424c3e0d2SAndreas Gohr        foreach($data as $val) {
75b0b7909bSAndreas Gohr            $this->index($val['id']);
7624c3e0d2SAndreas Gohr        }
7724c3e0d2SAndreas Gohr    }
7824c3e0d2SAndreas Gohr
79b0b7909bSAndreas Gohr    /**
80b0b7909bSAndreas Gohr     * Index the given page
81b0b7909bSAndreas Gohr     *
82b0b7909bSAndreas Gohr     * @param string $id
83b0b7909bSAndreas Gohr     */
84*4027a91aSSatoshi Sahara    protected function index($id)
85*4027a91aSSatoshi Sahara    {
86b0b7909bSAndreas Gohr        $this->quietecho("$id... ");
87*4027a91aSSatoshi Sahara        $Indexer = Indexer::getInstance();
883f4a342bSSatoshi Sahara        $Indexer->addPage($id, !$this->quiet, $this->clear);
89b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
9024c3e0d2SAndreas Gohr    }
9124c3e0d2SAndreas Gohr
9224c3e0d2SAndreas Gohr    /**
9324c3e0d2SAndreas Gohr     * Clear all index files
9424c3e0d2SAndreas Gohr     */
95*4027a91aSSatoshi Sahara    protected function clearindex()
96*4027a91aSSatoshi Sahara    {
97b0b7909bSAndreas Gohr        $this->quietecho("Clearing index... ");
98*4027a91aSSatoshi Sahara        $Indexer = Indexer::getInstance();
993f4a342bSSatoshi Sahara        $Indexer->clear();
100b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
10124c3e0d2SAndreas Gohr    }
10224c3e0d2SAndreas Gohr
103b0b7909bSAndreas Gohr    /**
104b0b7909bSAndreas Gohr     * Print message if not supressed
105b0b7909bSAndreas Gohr     *
106b0b7909bSAndreas Gohr     * @param string $msg
107b0b7909bSAndreas Gohr     */
108*4027a91aSSatoshi Sahara    protected function quietecho($msg)
109*4027a91aSSatoshi Sahara    {
110b0b7909bSAndreas Gohr        if(!$this->quiet) echo $msg;
111b0b7909bSAndreas Gohr    }
112981f048aSGabriel Birke}
113981f048aSGabriel Birke
114b0b7909bSAndreas Gohr// Main
115b0b7909bSAndreas Gohr$cli = new IndexerCLI();
116b0b7909bSAndreas Gohr$cli->run();
117