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