xref: /dokuwiki/bin/indexer.php (revision 141d9fe1576b731761d04a0ddb937eba0acc05b9)
1cbfa4829SPhy#!/usr/bin/env php
224c3e0d2SAndreas Gohr<?php
3cbeaa4a0SAndreas Gohr
4*141d9fe1SAndreas Gohruse dokuwiki\Logger;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
6cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
7cbeaa4a0SAndreas Gohr
8b1f206e1SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
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 */
158c7c53b0SAndreas Gohrclass IndexerCLI extends CLI
168c7c53b0SAndreas 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     */
26d868eb89SAndreas Gohr    protected function setup(Options $options)
27d868eb89SAndreas 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',
40*141d9fe1SAndreas Gohr            'DEPRECATED',
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     */
53d868eb89SAndreas Gohr    protected function main(Options $options)
54d868eb89SAndreas Gohr    {
55b0b7909bSAndreas Gohr        $this->clear = $options->getOpt('clear');
56b0b7909bSAndreas Gohr        $this->quiet = $options->getOpt('quiet');
5724c3e0d2SAndreas Gohr
58*141d9fe1SAndreas Gohr        if ($this->quiet) {
59*141d9fe1SAndreas Gohr            Logger::deprecated('Calling bin/indexer.php with -q/--quiet is deprecated. Use --loglevel instead.');
60*141d9fe1SAndreas Gohr            $this->setLogLevel('emergency');
61*141d9fe1SAndreas Gohr        }
62*141d9fe1SAndreas Gohr
63b0b7909bSAndreas Gohr        if ($this->clear) $this->clearindex();
6424c3e0d2SAndreas Gohr
65b0b7909bSAndreas Gohr        $this->update();
6624c3e0d2SAndreas Gohr    }
6724c3e0d2SAndreas Gohr
68b0b7909bSAndreas Gohr    /**
69b0b7909bSAndreas Gohr     * Update the index
70b0b7909bSAndreas Gohr     */
71d868eb89SAndreas Gohr    protected function update()
72d868eb89SAndreas Gohr    {
7324c3e0d2SAndreas Gohr        global $conf;
74b1f206e1SAndreas Gohr        $data = [];
75*141d9fe1SAndreas Gohr        $this->notice('Searching pages...');
76b1f206e1SAndreas Gohr        search($data, $conf['datadir'], 'search_allpages', ['skipacl' => true]);
77*141d9fe1SAndreas Gohr        $this->info(count($data) . ' pages found.');
7824c3e0d2SAndreas Gohr
7924c3e0d2SAndreas Gohr        foreach ($data as $val) {
80b0b7909bSAndreas Gohr            $this->index($val['id']);
8124c3e0d2SAndreas Gohr        }
8224c3e0d2SAndreas Gohr    }
8324c3e0d2SAndreas Gohr
84b0b7909bSAndreas Gohr    /**
85b0b7909bSAndreas Gohr     * Index the given page
86b0b7909bSAndreas Gohr     *
87b0b7909bSAndreas Gohr     * @param string $id
88b0b7909bSAndreas Gohr     */
89d868eb89SAndreas Gohr    protected function index($id)
90d868eb89SAndreas Gohr    {
91*141d9fe1SAndreas Gohr        $this->notice("$id indexing...");
92*141d9fe1SAndreas Gohr        try {
93*141d9fe1SAndreas Gohr            if (idx_addPage($id, isset($this->loglevel['info']), $this->clear)) {
94*141d9fe1SAndreas Gohr                $this->success("$id indexed.");
95*141d9fe1SAndreas Gohr            } else {
96*141d9fe1SAndreas Gohr                $this->info("$id index not updated.");
97*141d9fe1SAndreas Gohr            }
98*141d9fe1SAndreas Gohr        } catch (Throwable $e) {
99*141d9fe1SAndreas Gohr            $this->error("$id indexing error: " . $e->getMessage());
100*141d9fe1SAndreas Gohr            $this->debug($e->getTraceAsString());
101*141d9fe1SAndreas Gohr            return;
102*141d9fe1SAndreas Gohr        }
10324c3e0d2SAndreas Gohr    }
10424c3e0d2SAndreas Gohr
10524c3e0d2SAndreas Gohr    /**
10624c3e0d2SAndreas Gohr     * Clear all index files
10724c3e0d2SAndreas Gohr     */
108d868eb89SAndreas Gohr    protected function clearindex()
109d868eb89SAndreas Gohr    {
110*141d9fe1SAndreas Gohr        $this->notice('Clearing index...');
1113cf3c7d6SMichael Hamann        idx_get_indexer()->clear();
112*141d9fe1SAndreas Gohr        $this->success('Index cleared.');
113b0b7909bSAndreas Gohr    }
114981f048aSGabriel Birke}
115981f048aSGabriel Birke
116b0b7909bSAndreas Gohr// Main
117b0b7909bSAndreas Gohr$cli = new IndexerCLI();
118b0b7909bSAndreas Gohr$cli->run();
119