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