From b0b7909bdd454e9614f4ffe34f384b0da0ce4585 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 20 Mar 2014 20:55:57 +0100 Subject: converted some more CLI tools, minor CLI class updates --- bin/indexer.php | 176 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 89 insertions(+), 87 deletions(-) (limited to 'bin/indexer.php') diff --git a/bin/indexer.php b/bin/indexer.php index 6f6b5d9fa..76269f95a 100755 --- a/bin/indexer.php +++ b/bin/indexer.php @@ -1,98 +1,100 @@ #!/usr/bin/php isError() ) { - fwrite( STDERR, $OPTS->getMessage() . "\n"); - _usage(); - exit(1); -} -$CLEAR = false; -$QUIET = false; -$INDEXER = null; -foreach ($OPTS->options as $key => $val) { - switch ($key) { - case 'h': - case 'help': - _usage(); - exit; - case 'c': - case 'clear': - $CLEAR = true; - break; - case 'q': - case 'quiet': - $QUIET = true; - break; +if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/'); +define('NOSESSION', 1); +require_once(DOKU_INC . 'inc/init.php'); + +class IndexerCLI extends DokuCLI { + + private $quiet = false; + private $clear = false; + + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + $options->setHelp( + 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' . + 'given the index is cleared first.' + ); + + $options->registerOption( + 'clear', + 'clear the index before updating', + 'c' + ); + $options->registerOption( + 'quiet', + 'don\'t produce any output', + 'q' + ); } -} - -#------------------------------------------------------------------------------ -# Action - -if($CLEAR) _clearindex(); -_update(); - - - -#------------------------------------------------------------------------------ - -function _usage() { - print "Usage: indexer.php - - Updates the searchindex by indexing all new or changed pages - when the -c option is given the index is cleared first. - OPTIONS - -h, --help show this help and exit - -c, --clear clear the index before updating - -q, --quiet don't produce any output -"; -} - -function _update(){ - global $conf; - $data = array(); - _quietecho("Searching pages... "); - search($data,$conf['datadir'],'search_allpages',array('skipacl' => true)); - _quietecho(count($data)." pages found.\n"); - - foreach($data as $val){ - _index($val['id']); + /** + * Your main program + * + * Arguments and options have been parsed when this is run + * + * @param DokuCLI_Options $options + * @return void + */ + protected function main(DokuCLI_Options $options) { + $this->clear = $options->getOpt('clear'); + $this->quiet = $options->getOpt('quiet'); + + if($this->clear) $this->clearindex(); + + $this->update(); } -} -function _index($id){ - global $CLEAR; - global $QUIET; + /** + * Update the index + */ + function update() { + global $conf; + $data = array(); + $this->quietecho("Searching pages... "); + search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true)); + $this->quietecho(count($data) . " pages found.\n"); + + foreach($data as $val) { + $this->index($val['id']); + } + } - _quietecho("$id... "); - idx_addPage($id, !$QUIET, $CLEAR); - _quietecho("done.\n"); -} + /** + * Index the given page + * + * @param string $id + */ + function index($id) { + $this->quietecho("$id... "); + idx_addPage($id, !$this->quiet, $this->clear); + $this->quietecho("done.\n"); + } -/** - * Clear all index files - */ -function _clearindex(){ - _quietecho("Clearing index... "); - idx_get_indexer()->clear(); - _quietecho("done.\n"); -} + /** + * Clear all index files + */ + function clearindex() { + $this->quietecho("Clearing index... "); + idx_get_indexer()->clear(); + $this->quietecho("done.\n"); + } -function _quietecho($msg) { - global $QUIET; - if(!$QUIET) echo $msg; + /** + * Print message if not supressed + * + * @param string $msg + */ + function quietecho($msg) { + if(!$this->quiet) echo $msg; + } } -//Setup VIM: ex: et ts=2 : +// Main +$cli = new IndexerCLI(); +$cli->run(); \ No newline at end of file -- cgit v1.2.3 From 1c36b3d86f90185519cadaad85251922dc771fe1 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 16 May 2014 09:11:15 +0200 Subject: code reformat --- bin/indexer.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'bin/indexer.php') diff --git a/bin/indexer.php b/bin/indexer.php index 76269f95a..13895c36a 100755 --- a/bin/indexer.php +++ b/bin/indexer.php @@ -1,9 +1,12 @@ #!/usr/bin/php setHelp( - 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' . - 'given the index is cleared first.' + 'Updates the searchindex by indexing all new or changed pages. When the -c option is '. + 'given the index is cleared first.' ); $options->registerOption( - 'clear', - 'clear the index before updating', - 'c' + 'clear', + 'clear the index before updating', + 'c' ); $options->registerOption( - 'quiet', - 'don\'t produce any output', - 'q' + 'quiet', + 'don\'t produce any output', + 'q' ); } @@ -58,7 +61,7 @@ class IndexerCLI extends DokuCLI { $data = array(); $this->quietecho("Searching pages... "); search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true)); - $this->quietecho(count($data) . " pages found.\n"); + $this->quietecho(count($data)." pages found.\n"); foreach($data as $val) { $this->index($val['id']); -- cgit v1.2.3