aboutsummaryrefslogtreecommitdiff
path: root/vendors/dokuwiki/bin/indexer.php
blob: 55f3608d59ec502bc9fb2f4698dea8cf2d03293e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/php
<?php
if ('cli' != php_sapi_name()) die();

ini_set('memory_limit','128M');
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC.'inc/common.php');
require_once(DOKU_INC.'inc/pageutils.php');
require_once(DOKU_INC.'inc/search.php');
require_once(DOKU_INC.'inc/indexer.php');
require_once(DOKU_INC.'inc/auth.php');
require_once(DOKU_INC.'inc/cliopts.php');
session_write_close();

// Version tag used to force rebuild on upgrade
// Need to keep in sync with lib/exe/indexer.php
if(!defined('INDEXER_VERSION')) define('INDEXER_VERSION', 2);

// handle options
$short_opts = 'hcuq';
$long_opts  = array('help', 'clear', 'update', 'quiet');
$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
if ( $OPTS->isError() ) {
    fwrite( STDERR, $OPTS->getMessage() . "\n");
    _usage();
    exit(1);
}
$CLEAR = false;
$QUIET = false;
foreach ($OPTS->options as $key => $val) {
    switch ($key) {
        case 'h':
        case 'help':
            _usage();
            exit;
        case 'c':
        case 'clear':
            $CLEAR = true;
            break;
        case 'q':
        case 'quiet':
            $QUIET = true;
            break;
    }
}

#------------------------------------------------------------------------------
# Action

if($CLEAR) _clearindex();
_update();



#------------------------------------------------------------------------------

function _usage() {
    print "Usage: indexer.php <options>

    Updates the searchindex by indexing all new or changed pages
    when the -c option is given the index is cleared first.

    OPTIONS
        -h, --help     show this help and exit
        -c, --clear    clear the index before updating
        -q, --quiet    don't produce any output
";
}

function _update(){
    global $conf;

    // upgrade to version 2
    if (!@file_exists($conf['indexdir'].'/pageword.idx')){
        _lock();
        idx_upgradePageWords();
        _unlock();
    }

    $data = array();
    _quietecho("Searching pages... ");
    search($data,$conf['datadir'],'search_allpages',array('skipacl' => true));
    _quietecho(count($data)." pages found.\n");

    foreach($data as $val){
        _index($val['id']);
    }
}

function _index($id){
    global $CLEAR;

    // if not cleared only update changed and new files
    if(!$CLEAR){
        $idxtag = metaFN($id,'.indexed');
        if(@file_exists($idxtag)){
            if(io_readFile($idxtag) >= INDEXER_VERSION){
                $last = @filemtime(metaFN($id,'.indexed'));
                if($last > @filemtime(wikiFN($id))) return;
            }
        }
    }

    _lock();
    _quietecho("$id... ");
    idx_addPage($id);
    io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
    _quietecho("done.\n");
    _unlock();
}

/**
 * lock the indexer system
 */
function _lock(){
    global $conf;
    $lock = $conf['lockdir'].'/_indexer.lock';
    $said = false;
    while(!@mkdir($lock, $conf['dmode'])){
        if(time()-@filemtime($lock) > 60*5){
            // looks like a stale lock - remove it
            @rmdir($lock);
        }else{
            if($said){
                _quietecho(".");
            }else{
                _quietecho("Waiting for lockfile (max. 5 min)");
                $said = true;
            }
            sleep(15);
        }
    }
    if($conf['dperm']) chmod($lock, $conf['dperm']);
    if($said) _quietecho("\n");
}

/**
 * unlock the indexer sytem
 */
function _unlock(){
    global $conf;
    $lock = $conf['lockdir'].'/_indexer.lock';
    @rmdir($lock);
}

/**
 * Clear all index files
 */
function _clearindex(){
    global $conf;
    _lock();
    _quietecho("Clearing index... ");
    io_saveFile($conf['indexdir'].'/page.idx','');
    $dir = @opendir($conf['indexdir']);
    if($dir!==false){
        while(($f = readdir($dir)) !== false){
            if(substr($f,-4)=='.idx' &&
               (substr($f,0,1)=='i' || substr($f,0,1)=='w'))
                @unlink($conf['indexdir']."/$f");
        }
    }
    _quietecho("done.\n");
    _unlock();
}

function _quietecho($msg) {
    global $QUIET;
    if(!$QUIET) echo $msg;
}

//Setup VIM: ex: et ts=2 enc=utf-8 :