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