xref: /dokuwiki/bin/indexer.php (revision c9176a05f54edbe959b2bb0d3d4629c7b0d4545c)
1#!/usr/bin/php
2<?php
3if ('cli' != php_sapi_name()) die();
4
5if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
6require_once(DOKU_INC.'inc/init.php');
7require_once(DOKU_INC.'inc/common.php');
8require_once(DOKU_INC.'inc/pageutils.php');
9require_once(DOKU_INC.'inc/search.php');
10require_once(DOKU_INC.'inc/indexer.php');
11require_once(DOKU_INC.'inc/auth.php');
12require_once(DOKU_INC.'inc/cliopts.php');
13session_write_close();
14
15// Version tag used to force rebuild on upgrade
16// Need to keep in sync with lib/exe/indexer.php
17if(!defined('INDEXER_VERSION')) define('INDEXER_VERSION', 2);
18
19// handle options
20$short_opts = 'hcuq';
21$long_opts  = array('help', 'clear', 'update', 'quiet');
22$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
23if ( $OPTS->isError() ) {
24    fwrite( STDERR, $OPTS->getMessage() . "\n");
25    _usage();
26    exit(1);
27}
28$CLEAR = false;
29$QUIET = false;
30foreach ($OPTS->options as $key => $val) {
31    switch ($key) {
32        case 'h':
33        case 'help':
34            _usage();
35            exit;
36        case 'c':
37        case 'clear':
38            $CLEAR = true;
39            break;
40        case 'q':
41        case 'quiet':
42            $QUIET = true;
43            break;
44    }
45}
46
47#------------------------------------------------------------------------------
48# Action
49
50if($CLEAR) _clearindex();
51_update();
52
53
54
55#------------------------------------------------------------------------------
56
57function _usage() {
58    print "Usage: indexer.php <options>
59
60    Updates the searchindex by indexing all new or changed pages
61    when the -c option is given the index is cleared first.
62
63    OPTIONS
64        -h, --help     show this help and exit
65        -c, --clear    clear the index before updating
66        -q, --quiet    don't produce any output
67";
68}
69
70function _update(){
71    global $conf;
72
73    // upgrade to version 2
74    if (!@file_exists($conf['indexdir'].'/pageword.idx')){
75        _lock();
76        idx_upgradePageWords();
77        _unlock();
78    }
79
80    $data = array();
81    _quietecho("Searching pages... ");
82    search($data,$conf['datadir'],'search_allpages',array());
83    _quietecho(count($data)." pages found.\n");
84
85    foreach($data as $val){
86        _index($val['id']);
87    }
88}
89
90function _index($id){
91    global $CLEAR;
92
93    // if not cleared only update changed and new files
94    if(!$CLEAR){
95        $idxtag = metaFN($id,'.indexed');
96        if(@file_exists($idxtag)){
97            if(io_readFile($idxtag) >= INDEXER_VERSION){
98                $last = @filemtime(metaFN($id,'.indexed'));
99                if($last > @filemtime(wikiFN($id))) return;
100            }
101        }
102    }
103
104    _lock();
105    _quietecho("$id... ");
106    idx_addPage($id);
107    io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
108    _quietecho("done.\n");
109    _unlock();
110}
111
112/**
113 * lock the indexer system
114 */
115function _lock(){
116    global $conf;
117    $lock = $conf['lockdir'].'/_indexer.lock';
118    $said = false;
119    while(!@mkdir($lock, $conf['dmode'])){
120        if(time()-@filemtime($lock) > 60*5){
121            // looks like a stale lock - remove it
122            @rmdir($lock);
123        }else{
124            if($said){
125                _quietecho(".");
126            }else{
127                _quietecho("Waiting for lockfile (max. 5 min)");
128                $said = true;
129            }
130            sleep(15);
131        }
132    }
133    if($conf['dperm']) chmod($lock, $conf['dperm']);
134    if($said) _quietecho("\n");
135}
136
137/**
138 * unlock the indexer sytem
139 */
140function _unlock(){
141    global $conf;
142    $lock = $conf['lockdir'].'/_indexer.lock';
143    @rmdir($lock);
144}
145
146/**
147 * Clear all index files
148 */
149function _clearindex(){
150    global $conf;
151    _lock();
152    _quietecho("Clearing index... ");
153    io_saveFile($conf['indexdir'].'/page.idx','');
154    $dir = @opendir($conf['indexdir']);
155    if($dir!==false){
156        while(($f = readdir($dir)) !== false){
157            if(substr($f,-4)=='.idx' &&
158               (substr($f,0,1)=='i' || substr($f,0,1)=='w'))
159                @unlink($conf['indexdir']."/$f");
160        }
161    }
162    _quietecho("done.\n");
163    _unlock();
164}
165
166function _quietecho($msg) {
167    global $QUIET;
168    if(!$QUIET) echo $msg;
169}
170
171//Setup VIM: ex: et ts=2 enc=utf-8 :
172