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// handle options 15$short_opts = 'hcu'; 16$long_opts = array('help', 'clean', 'update'); 17$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts); 18if ( $OPTS->isError() ) { 19 fwrite( STDERR, $OPTS->getMessage() . "\n"); 20 _usage(); 21 exit(1); 22} 23$CLEAR = false; 24foreach ($OPTS->options as $key => $val) { 25 switch ($key) { 26 case 'h': 27 case 'help': 28 _usage(); 29 exit; 30 case 'c': 31 case 'clear': 32 $CLEAR = true; 33 break; 34 } 35} 36 37#------------------------------------------------------------------------------ 38# Action 39 40if($CLEAR) _clearindex(); 41_update(); 42 43 44 45#------------------------------------------------------------------------------ 46 47function _usage() { 48 print "Usage: indexer.php <options> 49 50 Updates the searchindex by indexing all new or changed pages 51 when the -c option is given the index is cleared first. 52 53 OPTIONS 54 -h, --help show this help and exit 55 -c, --clear clear the index before updating 56"; 57} 58 59function _update(){ 60 global $conf; 61 $data = array(); 62 echo "Searching pages... "; 63 search($data,$conf['datadir'],'search_allpages',array()); 64 echo count($data)." pages found.\n"; 65 66 foreach($data as $val){ 67 _index($val['id']); 68 } 69} 70 71function _index($id){ 72 global $CLEAR; 73 74 // if not cleared only update changed and new files 75 if(!$CLEAR){ 76 $last = @filemtime(metaFN($id,'.indexed')); 77 if($last > @filemtime(wikiFN($id))) return; 78 } 79 80 _lock(); 81 echo "$id... "; 82 idx_addPage($id); 83 io_saveFile(metaFN($id,'.indexed'),' '); 84 echo "done.\n"; 85 _unlock(); 86} 87 88/** 89 * lock the indexer system 90 */ 91function _lock(){ 92 global $conf; 93 $lock = $conf['lockdir'].'/_indexer.lock'; 94 $said = false; 95 while(!@mkdir($lock, $conf['dmode'])){ 96 if(time()-@filemtime($lock) > 60*5){ 97 // looks like a stale lock - remove it 98 @rmdir($lock); 99 }else{ 100 if($said){ 101 echo "."; 102 }else{ 103 echo "Waiting for lockfile (max. 5 min)"; 104 $said = true; 105 } 106 sleep(15); 107 } 108 } 109 if($conf['dperm']) chmod($lock, $conf['dperm']); 110 if($said) print "\n"; 111} 112 113/** 114 * unlock the indexer sytem 115 */ 116function _unlock(){ 117 global $conf; 118 $lock = $conf['lockdir'].'/_indexer.lock'; 119 @rmdir($lock); 120} 121 122/** 123 * Clear all index files 124 */ 125function _clearindex(){ 126 global $conf; 127 _lock(); 128 echo "Clearing index... "; 129 io_saveFile($conf['cachedir'].'/word.idx',''); 130 io_saveFile($conf['cachedir'].'/page.idx',''); 131 io_saveFile($conf['cachedir'].'/index.idx',''); 132 echo "done.\n"; 133 _unlock(); 134} 135 136//Setup VIM: ex: et ts=2 enc=utf-8 : 137