124c3e0d2SAndreas Gohr#!/usr/bin/php 224c3e0d2SAndreas Gohr<?php 378ba70a7Schrisif ('cli' != php_sapi_name()) die(); 424c3e0d2SAndreas Gohr 524c3e0d2SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 624c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php'); 724c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php'); 824c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php'); 924c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/search.php'); 1024c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/indexer.php'); 1124c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/cliopts.php'); 1224c3e0d2SAndreas Gohrsession_write_close(); 1324c3e0d2SAndreas Gohr 14*66b95544STom N Harris// Version tag used to force rebuild on upgrade 15*66b95544STom N Harris// Need to keep in sync with lib/exe/indexer.php 16*66b95544STom N Harrisif(!defined('INDEXER_VERSION')) define('INDEXER_VERSION', 1); 17*66b95544STom N Harris 1824c3e0d2SAndreas Gohr// handle options 1924c3e0d2SAndreas Gohr$short_opts = 'hcu'; 2024c3e0d2SAndreas Gohr$long_opts = array('help', 'clean', 'update'); 2124c3e0d2SAndreas Gohr$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts); 2224c3e0d2SAndreas Gohrif ( $OPTS->isError() ) { 2324c3e0d2SAndreas Gohr fwrite( STDERR, $OPTS->getMessage() . "\n"); 2424c3e0d2SAndreas Gohr _usage(); 2524c3e0d2SAndreas Gohr exit(1); 2624c3e0d2SAndreas Gohr} 2724c3e0d2SAndreas Gohr$CLEAR = false; 2824c3e0d2SAndreas Gohrforeach ($OPTS->options as $key => $val) { 2924c3e0d2SAndreas Gohr switch ($key) { 3024c3e0d2SAndreas Gohr case 'h': 3124c3e0d2SAndreas Gohr case 'help': 3224c3e0d2SAndreas Gohr _usage(); 3324c3e0d2SAndreas Gohr exit; 3424c3e0d2SAndreas Gohr case 'c': 3524c3e0d2SAndreas Gohr case 'clear': 3624c3e0d2SAndreas Gohr $CLEAR = true; 3724c3e0d2SAndreas Gohr break; 3824c3e0d2SAndreas Gohr } 3924c3e0d2SAndreas Gohr} 4024c3e0d2SAndreas Gohr 4124c3e0d2SAndreas Gohr#------------------------------------------------------------------------------ 4224c3e0d2SAndreas Gohr# Action 4324c3e0d2SAndreas Gohr 4424c3e0d2SAndreas Gohrif($CLEAR) _clearindex(); 4524c3e0d2SAndreas Gohr_update(); 4624c3e0d2SAndreas Gohr 4724c3e0d2SAndreas Gohr 4824c3e0d2SAndreas Gohr 4924c3e0d2SAndreas Gohr#------------------------------------------------------------------------------ 5024c3e0d2SAndreas Gohr 5124c3e0d2SAndreas Gohrfunction _usage() { 5224c3e0d2SAndreas Gohr print "Usage: indexer.php <options> 5324c3e0d2SAndreas Gohr 5424c3e0d2SAndreas Gohr Updates the searchindex by indexing all new or changed pages 5524c3e0d2SAndreas Gohr when the -c option is given the index is cleared first. 5624c3e0d2SAndreas Gohr 5724c3e0d2SAndreas Gohr OPTIONS 5824c3e0d2SAndreas Gohr -h, --help show this help and exit 5924c3e0d2SAndreas Gohr -c, --clear clear the index before updating 6024c3e0d2SAndreas Gohr"; 6124c3e0d2SAndreas Gohr} 6224c3e0d2SAndreas Gohr 6324c3e0d2SAndreas Gohrfunction _update(){ 6424c3e0d2SAndreas Gohr global $conf; 6524c3e0d2SAndreas Gohr $data = array(); 6624c3e0d2SAndreas Gohr echo "Searching pages... "; 6724c3e0d2SAndreas Gohr search($data,$conf['datadir'],'search_allpages',array()); 6824c3e0d2SAndreas Gohr echo count($data)." pages found.\n"; 6924c3e0d2SAndreas Gohr 7024c3e0d2SAndreas Gohr foreach($data as $val){ 7124c3e0d2SAndreas Gohr _index($val['id']); 7224c3e0d2SAndreas Gohr } 7324c3e0d2SAndreas Gohr} 7424c3e0d2SAndreas Gohr 7524c3e0d2SAndreas Gohrfunction _index($id){ 7624c3e0d2SAndreas Gohr global $CLEAR; 7724c3e0d2SAndreas Gohr 7824c3e0d2SAndreas Gohr // if not cleared only update changed and new files 7924c3e0d2SAndreas Gohr if(!$CLEAR){ 80*66b95544STom N Harris $idxtag = metaFN($id,'.indexed'); 81*66b95544STom N Harris if(@file_exists($idxtag)){ 82*66b95544STom N Harris if(io_readFile($idxtag) >= INDEXER_VERSION){ 8324c3e0d2SAndreas Gohr $last = @filemtime(metaFN($id,'.indexed')); 8424c3e0d2SAndreas Gohr if($last > @filemtime(wikiFN($id))) return; 8524c3e0d2SAndreas Gohr } 86*66b95544STom N Harris } 87*66b95544STom N Harris } 8824c3e0d2SAndreas Gohr 8924c3e0d2SAndreas Gohr _lock(); 9024c3e0d2SAndreas Gohr echo "$id... "; 9124c3e0d2SAndreas Gohr idx_addPage($id); 92*66b95544STom N Harris io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION); 9324c3e0d2SAndreas Gohr echo "done.\n"; 9424c3e0d2SAndreas Gohr _unlock(); 9524c3e0d2SAndreas Gohr} 9624c3e0d2SAndreas Gohr 9724c3e0d2SAndreas Gohr/** 9824c3e0d2SAndreas Gohr * lock the indexer system 9924c3e0d2SAndreas Gohr */ 10024c3e0d2SAndreas Gohrfunction _lock(){ 10124c3e0d2SAndreas Gohr global $conf; 10224c3e0d2SAndreas Gohr $lock = $conf['lockdir'].'/_indexer.lock'; 10324c3e0d2SAndreas Gohr $said = false; 10444881d27STroels Liebe Bentsen while(!@mkdir($lock, $conf['dmode'])){ 10524c3e0d2SAndreas Gohr if(time()-@filemtime($lock) > 60*5){ 10624c3e0d2SAndreas Gohr // looks like a stale lock - remove it 10724c3e0d2SAndreas Gohr @rmdir($lock); 10824c3e0d2SAndreas Gohr }else{ 10924c3e0d2SAndreas Gohr if($said){ 11024c3e0d2SAndreas Gohr echo "."; 11124c3e0d2SAndreas Gohr }else{ 11224c3e0d2SAndreas Gohr echo "Waiting for lockfile (max. 5 min)"; 11324c3e0d2SAndreas Gohr $said = true; 11424c3e0d2SAndreas Gohr } 11524c3e0d2SAndreas Gohr sleep(15); 11624c3e0d2SAndreas Gohr } 11724c3e0d2SAndreas Gohr } 1181ca31cfeSAndreas Gohr if($conf['dperm']) chmod($lock, $conf['dperm']); 11924c3e0d2SAndreas Gohr if($said) print "\n"; 12024c3e0d2SAndreas Gohr} 12124c3e0d2SAndreas Gohr 12224c3e0d2SAndreas Gohr/** 12324c3e0d2SAndreas Gohr * unlock the indexer sytem 12424c3e0d2SAndreas Gohr */ 12524c3e0d2SAndreas Gohrfunction _unlock(){ 12624c3e0d2SAndreas Gohr global $conf; 12724c3e0d2SAndreas Gohr $lock = $conf['lockdir'].'/_indexer.lock'; 12824c3e0d2SAndreas Gohr @rmdir($lock); 12924c3e0d2SAndreas Gohr} 13024c3e0d2SAndreas Gohr 13124c3e0d2SAndreas Gohr/** 13224c3e0d2SAndreas Gohr * Clear all index files 13324c3e0d2SAndreas Gohr */ 13424c3e0d2SAndreas Gohrfunction _clearindex(){ 13524c3e0d2SAndreas Gohr global $conf; 13624c3e0d2SAndreas Gohr _lock(); 13724c3e0d2SAndreas Gohr echo "Clearing index... "; 138*66b95544STom N Harris io_saveFile($conf['indexdir'].'/page.idx',''); 139*66b95544STom N Harris $dir = @opendir($conf['indexdir']); 140*66b95544STom N Harris if($dir!==false){ 141*66b95544STom N Harris while(($f = readdir($dir)) !== false){ 142*66b95544STom N Harris if(substr($f,-4)=='.idx' && 143*66b95544STom N Harris (substr($f,0,1)=='i' || substr($f,0,1)=='w')) 144*66b95544STom N Harris @unlink($conf['indexdir']."/$f"); 145*66b95544STom N Harris } 146*66b95544STom N Harris } 14724c3e0d2SAndreas Gohr echo "done.\n"; 14824c3e0d2SAndreas Gohr _unlock(); 14924c3e0d2SAndreas Gohr} 15024c3e0d2SAndreas Gohr 15124c3e0d2SAndreas Gohr//Setup VIM: ex: et ts=2 enc=utf-8 : 152