1cbfa4829SPhy#!/usr/bin/env php 224c3e0d2SAndreas Gohr<?php 3cbeaa4a0SAndreas Gohr 4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI; 5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options; 6cbeaa4a0SAndreas Gohr 7b1f206e1SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/'); 8b0b7909bSAndreas Gohrdefine('NOSESSION', 1); 924c3e0d2SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php'); 1024c3e0d2SAndreas Gohr 111c36b3d8SAndreas Gohr/** 121c36b3d8SAndreas Gohr * Update the Search Index from command line 131c36b3d8SAndreas Gohr */ 14*8c7c53b0SAndreas Gohrclass IndexerCLI extends CLI 15*8c7c53b0SAndreas Gohr{ 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; 66b1f206e1SAndreas Gohr $data = []; 67b0b7909bSAndreas Gohr $this->quietecho("Searching pages... "); 68b1f206e1SAndreas Gohr search($data, $conf['datadir'], 'search_allpages', ['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... "); 83b0b7909bSAndreas Gohr idx_addPage($id, !$this->quiet, $this->clear); 84b0b7909bSAndreas Gohr $this->quietecho("done.\n"); 8524c3e0d2SAndreas Gohr } 8624c3e0d2SAndreas Gohr 8724c3e0d2SAndreas Gohr /** 8824c3e0d2SAndreas Gohr * Clear all index files 8924c3e0d2SAndreas Gohr */ 902b2d0ba9SAndreas Gohr protected function clearindex() { 91b0b7909bSAndreas Gohr $this->quietecho("Clearing index... "); 923cf3c7d6SMichael Hamann idx_get_indexer()->clear(); 93b0b7909bSAndreas Gohr $this->quietecho("done.\n"); 9424c3e0d2SAndreas Gohr } 9524c3e0d2SAndreas Gohr 96b0b7909bSAndreas Gohr /** 97b0b7909bSAndreas Gohr * Print message if not supressed 98b0b7909bSAndreas Gohr * 99b0b7909bSAndreas Gohr * @param string $msg 100b0b7909bSAndreas Gohr */ 1012b2d0ba9SAndreas Gohr protected function quietecho($msg) { 102b0b7909bSAndreas Gohr if(!$this->quiet) echo $msg; 103b0b7909bSAndreas Gohr } 104981f048aSGabriel Birke} 105981f048aSGabriel Birke 106b0b7909bSAndreas Gohr// Main 107b0b7909bSAndreas Gohr$cli = new IndexerCLI(); 108b0b7909bSAndreas Gohr$cli->run(); 109