xref: /dokuwiki/bin/indexer.php (revision d868eb89f182718a31113373a6272670bd7f8012)
1cbfa4829SPhy#!/usr/bin/env php
224c3e0d2SAndreas Gohr<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6cbeaa4a0SAndreas Gohr
7b1f206e1SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
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 */
148c7c53b0SAndreas Gohrclass IndexerCLI extends CLI
158c7c53b0SAndreas Gohr{
16b0b7909bSAndreas Gohr
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*d868eb89SAndreas Gohr    protected function setup(Options $options)
27*d868eb89SAndreas Gohr    {
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*d868eb89SAndreas Gohr    protected function main(Options $options)
54*d868eb89SAndreas Gohr    {
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*d868eb89SAndreas Gohr    protected function update()
67*d868eb89SAndreas Gohr    {
6824c3e0d2SAndreas Gohr        global $conf;
69b1f206e1SAndreas Gohr        $data = [];
70b0b7909bSAndreas Gohr        $this->quietecho("Searching pages... ");
71b1f206e1SAndreas Gohr        search($data, $conf['datadir'], 'search_allpages', ['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*d868eb89SAndreas Gohr    protected function index($id)
85*d868eb89SAndreas Gohr    {
86b0b7909bSAndreas Gohr        $this->quietecho("$id... ");
87b0b7909bSAndreas Gohr        idx_addPage($id, !$this->quiet, $this->clear);
88b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
8924c3e0d2SAndreas Gohr    }
9024c3e0d2SAndreas Gohr
9124c3e0d2SAndreas Gohr    /**
9224c3e0d2SAndreas Gohr     * Clear all index files
9324c3e0d2SAndreas Gohr     */
94*d868eb89SAndreas Gohr    protected function clearindex()
95*d868eb89SAndreas Gohr    {
96b0b7909bSAndreas Gohr        $this->quietecho("Clearing index... ");
973cf3c7d6SMichael Hamann        idx_get_indexer()->clear();
98b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
9924c3e0d2SAndreas Gohr    }
10024c3e0d2SAndreas Gohr
101b0b7909bSAndreas Gohr    /**
102b0b7909bSAndreas Gohr     * Print message if not supressed
103b0b7909bSAndreas Gohr     *
104b0b7909bSAndreas Gohr     * @param string $msg
105b0b7909bSAndreas Gohr     */
106*d868eb89SAndreas Gohr    protected function quietecho($msg)
107*d868eb89SAndreas Gohr    {
108b0b7909bSAndreas Gohr        if(!$this->quiet) echo $msg;
109b0b7909bSAndreas Gohr    }
110981f048aSGabriel Birke}
111981f048aSGabriel Birke
112b0b7909bSAndreas Gohr// Main
113b0b7909bSAndreas Gohr$cli = new IndexerCLI();
114b0b7909bSAndreas Gohr$cli->run();
115