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 $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.' 30 ); 31 32 $options->registerOption( 33 'clear', 34 'clear the index before updating', 35 'c' 36 ); 37 $options->registerOption( 38 'quiet', 39 'don\'t produce any output', 40 'q' 41 ); 42 } 43 44 /** 45 * Your main program 46 * 47 * Arguments and options have been parsed when this is run 48 * 49 * @param Options $options 50 * @return void 51 */ 52 protected function main(Options $options) { 53 $this->clear = $options->getOpt('clear'); 54 $this->quiet = $options->getOpt('quiet'); 55 56 if($this->clear) $this->clearindex(); 57 58 $this->update(); 59 } 60 61 /** 62 * Update the index 63 */ 64 protected function update() { 65 global $conf; 66 $data = []; 67 $this->quietecho("Searching pages... "); 68 search($data, $conf['datadir'], 'search_allpages', ['skipacl' => true]); 69 $this->quietecho(count($data) . " pages found.\n"); 70 71 foreach($data as $val) { 72 $this->index($val['id']); 73 } 74 } 75 76 /** 77 * Index the given page 78 * 79 * @param string $id 80 */ 81 protected function index($id) { 82 $this->quietecho("$id... "); 83 idx_addPage($id, !$this->quiet, $this->clear); 84 $this->quietecho("done.\n"); 85 } 86 87 /** 88 * Clear all index files 89 */ 90 protected function clearindex() { 91 $this->quietecho("Clearing index... "); 92 idx_get_indexer()->clear(); 93 $this->quietecho("done.\n"); 94 } 95 96 /** 97 * Print message if not supressed 98 * 99 * @param string $msg 100 */ 101 protected function quietecho($msg) { 102 if(!$this->quiet) echo $msg; 103 } 104} 105 106// Main 107$cli = new IndexerCLI(); 108$cli->run(); 109