1#!/usr/bin/env php 2<?php 3 4use dokuwiki\Logger; 5use splitbrain\phpcli\CLI; 6use splitbrain\phpcli\Options; 7use dokuwiki\Search\Indexer; 8 9if (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/'); 10define('NOSESSION', 1); 11require_once(DOKU_INC . 'inc/init.php'); 12 13/** 14 * Update the Search Index from command line 15 */ 16class IndexerCLI extends CLI 17{ 18 private $quiet = false; 19 private $clear = false; 20 21 /** 22 * Register options and arguments on the given $options object 23 * 24 * @param Options $options 25 * @return void 26 */ 27 protected function setup(Options $options) 28 { 29 $options->setHelp( 30 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' . 31 'given the index is cleared first.' 32 ); 33 34 $options->registerOption( 35 'clear', 36 'clear the index before updating', 37 'c' 38 ); 39 $options->registerOption( 40 'quiet', 41 'DEPRECATED', 42 'q' 43 ); 44 } 45 46 /** 47 * Your main program 48 * 49 * Arguments and options have been parsed when this is run 50 * 51 * @param Options $options 52 * @return void 53 */ 54 protected function main(Options $options) 55 { 56 $this->clear = $options->getOpt('clear'); 57 $this->quiet = $options->getOpt('quiet'); 58 59 if ($this->quiet) { 60 Logger::deprecated('Calling bin/indexer.php with -q/--quiet is deprecated. Use --loglevel instead.'); 61 $this->setLogLevel('emergency'); 62 } 63 64 if ($this->clear) $this->clearindex(); 65 66 $this->update(); 67 } 68 69 /** 70 * Update the index 71 */ 72 protected function update() 73 { 74 global $conf; 75 $data = []; 76 $this->notice('Searching pages...'); 77 search($data, $conf['datadir'], 'search_allpages', ['skipacl' => true]); 78 $this->info(count($data) . ' pages found.'); 79 80 foreach ($data as $val) { 81 $this->index($val['id']); 82 } 83 } 84 85 /** 86 * Index the given page 87 * 88 * @param string $id 89 */ 90 protected function index($id) 91 { 92 $this->notice("$id indexing..."); 93 try { 94 (new Indexer())->addPage($id, $this->clear); 95 $this->success("$id indexed."); 96 } catch (Throwable $e) { 97 $this->error("$id indexing error: " . $e->getMessage()); 98 $this->debug($e->getTraceAsString()); 99 return; 100 } 101 } 102 103 /** 104 * Clear all index files 105 */ 106 protected function clearindex() 107 { 108 $this->notice('Clearing index...'); 109 (new Indexer)->clear(); 110 $this->success('Index cleared.'); 111 } 112} 113 114// Main 115$cli = new IndexerCLI(); 116$cli->run(); 117