summaryrefslogtreecommitdiff
path: root/bin/indexer.php
blob: 6f6b5d9fa53997d3d90656bbbc4dc5ef9de9b369 (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
#!/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/cliopts.php');
session_write_close();

// 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;
$INDEXER = null;
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;
    $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;
    global $QUIET;

    _quietecho("$id... ");
    idx_addPage($id, !$QUIET, $CLEAR);
    _quietecho("done.\n");
}

/**
 * Clear all index files
 */
function _clearindex(){
    _quietecho("Clearing index... ");
    idx_get_indexer()->clear();
    _quietecho("done.\n");
}

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

//Setup VIM: ex: et ts=2 :