xref: /dokuwiki/bin/indexer.php (revision d613051ae9b5db0528293dbe6d918df6dbd7dc4e)
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/cliopts.php');
12session_write_close();
13
14// Version tag used to force rebuild on upgrade
15// Need to keep in sync with lib/exe/indexer.php
16if(!defined('INDEXER_VERSION')) define('INDEXER_VERSION', 1);
17
18// handle options
19$short_opts = 'hcu';
20$long_opts  = array('help', 'clean', 'update');
21$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
22if ( $OPTS->isError() ) {
23    fwrite( STDERR, $OPTS->getMessage() . "\n");
24    _usage();
25    exit(1);
26}
27$CLEAR = false;
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    }
39}
40
41#------------------------------------------------------------------------------
42# Action
43
44if($CLEAR) _clearindex();
45_update();
46
47
48
49#------------------------------------------------------------------------------
50
51function _usage() {
52    print "Usage: indexer.php <options>
53
54    Updates the searchindex by indexing all new or changed pages
55    when the -c option is given the index is cleared first.
56
57    OPTIONS
58        -h, --help     show this help and exit
59        -c, --clear    clear the index before updating
60";
61}
62
63function _update(){
64    global $conf;
65    $data = array();
66    echo "Searching pages... ";
67    search($data,$conf['datadir'],'search_allpages',array());
68    echo count($data)." pages found.\n";
69
70    foreach($data as $val){
71        _index($val['id']);
72    }
73}
74
75function _index($id){
76    global $CLEAR;
77
78    // if not cleared only update changed and new files
79    if(!$CLEAR){
80        $idxtag = metaFN($id,'.indexed');
81        if(@file_exists($idxtag)){
82            if(io_readFile($idxtag) >= INDEXER_VERSION){
83                $last = @filemtime(metaFN($id,'.indexed'));
84                if($last > @filemtime(wikiFN($id))) return;
85            }
86        }
87    }
88
89    _lock();
90    echo "$id... ";
91    idx_addPage($id);
92    io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
93    echo "done.\n";
94    _unlock();
95}
96
97/**
98 * lock the indexer system
99 */
100function _lock(){
101    global $conf;
102    $lock = $conf['lockdir'].'/_indexer.lock';
103    $said = false;
104    while(!@mkdir($lock, $conf['dmode'])){
105        if(time()-@filemtime($lock) > 60*5){
106            // looks like a stale lock - remove it
107            @rmdir($lock);
108        }else{
109            if($said){
110                echo ".";
111            }else{
112                echo "Waiting for lockfile (max. 5 min)";
113                $said = true;
114            }
115            sleep(15);
116        }
117    }
118    if($conf['dperm']) chmod($lock, $conf['dperm']);
119    if($said) print "\n";
120}
121
122/**
123 * unlock the indexer sytem
124 */
125function _unlock(){
126    global $conf;
127    $lock = $conf['lockdir'].'/_indexer.lock';
128    @rmdir($lock);
129}
130
131/**
132 * Clear all index files
133 */
134function _clearindex(){
135    global $conf;
136    _lock();
137    echo "Clearing index... ";
138    io_saveFile($conf['indexdir'].'/page.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                @unlink($conf['indexdir']."/$f");
145        }
146    }
147    echo "done.\n";
148    _unlock();
149}
150
151//Setup VIM: ex: et ts=2 enc=utf-8 :
152