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