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