124c3e0d2SAndreas Gohr#!/usr/bin/php 224c3e0d2SAndreas Gohr<?php 3cbeaa4a0SAndreas Gohr 4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI; 5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options; 6*3f4a342bSSatoshi Saharause dokuwiki\Search\PageIndex; 7cbeaa4a0SAndreas Gohr 824c3e0d2SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/'); 9b0b7909bSAndreas Gohrdefine('NOSESSION', 1); 1024c3e0d2SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php'); 1124c3e0d2SAndreas Gohr 121c36b3d8SAndreas Gohr/** 131c36b3d8SAndreas Gohr * Update the Search Index from command line 141c36b3d8SAndreas Gohr */ 15cbeaa4a0SAndreas Gohrclass IndexerCLI extends CLI { 16b0b7909bSAndreas Gohr 17b0b7909bSAndreas Gohr private $quiet = false; 18b0b7909bSAndreas Gohr private $clear = false; 19b0b7909bSAndreas Gohr 20b0b7909bSAndreas Gohr /** 21b0b7909bSAndreas Gohr * Register options and arguments on the given $options object 22b0b7909bSAndreas Gohr * 23cbeaa4a0SAndreas Gohr * @param Options $options 24b0b7909bSAndreas Gohr * @return void 25b0b7909bSAndreas Gohr */ 26cbeaa4a0SAndreas Gohr protected function setup(Options $options) { 27b0b7909bSAndreas Gohr $options->setHelp( 28b0b7909bSAndreas Gohr 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' . 29b0b7909bSAndreas Gohr 'given the index is cleared first.' 30b0b7909bSAndreas Gohr ); 31b0b7909bSAndreas Gohr 32b0b7909bSAndreas Gohr $options->registerOption( 33b0b7909bSAndreas Gohr 'clear', 34b0b7909bSAndreas Gohr 'clear the index before updating', 35b0b7909bSAndreas Gohr 'c' 36b0b7909bSAndreas Gohr ); 37b0b7909bSAndreas Gohr $options->registerOption( 38b0b7909bSAndreas Gohr 'quiet', 39b0b7909bSAndreas Gohr 'don\'t produce any output', 40b0b7909bSAndreas Gohr 'q' 41b0b7909bSAndreas Gohr ); 4224c3e0d2SAndreas Gohr } 4324c3e0d2SAndreas Gohr 44b0b7909bSAndreas Gohr /** 45b0b7909bSAndreas Gohr * Your main program 46b0b7909bSAndreas Gohr * 47b0b7909bSAndreas Gohr * Arguments and options have been parsed when this is run 48b0b7909bSAndreas Gohr * 49cbeaa4a0SAndreas Gohr * @param Options $options 50b0b7909bSAndreas Gohr * @return void 51b0b7909bSAndreas Gohr */ 52cbeaa4a0SAndreas Gohr protected function main(Options $options) { 53b0b7909bSAndreas Gohr $this->clear = $options->getOpt('clear'); 54b0b7909bSAndreas Gohr $this->quiet = $options->getOpt('quiet'); 5524c3e0d2SAndreas Gohr 56b0b7909bSAndreas Gohr if($this->clear) $this->clearindex(); 5724c3e0d2SAndreas Gohr 58b0b7909bSAndreas Gohr $this->update(); 5924c3e0d2SAndreas Gohr } 6024c3e0d2SAndreas Gohr 61b0b7909bSAndreas Gohr /** 62b0b7909bSAndreas Gohr * Update the index 63b0b7909bSAndreas Gohr */ 642b2d0ba9SAndreas Gohr protected function update() { 6524c3e0d2SAndreas Gohr global $conf; 6624c3e0d2SAndreas Gohr $data = array(); 67b0b7909bSAndreas Gohr $this->quietecho("Searching pages... "); 689df39f14SRoland Alder search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true)); 69b0b7909bSAndreas Gohr $this->quietecho(count($data) . " pages found.\n"); 7024c3e0d2SAndreas Gohr 7124c3e0d2SAndreas Gohr foreach($data as $val) { 72b0b7909bSAndreas Gohr $this->index($val['id']); 7324c3e0d2SAndreas Gohr } 7424c3e0d2SAndreas Gohr } 7524c3e0d2SAndreas Gohr 76b0b7909bSAndreas Gohr /** 77b0b7909bSAndreas Gohr * Index the given page 78b0b7909bSAndreas Gohr * 79b0b7909bSAndreas Gohr * @param string $id 80b0b7909bSAndreas Gohr */ 812b2d0ba9SAndreas Gohr protected function index($id) { 82b0b7909bSAndreas Gohr $this->quietecho("$id... "); 83*3f4a342bSSatoshi Sahara $Indexer = PageIndex::getInstance(); 84*3f4a342bSSatoshi Sahara $Indexer->addPage($id, !$this->quiet, $this->clear); 85b0b7909bSAndreas Gohr $this->quietecho("done.\n"); 8624c3e0d2SAndreas Gohr } 8724c3e0d2SAndreas Gohr 8824c3e0d2SAndreas Gohr /** 8924c3e0d2SAndreas Gohr * Clear all index files 9024c3e0d2SAndreas Gohr */ 912b2d0ba9SAndreas Gohr protected function clearindex() { 92b0b7909bSAndreas Gohr $this->quietecho("Clearing index... "); 93*3f4a342bSSatoshi Sahara $Indexer = PageIndex::getInstance(); 94*3f4a342bSSatoshi Sahara $Indexer->clear(); 95b0b7909bSAndreas Gohr $this->quietecho("done.\n"); 9624c3e0d2SAndreas Gohr } 9724c3e0d2SAndreas Gohr 98b0b7909bSAndreas Gohr /** 99b0b7909bSAndreas Gohr * Print message if not supressed 100b0b7909bSAndreas Gohr * 101b0b7909bSAndreas Gohr * @param string $msg 102b0b7909bSAndreas Gohr */ 1032b2d0ba9SAndreas Gohr protected function quietecho($msg) { 104b0b7909bSAndreas Gohr if(!$this->quiet) echo $msg; 105b0b7909bSAndreas Gohr } 106981f048aSGabriel Birke} 107981f048aSGabriel Birke 108b0b7909bSAndreas Gohr// Main 109b0b7909bSAndreas Gohr$cli = new IndexerCLI(); 110b0b7909bSAndreas Gohr$cli->run(); 111