first commit. dokuwiki.
[h2N7SspZmY.git] / bin / indexer.php
blob55f3608d59ec502bc9fb2f4698dea8cf2d03293e
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 // Version tag used to force rebuild on upgrade
17 // Need to keep in sync with lib/exe/indexer.php
18 if(!defined('INDEXER_VERSION')) define('INDEXER_VERSION', 2);
20 // handle options
21 $short_opts = 'hcuq';
22 $long_opts = array('help', 'clear', 'update', 'quiet');
23 $OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
24 if ( $OPTS->isError() ) {
25 fwrite( STDERR, $OPTS->getMessage() . "\n");
26 _usage();
27 exit(1);
29 $CLEAR = false;
30 $QUIET = false;
31 foreach ($OPTS->options as $key => $val) {
32 switch ($key) {
33 case 'h':
34 case 'help':
35 _usage();
36 exit;
37 case 'c':
38 case 'clear':
39 $CLEAR = true;
40 break;
41 case 'q':
42 case 'quiet':
43 $QUIET = true;
44 break;
48 #------------------------------------------------------------------------------
49 # Action
51 if($CLEAR) _clearindex();
52 _update();
56 #------------------------------------------------------------------------------
58 function _usage() {
59 print "Usage: indexer.php <options>
61 Updates the searchindex by indexing all new or changed pages
62 when the -c option is given the index is cleared first.
64 OPTIONS
65 -h, --help show this help and exit
66 -c, --clear clear the index before updating
67 -q, --quiet don't produce any output
71 function _update(){
72 global $conf;
74 // upgrade to version 2
75 if (!@file_exists($conf['indexdir'].'/pageword.idx')){
76 _lock();
77 idx_upgradePageWords();
78 _unlock();
81 $data = array();
82 _quietecho("Searching pages... ");
83 search($data,$conf['datadir'],'search_allpages',array('skipacl' => true));
84 _quietecho(count($data)." pages found.\n");
86 foreach($data as $val){
87 _index($val['id']);
91 function _index($id){
92 global $CLEAR;
94 // if not cleared only update changed and new files
95 if(!$CLEAR){
96 $idxtag = metaFN($id,'.indexed');
97 if(@file_exists($idxtag)){
98 if(io_readFile($idxtag) >= INDEXER_VERSION){
99 $last = @filemtime(metaFN($id,'.indexed'));
100 if($last > @filemtime(wikiFN($id))) return;
105 _lock();
106 _quietecho("$id... ");
107 idx_addPage($id);
108 io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
109 _quietecho("done.\n");
110 _unlock();
114 * lock the indexer system
116 function _lock(){
117 global $conf;
118 $lock = $conf['lockdir'].'/_indexer.lock';
119 $said = false;
120 while(!@mkdir($lock, $conf['dmode'])){
121 if(time()-@filemtime($lock) > 60*5){
122 // looks like a stale lock - remove it
123 @rmdir($lock);
124 }else{
125 if($said){
126 _quietecho(".");
127 }else{
128 _quietecho("Waiting for lockfile (max. 5 min)");
129 $said = true;
131 sleep(15);
134 if($conf['dperm']) chmod($lock, $conf['dperm']);
135 if($said) _quietecho("\n");
139 * unlock the indexer sytem
141 function _unlock(){
142 global $conf;
143 $lock = $conf['lockdir'].'/_indexer.lock';
144 @rmdir($lock);
148 * Clear all index files
150 function _clearindex(){
151 global $conf;
152 _lock();
153 _quietecho("Clearing index... ");
154 io_saveFile($conf['indexdir'].'/page.idx','');
155 $dir = @opendir($conf['indexdir']);
156 if($dir!==false){
157 while(($f = readdir($dir)) !== false){
158 if(substr($f,-4)=='.idx' &&
159 (substr($f,0,1)=='i' || substr($f,0,1)=='w'))
160 @unlink($conf['indexdir']."/$f");
163 _quietecho("done.\n");
164 _unlock();
167 function _quietecho($msg) {
168 global $QUIET;
169 if(!$QUIET) echo $msg;
172 //Setup VIM: ex: et ts=2 enc=utf-8 :