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', 2); 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 72 // upgrade to version 2 73 if (!@file_exists($conf['indexdir'].'/pageword.idx')){ 74 _lock(); 75 idx_upgradePageWords(); 76 _unlock(); 77 } 78 79 $data = array(); 80 _quietecho("Searching pages... "); 81 search($data,$conf['datadir'],'search_allpages',array()); 82 _quietecho(count($data)." pages found.\n"); 83 84 foreach($data as $val){ 85 _index($val['id']); 86 } 87} 88 89function _index($id){ 90 global $CLEAR; 91 92 // if not cleared only update changed and new files 93 if(!$CLEAR){ 94 $idxtag = metaFN($id,'.indexed'); 95 if(@file_exists($idxtag)){ 96 if(io_readFile($idxtag) >= INDEXER_VERSION){ 97 $last = @filemtime(metaFN($id,'.indexed')); 98 if($last > @filemtime(wikiFN($id))) return; 99 } 100 } 101 } 102 103 _lock(); 104 _quietecho("$id... "); 105 idx_addPage($id); 106 io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION); 107 _quietecho("done.\n"); 108 _unlock(); 109} 110 111/** 112 * lock the indexer system 113 */ 114function _lock(){ 115 global $conf; 116 $lock = $conf['lockdir'].'/_indexer.lock'; 117 $said = false; 118 while(!@mkdir($lock, $conf['dmode'])){ 119 if(time()-@filemtime($lock) > 60*5){ 120 // looks like a stale lock - remove it 121 @rmdir($lock); 122 }else{ 123 if($said){ 124 _quietecho("."); 125 }else{ 126 _quietecho("Waiting for lockfile (max. 5 min)"); 127 $said = true; 128 } 129 sleep(15); 130 } 131 } 132 if($conf['dperm']) chmod($lock, $conf['dperm']); 133 if($said) _quietecho("\n"); 134} 135 136/** 137 * unlock the indexer sytem 138 */ 139function _unlock(){ 140 global $conf; 141 $lock = $conf['lockdir'].'/_indexer.lock'; 142 @rmdir($lock); 143} 144 145/** 146 * Clear all index files 147 */ 148function _clearindex(){ 149 global $conf; 150 _lock(); 151 _quietecho("Clearing index... "); 152 io_saveFile($conf['indexdir'].'/page.idx',''); 153 $dir = @opendir($conf['indexdir']); 154 if($dir!==false){ 155 while(($f = readdir($dir)) !== false){ 156 if(substr($f,-4)=='.idx' && 157 (substr($f,0,1)=='i' || substr($f,0,1)=='w')) 158 @unlink($conf['indexdir']."/$f"); 159 } 160 } 161 _quietecho("done.\n"); 162 _unlock(); 163} 164 165function _quietecho($msg) { 166 global $QUIET; 167 if(!$QUIET) echo $msg; 168} 169 170//Setup VIM: ex: et ts=2 enc=utf-8 : 171