xref: /dokuwiki/bin/indexer.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
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    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     *
22cbeaa4a0SAndreas Gohr     * @param Options $options
23b0b7909bSAndreas Gohr     * @return void
24b0b7909bSAndreas Gohr     */
25*d868eb89SAndreas Gohr    protected function setup(Options $options)
26*d868eb89SAndreas Gohr    {
27b0b7909bSAndreas Gohr        $options->setHelp(
28b0b7909bSAndreas Gohr            'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
29b0b7909bSAndreas Gohr            'given the index is cleared first.'
30b0b7909bSAndreas Gohr        );
31b0b7909bSAndreas Gohr
32b0b7909bSAndreas Gohr        $options->registerOption(
33b0b7909bSAndreas Gohr            'clear',
34b0b7909bSAndreas Gohr            'clear the index before updating',
35b0b7909bSAndreas Gohr            'c'
36b0b7909bSAndreas Gohr        );
37b0b7909bSAndreas Gohr        $options->registerOption(
38b0b7909bSAndreas Gohr            'quiet',
39b0b7909bSAndreas Gohr            'don\'t produce any output',
40b0b7909bSAndreas Gohr            'q'
41b0b7909bSAndreas Gohr        );
4224c3e0d2SAndreas Gohr    }
4324c3e0d2SAndreas Gohr
44b0b7909bSAndreas Gohr    /**
45b0b7909bSAndreas Gohr     * Your main program
46b0b7909bSAndreas Gohr     *
47b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
48b0b7909bSAndreas Gohr     *
49cbeaa4a0SAndreas Gohr     * @param Options $options
50b0b7909bSAndreas Gohr     * @return void
51b0b7909bSAndreas Gohr     */
52*d868eb89SAndreas Gohr    protected function main(Options $options)
53*d868eb89SAndreas Gohr    {
54b0b7909bSAndreas Gohr        $this->clear = $options->getOpt('clear');
55b0b7909bSAndreas Gohr        $this->quiet = $options->getOpt('quiet');
5624c3e0d2SAndreas Gohr
57b0b7909bSAndreas Gohr        if ($this->clear) $this->clearindex();
5824c3e0d2SAndreas Gohr
59b0b7909bSAndreas Gohr        $this->update();
6024c3e0d2SAndreas Gohr    }
6124c3e0d2SAndreas Gohr
62b0b7909bSAndreas Gohr    /**
63b0b7909bSAndreas Gohr     * Update the index
64b0b7909bSAndreas Gohr     */
65*d868eb89SAndreas Gohr    protected function update()
66*d868eb89SAndreas Gohr    {
6724c3e0d2SAndreas Gohr        global $conf;
68b1f206e1SAndreas Gohr        $data = [];
69b0b7909bSAndreas Gohr        $this->quietecho("Searching pages... ");
70b1f206e1SAndreas Gohr        search($data, $conf['datadir'], 'search_allpages', ['skipacl' => true]);
71b0b7909bSAndreas Gohr        $this->quietecho(count($data) . " pages found.\n");
7224c3e0d2SAndreas Gohr
7324c3e0d2SAndreas Gohr        foreach ($data as $val) {
74b0b7909bSAndreas Gohr            $this->index($val['id']);
7524c3e0d2SAndreas Gohr        }
7624c3e0d2SAndreas Gohr    }
7724c3e0d2SAndreas Gohr
78b0b7909bSAndreas Gohr    /**
79b0b7909bSAndreas Gohr     * Index the given page
80b0b7909bSAndreas Gohr     *
81b0b7909bSAndreas Gohr     * @param string $id
82b0b7909bSAndreas Gohr     */
83*d868eb89SAndreas Gohr    protected function index($id)
84*d868eb89SAndreas Gohr    {
85b0b7909bSAndreas Gohr        $this->quietecho("$id... ");
86b0b7909bSAndreas Gohr        idx_addPage($id, !$this->quiet, $this->clear);
87b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
8824c3e0d2SAndreas Gohr    }
8924c3e0d2SAndreas Gohr
9024c3e0d2SAndreas Gohr    /**
9124c3e0d2SAndreas Gohr     * Clear all index files
9224c3e0d2SAndreas Gohr     */
93*d868eb89SAndreas Gohr    protected function clearindex()
94*d868eb89SAndreas Gohr    {
95b0b7909bSAndreas Gohr        $this->quietecho("Clearing index... ");
963cf3c7d6SMichael Hamann        idx_get_indexer()->clear();
97b0b7909bSAndreas Gohr        $this->quietecho("done.\n");
9824c3e0d2SAndreas Gohr    }
9924c3e0d2SAndreas Gohr
100b0b7909bSAndreas Gohr    /**
101b0b7909bSAndreas Gohr     * Print message if not supressed
102b0b7909bSAndreas Gohr     *
103b0b7909bSAndreas Gohr     * @param string $msg
104b0b7909bSAndreas Gohr     */
105*d868eb89SAndreas Gohr    protected function quietecho($msg)
106*d868eb89SAndreas Gohr    {
107b0b7909bSAndreas Gohr        if (!$this->quiet) echo $msg;
108b0b7909bSAndreas Gohr    }
109981f048aSGabriel Birke}
110981f048aSGabriel Birke
111b0b7909bSAndreas Gohr// Main
112b0b7909bSAndreas Gohr$cli = new IndexerCLI();
113b0b7909bSAndreas Gohr$cli->run();
114