xref: /dokuwiki/bin/indexer.php (revision 23ae1b94a29fd9c3219a8cb37f3f2e21fbe03bc6)
1#!/usr/bin/php
2<?php
3if ('cli' != php_sapi_name()) die();
4
5ini_set('memory_limit','128M');
6if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
7require_once(DOKU_INC.'inc/init.php');
8require_once(DOKU_INC.'inc/common.php');
9require_once(DOKU_INC.'inc/pageutils.php');
10require_once(DOKU_INC.'inc/search.php');
11require_once(DOKU_INC.'inc/indexer.php');
12require_once(DOKU_INC.'inc/auth.php');
13require_once(DOKU_INC.'inc/cliopts.php');
14session_write_close();
15
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);
20if ( $OPTS->isError() ) {
21    fwrite( STDERR, $OPTS->getMessage() . "\n");
22    _usage();
23    exit(1);
24}
25$CLEAR = false;
26$QUIET = false;
27$INDEXER = null;
28foreach ($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;
42    }
43}
44
45#------------------------------------------------------------------------------
46# Action
47
48if($CLEAR) _clearindex();
49_update();
50
51
52
53#------------------------------------------------------------------------------
54
55function _usage() {
56    print "Usage: indexer.php <options>
57
58    Updates the searchindex by indexing all new or changed pages
59    when the -c option is given the index is cleared first.
60
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
65";
66}
67
68function _update(){
69    global $conf;
70    global $INDEXER;
71
72    $INDEXER = idx_get_indexer();
73
74    $data = array();
75    _quietecho("Searching pages... ");
76    search($data,$conf['datadir'],'search_allpages',array('skipacl' => true));
77    _quietecho(count($data)." pages found.\n");
78
79    foreach($data as $val){
80        _index($val['id']);
81    }
82}
83
84function _index($id){
85    global $INDEXER;
86    global $CLEAR;
87    global $QUIET;
88
89    // if not cleared only update changed and new files
90    if($CLEAR){
91        $idxtag = metaFN($id,'.indexed');
92        if(@file_exists($idxtag)){
93            @unlink($idxtag);
94        }
95    }
96
97    _quietecho("$id... ");
98    idx_addPage($id, !$QUIET);
99    _quietecho("done.\n");
100}
101
102/**
103 * lock the indexer system
104 */
105function _lock(){
106    global $conf;
107    $lock = $conf['lockdir'].'/_indexer.lock';
108    $said = false;
109    while(!@mkdir($lock, $conf['dmode'])){
110        if(time()-@filemtime($lock) > 60*5){
111            // looks like a stale lock - remove it
112            @rmdir($lock);
113        }else{
114            if($said){
115                _quietecho(".");
116            }else{
117                _quietecho("Waiting for lockfile (max. 5 min)");
118                $said = true;
119            }
120            sleep(15);
121        }
122    }
123    if($conf['dperm']) chmod($lock, $conf['dperm']);
124    if($said) _quietecho("\n");
125}
126
127/**
128 * unlock the indexer sytem
129 */
130function _unlock(){
131    global $conf;
132    $lock = $conf['lockdir'].'/_indexer.lock';
133    @rmdir($lock);
134}
135
136/**
137 * Clear all index files
138 */
139function _clearindex(){
140    global $conf;
141    _lock();
142    _quietecho("Clearing index... ");
143    io_saveFile($conf['indexdir'].'/page.idx','');
144    //io_saveFile($conf['indexdir'].'/title.idx','');
145    $dir = @opendir($conf['indexdir']);
146    if($dir!==false){
147        while(($f = readdir($dir)) !== false){
148            if(substr($f,-4)=='.idx' &&
149               (substr($f,0,1)=='i' || substr($f,0,1)=='w'))
150                @unlink($conf['indexdir']."/$f");
151        }
152    }
153    @unlink($conf['indexdir'].'/lengths.idx');
154    _quietecho("done.\n");
155    _unlock();
156}
157
158function _quietecho($msg) {
159    global $QUIET;
160    if(!$QUIET) echo $msg;
161}
162
163//Setup VIM: ex: et ts=2 :
164