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 // if not cleared only update changed and new files 90 if(!$CLEAR){ 91 $idxtag = metaFN($id,'.indexed'); 92 if(@file_exists($idxtag)){ 93 if(io_readFile($idxtag) == idx_get_version()){ 94 $last = @filemtime($idxtag); 95 if($last > @filemtime(wikiFN($id))) return; 96 } 97 } 98 } 99 100 _quietecho("$id... "); 101 $body = ''; 102 $data = array($id, $body); 103 $evt = new Doku_Event('INDEXER_PAGE_ADD', $data); 104 if ($evt->advise_before()) $data[1] = $data[1] . " " . rawWiki($id); 105 $evt->advise_after(); 106 unset($evt); 107 list($id,$body) = $data; 108 $said = false; 109 while(true) { 110 $result = $INDEXER->addPageWords($id, $body); 111 if ($result == "locked") { 112 if($said){ 113 _quietecho("."); 114 }else{ 115 _quietecho("Waiting for lockfile (max. 5 min)"); 116 $said = true; 117 } 118 sleep(15); 119 } else { 120 break; 121 } 122 } 123 if ($result) 124 io_saveFile(metaFN($id,'.indexed'), idx_get_version()); 125 _quietecho("done.\n"); 126} 127 128/** 129 * lock the indexer system 130 */ 131function _lock(){ 132 global $conf; 133 $lock = $conf['lockdir'].'/_indexer.lock'; 134 $said = false; 135 while(!@mkdir($lock, $conf['dmode'])){ 136 if(time()-@filemtime($lock) > 60*5){ 137 // looks like a stale lock - remove it 138 @rmdir($lock); 139 }else{ 140 if($said){ 141 _quietecho("."); 142 }else{ 143 _quietecho("Waiting for lockfile (max. 5 min)"); 144 $said = true; 145 } 146 sleep(15); 147 } 148 } 149 if($conf['dperm']) chmod($lock, $conf['dperm']); 150 if($said) _quietecho("\n"); 151} 152 153/** 154 * unlock the indexer sytem 155 */ 156function _unlock(){ 157 global $conf; 158 $lock = $conf['lockdir'].'/_indexer.lock'; 159 @rmdir($lock); 160} 161 162/** 163 * Clear all index files 164 */ 165function _clearindex(){ 166 global $conf; 167 _lock(); 168 _quietecho("Clearing index... "); 169 io_saveFile($conf['indexdir'].'/page.idx',''); 170 //io_saveFile($conf['indexdir'].'/title.idx',''); 171 $dir = @opendir($conf['indexdir']); 172 if($dir!==false){ 173 while(($f = readdir($dir)) !== false){ 174 if(substr($f,-4)=='.idx' && 175 (substr($f,0,1)=='i' || substr($f,0,1)=='w')) 176 @unlink($conf['indexdir']."/$f"); 177 } 178 } 179 @unlink($conf['indexdir'].'/lengths.idx'); 180 _quietecho("done.\n"); 181 _unlock(); 182} 183 184function _quietecho($msg) { 185 global $QUIET; 186 if(!$QUIET) echo $msg; 187} 188 189//Setup VIM: ex: et ts=2 enc=utf-8 : 190