1#!/usr/bin/php 2<?php 3if ('cli' != php_sapi_name()) die(); 4 5ini_set('memory_limit','128M'); 6if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 7require_once(DOKU_INC.'inc/init.php'); 8require_once(DOKU_INC.'inc/common.php'); 9require_once(DOKU_INC.'inc/pageutils.php'); 10require_once(DOKU_INC.'inc/search.php'); 11require_once(DOKU_INC.'inc/indexer.php'); 12require_once(DOKU_INC.'inc/auth.php'); 13require_once(DOKU_INC.'inc/cliopts.php'); 14session_write_close(); 15 16// handle options 17$short_opts = 'hcuq'; 18$long_opts = array('help', 'clear', 'update', 'quiet'); 19$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts); 20if ( $OPTS->isError() ) { 21 fwrite( STDERR, $OPTS->getMessage() . "\n"); 22 _usage(); 23 exit(1); 24} 25$CLEAR = false; 26$QUIET = false; 27$INDEXER = null; 28foreach ($OPTS->options as $key => $val) { 29 switch ($key) { 30 case 'h': 31 case 'help': 32 _usage(); 33 exit; 34 case 'c': 35 case 'clear': 36 $CLEAR = true; 37 break; 38 case 'q': 39 case 'quiet': 40 $QUIET = true; 41 break; 42 } 43} 44 45#------------------------------------------------------------------------------ 46# Action 47 48if($CLEAR) _clearindex(); 49_update(); 50 51 52 53#------------------------------------------------------------------------------ 54 55function _usage() { 56 print "Usage: indexer.php <options> 57 58 Updates the searchindex by indexing all new or changed pages 59 when the -c option is given the index is cleared first. 60 61 OPTIONS 62 -h, --help show this help and exit 63 -c, --clear clear the index before updating 64 -q, --quiet don't produce any output 65"; 66} 67 68function _update(){ 69 global $conf; 70 global $INDEXER; 71 72 $INDEXER = idx_get_indexer(); 73 74 $data = array(); 75 _quietecho("Searching pages... "); 76 search($data,$conf['datadir'],'search_allpages',array('skipacl' => true)); 77 _quietecho(count($data)." pages found.\n"); 78 79 foreach($data as $val){ 80 _index($val['id']); 81 } 82} 83 84function _index($id){ 85 global $INDEXER; 86 global $CLEAR; 87 global $QUIET; 88 89 _quietecho("$id... "); 90 idx_addPage($id, !$QUIET, $CLEAR); 91 _quietecho("done.\n"); 92} 93 94/** 95 * lock the indexer system 96 */ 97function _lock(){ 98 global $conf; 99 $lock = $conf['lockdir'].'/_indexer.lock'; 100 $said = false; 101 while(!@mkdir($lock, $conf['dmode'])){ 102 if(time()-@filemtime($lock) > 60*5){ 103 // looks like a stale lock - remove it 104 @rmdir($lock); 105 }else{ 106 if($said){ 107 _quietecho("."); 108 }else{ 109 _quietecho("Waiting for lockfile (max. 5 min)"); 110 $said = true; 111 } 112 sleep(15); 113 } 114 } 115 if($conf['dperm']) chmod($lock, $conf['dperm']); 116 if($said) _quietecho("\n"); 117} 118 119/** 120 * unlock the indexer sytem 121 */ 122function _unlock(){ 123 global $conf; 124 $lock = $conf['lockdir'].'/_indexer.lock'; 125 @rmdir($lock); 126} 127 128/** 129 * Clear all index files 130 */ 131function _clearindex(){ 132 global $conf; 133 _lock(); 134 _quietecho("Clearing index... "); 135 io_saveFile($conf['indexdir'].'/page.idx',''); 136 io_saveFile($conf['indexdir'].'/title.idx',''); 137 io_saveFile($conf['indexdir'].'/pageword.idx',''); 138 io_saveFile($conf['indexdir'].'/metadata.idx',''); 139 $dir = @opendir($conf['indexdir']); 140 if($dir!==false){ 141 while(($f = readdir($dir)) !== false){ 142 if(substr($f,-4)=='.idx' && 143 (substr($f,0,1)=='i' || substr($f,0,1)=='w' 144 || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx')) 145 @unlink($conf['indexdir']."/$f"); 146 } 147 } 148 @unlink($conf['indexdir'].'/lengths.idx'); 149 _quietecho("done.\n"); 150 _unlock(); 151} 152 153function _quietecho($msg) { 154 global $QUIET; 155 if(!$QUIET) echo $msg; 156} 157 158//Setup VIM: ex: et ts=2 : 159