Merge pull request #4165 from dokuwiki/bot/autofix
[dokuwiki.git] / bin / indexer.php
blobcda740d0b23361316079bbc08af7bb071ef82cd1
1 #!/usr/bin/env php
2 <?php
4 use splitbrain\phpcli\CLI;
5 use splitbrain\phpcli\Options;
7 if (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
8 define('NOSESSION', 1);
9 require_once(DOKU_INC . 'inc/init.php');
11 /**
12 * Update the Search Index from command line
14 class IndexerCLI extends CLI
16 private $quiet = false;
17 private $clear = false;
19 /**
20 * Register options and arguments on the given $options object
22 * @param Options $options
23 * @return void
25 protected function setup(Options $options)
27 $options->setHelp(
28 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
29 'given the index is cleared first.'
32 $options->registerOption(
33 'clear',
34 'clear the index before updating',
35 'c'
37 $options->registerOption(
38 'quiet',
39 'don\'t produce any output',
40 'q'
44 /**
45 * Your main program
47 * Arguments and options have been parsed when this is run
49 * @param Options $options
50 * @return void
52 protected function main(Options $options)
54 $this->clear = $options->getOpt('clear');
55 $this->quiet = $options->getOpt('quiet');
57 if ($this->clear) $this->clearindex();
59 $this->update();
62 /**
63 * Update the index
65 protected function update()
67 global $conf;
68 $data = [];
69 $this->quietecho("Searching pages... ");
70 search($data, $conf['datadir'], 'search_allpages', ['skipacl' => true]);
71 $this->quietecho(count($data) . " pages found.\n");
73 foreach ($data as $val) {
74 $this->index($val['id']);
78 /**
79 * Index the given page
81 * @param string $id
83 protected function index($id)
85 $this->quietecho("$id... ");
86 idx_addPage($id, !$this->quiet, $this->clear);
87 $this->quietecho("done.\n");
90 /**
91 * Clear all index files
93 protected function clearindex()
95 $this->quietecho("Clearing index... ");
96 idx_get_indexer()->clear();
97 $this->quietecho("done.\n");
101 * Print message if not supressed
103 * @param string $msg
105 protected function quietecho($msg)
107 if (!$this->quiet) echo $msg;
111 // Main
112 $cli = new IndexerCLI();
113 $cli->run();