1#!/usr/bin/env php 2<?php 3 4use splitbrain\phpcli\CLI; 5use splitbrain\phpcli\Options; 6 7if(!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/'); 8define('NOSESSION', 1); 9require_once(DOKU_INC . 'inc/init.php'); 10 11/** 12 * Update the Search Index from command line 13 */ 14class IndexerCLI extends CLI 15{ 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 = []; 70 $this->quietecho("Searching pages... "); 71 search($data, $conf['datadir'], 'search_allpages', ['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 idx_addPage($id, !$this->quiet, $this->clear); 88 $this->quietecho("done.\n"); 89 } 90 91 /** 92 * Clear all index files 93 */ 94 protected function clearindex() 95 { 96 $this->quietecho("Clearing index... "); 97 idx_get_indexer()->clear(); 98 $this->quietecho("done.\n"); 99 } 100 101 /** 102 * Print message if not supressed 103 * 104 * @param string $msg 105 */ 106 protected function quietecho($msg) 107 { 108 if(!$this->quiet) echo $msg; 109 } 110} 111 112// Main 113$cli = new IndexerCLI(); 114$cli->run(); 115