1cbfa4829SPhy#!/usr/bin/env php 224c3e0d2SAndreas Gohr<?php 3cbeaa4a0SAndreas Gohr 4141d9fe1SAndreas Gohruse dokuwiki\Logger; 5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI; 6cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options; 74027a91aSSatoshi Saharause dokuwiki\Search\Indexer; 8cbeaa4a0SAndreas Gohr 9b1f206e1SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/'); 10b0b7909bSAndreas Gohrdefine('NOSESSION', 1); 1124c3e0d2SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php'); 1224c3e0d2SAndreas Gohr 131c36b3d8SAndreas Gohr/** 141c36b3d8SAndreas Gohr * Update the Search Index from command line 151c36b3d8SAndreas Gohr */ 164027a91aSSatoshi Saharaclass IndexerCLI extends CLI 174027a91aSSatoshi Sahara{ 18b0b7909bSAndreas Gohr private $quiet = false; 19b0b7909bSAndreas Gohr private $clear = false; 20b0b7909bSAndreas Gohr 21b0b7909bSAndreas Gohr /** 22b0b7909bSAndreas Gohr * Register options and arguments on the given $options object 23b0b7909bSAndreas Gohr * 24cbeaa4a0SAndreas Gohr * @param Options $options 25b0b7909bSAndreas Gohr * @return void 26b0b7909bSAndreas Gohr */ 274027a91aSSatoshi Sahara protected function setup(Options $options) 284027a91aSSatoshi Sahara { 29b0b7909bSAndreas Gohr $options->setHelp( 30b0b7909bSAndreas Gohr 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' . 31b0b7909bSAndreas Gohr 'given the index is cleared first.' 32b0b7909bSAndreas Gohr ); 33b0b7909bSAndreas Gohr 34b0b7909bSAndreas Gohr $options->registerOption( 35b0b7909bSAndreas Gohr 'clear', 36b0b7909bSAndreas Gohr 'clear the index before updating', 37b0b7909bSAndreas Gohr 'c' 38b0b7909bSAndreas Gohr ); 39b0b7909bSAndreas Gohr $options->registerOption( 40b0b7909bSAndreas Gohr 'quiet', 41141d9fe1SAndreas Gohr 'DEPRECATED', 42b0b7909bSAndreas Gohr 'q' 43b0b7909bSAndreas Gohr ); 4424c3e0d2SAndreas Gohr } 4524c3e0d2SAndreas Gohr 46b0b7909bSAndreas Gohr /** 47b0b7909bSAndreas Gohr * Your main program 48b0b7909bSAndreas Gohr * 49b0b7909bSAndreas Gohr * Arguments and options have been parsed when this is run 50b0b7909bSAndreas Gohr * 51cbeaa4a0SAndreas Gohr * @param Options $options 52b0b7909bSAndreas Gohr * @return void 53b0b7909bSAndreas Gohr */ 544027a91aSSatoshi Sahara protected function main(Options $options) 554027a91aSSatoshi Sahara { 56b0b7909bSAndreas Gohr $this->clear = $options->getOpt('clear'); 57b0b7909bSAndreas Gohr $this->quiet = $options->getOpt('quiet'); 5824c3e0d2SAndreas Gohr 59141d9fe1SAndreas Gohr if ($this->quiet) { 60141d9fe1SAndreas Gohr Logger::deprecated('Calling bin/indexer.php with -q/--quiet is deprecated. Use --loglevel instead.'); 61141d9fe1SAndreas Gohr $this->setLogLevel('emergency'); 62141d9fe1SAndreas Gohr } 63141d9fe1SAndreas Gohr 64b0b7909bSAndreas Gohr if ($this->clear) $this->clearindex(); 6524c3e0d2SAndreas Gohr 66b0b7909bSAndreas Gohr $this->update(); 6724c3e0d2SAndreas Gohr } 6824c3e0d2SAndreas Gohr 69b0b7909bSAndreas Gohr /** 70b0b7909bSAndreas Gohr * Update the index 71b0b7909bSAndreas Gohr */ 724027a91aSSatoshi Sahara protected function update() 734027a91aSSatoshi Sahara { 7424c3e0d2SAndreas Gohr global $conf; 75b1f206e1SAndreas Gohr $data = []; 76141d9fe1SAndreas Gohr $this->notice('Searching pages...'); 77b1f206e1SAndreas Gohr search($data, $conf['datadir'], 'search_allpages', ['skipacl' => true]); 78141d9fe1SAndreas Gohr $this->info(count($data) . ' pages found.'); 7924c3e0d2SAndreas Gohr 8024c3e0d2SAndreas Gohr foreach ($data as $val) { 81b0b7909bSAndreas Gohr $this->index($val['id']); 8224c3e0d2SAndreas Gohr } 8324c3e0d2SAndreas Gohr } 8424c3e0d2SAndreas Gohr 85b0b7909bSAndreas Gohr /** 86b0b7909bSAndreas Gohr * Index the given page 87b0b7909bSAndreas Gohr * 88b0b7909bSAndreas Gohr * @param string $id 89b0b7909bSAndreas Gohr */ 904027a91aSSatoshi Sahara protected function index($id) 914027a91aSSatoshi Sahara { 92141d9fe1SAndreas Gohr $this->notice("$id indexing..."); 93141d9fe1SAndreas Gohr try { 9483b3acccSAndreas Gohr (new Indexer())->addPage($id, $this->clear); 95141d9fe1SAndreas Gohr $this->success("$id indexed."); 96141d9fe1SAndreas Gohr } catch (Throwable $e) { 97141d9fe1SAndreas Gohr $this->error("$id indexing error: " . $e->getMessage()); 98141d9fe1SAndreas Gohr $this->debug($e->getTraceAsString()); 99141d9fe1SAndreas Gohr return; 100141d9fe1SAndreas Gohr } 10124c3e0d2SAndreas Gohr } 10224c3e0d2SAndreas Gohr 10324c3e0d2SAndreas Gohr /** 10424c3e0d2SAndreas Gohr * Clear all index files 10524c3e0d2SAndreas Gohr */ 1064027a91aSSatoshi Sahara protected function clearindex() 1074027a91aSSatoshi Sahara { 108141d9fe1SAndreas Gohr $this->notice('Clearing index...'); 109*8788dbbdSsplitbrain (new Indexer())->clear(); 110141d9fe1SAndreas Gohr $this->success('Index cleared.'); 111b0b7909bSAndreas Gohr } 112981f048aSGabriel Birke} 113981f048aSGabriel Birke 114b0b7909bSAndreas Gohr// Main 115b0b7909bSAndreas Gohr$cli = new IndexerCLI(); 116b0b7909bSAndreas Gohr$cli->run(); 117