Remove testing md5 hash from installer
[dokuwiki.git] / bin / indexer.php
blobf6aeb4f0e3293eee3409d55a93fbd815ca365c3d
1 #!/usr/bin/php
2 <?php
3 if ('cli' != php_sapi_name()) die();
5 ini_set('memory_limit','128M');
6 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
7 require_once(DOKU_INC.'inc/init.php');
8 require_once(DOKU_INC.'inc/common.php');
9 require_once(DOKU_INC.'inc/pageutils.php');
10 require_once(DOKU_INC.'inc/search.php');
11 require_once(DOKU_INC.'inc/indexer.php');
12 require_once(DOKU_INC.'inc/auth.php');
13 require_once(DOKU_INC.'inc/cliopts.php');
14 session_write_close();
16 // handle options
17 $short_opts = 'hcuq';
18 $long_opts = array('help', 'clear', 'update', 'quiet');
19 $OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
20 if ( $OPTS->isError() ) {
21 fwrite( STDERR, $OPTS->getMessage() . "\n");
22 _usage();
23 exit(1);
25 $CLEAR = false;
26 $QUIET = false;
27 $INDEXER = null;
28 foreach ($OPTS->options as $key => $val) {
29 switch ($key) {
30 case 'h':
31 case 'help':
32 _usage();
33 exit;
34 case 'c':
35 case 'clear':
36 $CLEAR = true;
37 break;
38 case 'q':
39 case 'quiet':
40 $QUIET = true;
41 break;
45 #------------------------------------------------------------------------------
46 # Action
48 if($CLEAR) _clearindex();
49 _update();
53 #------------------------------------------------------------------------------
55 function _usage() {
56 print "Usage: indexer.php <options>
58 Updates the searchindex by indexing all new or changed pages
59 when the -c option is given the index is cleared first.
61 OPTIONS
62 -h, --help show this help and exit
63 -c, --clear clear the index before updating
64 -q, --quiet don't produce any output
68 function _update(){
69 global $conf;
70 global $INDEXER;
72 $INDEXER = idx_get_indexer();
74 $data = array();
75 _quietecho("Searching pages... ");
76 search($data,$conf['datadir'],'search_allpages',array('skipacl' => true));
77 _quietecho(count($data)." pages found.\n");
79 foreach($data as $val){
80 _index($val['id']);
84 function _index($id){
85 global $INDEXER;
86 global $CLEAR;
87 global $QUIET;
89 _quietecho("$id... ");
90 idx_addPage($id, !$QUIET, $CLEAR);
91 _quietecho("done.\n");
94 /**
95 * lock the indexer system
97 function _lock(){
98 global $conf;
99 $lock = $conf['lockdir'].'/_indexer.lock';
100 $said = false;
101 while(!@mkdir($lock, $conf['dmode'])){
102 if(time()-@filemtime($lock) > 60*5){
103 // looks like a stale lock - remove it
104 @rmdir($lock);
105 }else{
106 if($said){
107 _quietecho(".");
108 }else{
109 _quietecho("Waiting for lockfile (max. 5 min)");
110 $said = true;
112 sleep(15);
115 if($conf['dperm']) chmod($lock, $conf['dperm']);
116 if($said) _quietecho("\n");
120 * unlock the indexer sytem
122 function _unlock(){
123 global $conf;
124 $lock = $conf['lockdir'].'/_indexer.lock';
125 @rmdir($lock);
129 * Clear all index files
131 function _clearindex(){
132 global $conf;
133 _lock();
134 _quietecho("Clearing index... ");
135 io_saveFile($conf['indexdir'].'/page.idx','');
136 io_saveFile($conf['indexdir'].'/title.idx','');
137 io_saveFile($conf['indexdir'].'/pageword.idx','');
138 io_saveFile($conf['indexdir'].'/metadata.idx','');
139 $dir = @opendir($conf['indexdir']);
140 if($dir!==false){
141 while(($f = readdir($dir)) !== false){
142 if(substr($f,-4)=='.idx' &&
143 (substr($f,0,1)=='i' || substr($f,0,1)=='w'
144 || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx'))
145 @unlink($conf['indexdir']."/$f");
148 @unlink($conf['indexdir'].'/lengths.idx');
149 _quietecho("done.\n");
150 _unlock();
153 function _quietecho($msg) {
154 global $QUIET;
155 if(!$QUIET) echo $msg;
158 //Setup VIM: ex: et ts=2 :