xref: /dokuwiki/bin/indexer.php (revision 7c2ef4e8d524fb9262c5a08831220f9fb2dc11fe)
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;
27foreach ($OPTS->options as $key => $val) {
28    switch ($key) {
29        case 'h':
30        case 'help':
31            _usage();
32            exit;
33        case 'c':
34        case 'clear':
35            $CLEAR = true;
36            break;
37        case 'q':
38        case 'quiet':
39            $QUIET = true;
40            break;
41    }
42}
43
44#------------------------------------------------------------------------------
45# Action
46
47if($CLEAR) _clearindex();
48_update();
49
50
51
52#------------------------------------------------------------------------------
53
54function _usage() {
55    print "Usage: indexer.php <options>
56
57    Updates the searchindex by indexing all new or changed pages
58    when the -c option is given the index is cleared first.
59
60    OPTIONS
61        -h, --help     show this help and exit
62        -c, --clear    clear the index before updating
63        -q, --quiet    don't produce any output
64";
65}
66
67function _update(){
68    global $conf;
69
70    $data = array();
71    _quietecho("Searching pages... ");
72    search($data,$conf['datadir'],'search_allpages',array('skipacl' => true));
73    _quietecho(count($data)." pages found.\n");
74
75    foreach($data as $val){
76        _index($val['id']);
77    }
78}
79
80function _index($id){
81    global $CLEAR;
82
83    // if not cleared only update changed and new files
84    if(!$CLEAR){
85        $idxtag = metaFN($id,'.indexed');
86        if(@file_exists($idxtag)){
87            if(io_readFile($idxtag) == idx_get_version()){
88                $last = @filemtime(metaFN($id,'.indexed'));
89                if($last > @filemtime(wikiFN($id))) return;
90            }
91        }
92    }
93
94    _lock();
95    _quietecho("$id... ");
96    idx_addPage($id);
97    io_saveFile(metaFN($id,'.indexed'), idx_get_version());
98    _quietecho("done.\n");
99    _unlock();
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    _quietecho("done.\n");
154    _unlock();
155}
156
157function _quietecho($msg) {
158    global $QUIET;
159    if(!$QUIET) echo $msg;
160}
161
162//Setup VIM: ex: et ts=2 enc=utf-8 :
163