1#!/usr/bin/env php 2<?php 3 4use splitbrain\phpcli\CLI; 5use splitbrain\phpcli\Options; 6use dokuwiki\Search\Indexer; 7 8if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/'); 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 'don\'t produce any output', 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->clear) $this->clearindex(); 59 60 $this->update(); 61 } 62 63 /** 64 * Update the index 65 */ 66 protected function update() 67 { 68 global $conf; 69 $data = array(); 70 $this->quietecho("Searching pages... "); 71 search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true)); 72 $this->quietecho(count($data) . " pages found.\n"); 73 74 foreach($data as $val) { 75 $this->index($val['id']); 76 } 77 } 78 79 /** 80 * Index the given page 81 * 82 * @param string $id 83 */ 84 protected function index($id) 85 { 86 $this->quietecho("$id... "); 87 $Indexer = Indexer::getInstance(); 88 $Indexer->addPage($id, !$this->quiet, $this->clear); 89 $this->quietecho("done.\n"); 90 } 91 92 /** 93 * Clear all index files 94 */ 95 protected function clearindex() 96 { 97 $this->quietecho("Clearing index... "); 98 $Indexer = Indexer::getInstance(); 99 $Indexer->clear(); 100 $this->quietecho("done.\n"); 101 } 102 103 /** 104 * Print message if not supressed 105 * 106 * @param string $msg 107 */ 108 protected function quietecho($msg) 109 { 110 if(!$this->quiet) echo $msg; 111 } 112} 113 114// Main 115$cli = new IndexerCLI(); 116$cli->run(); 117