xref: /dokuwiki/bin/indexer.php (revision d041f8dbb022a5e66928f6ee2d591ad6fe176984)
124c3e0d2SAndreas Gohr#!/usr/bin/php
224c3e0d2SAndreas Gohr<?php
378ba70a7Schrisif ('cli' != php_sapi_name()) die();
424c3e0d2SAndreas Gohr
550b78159SElan Ruusamäeini_set('memory_limit','128M');
624c3e0d2SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
724c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
824c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php');
924c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php');
1024c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/search.php');
1124c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/indexer.php');
123a50618cSgweissbachrequire_once(DOKU_INC.'inc/auth.php');
1324c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/cliopts.php');
1424c3e0d2SAndreas Gohrsession_write_close();
1524c3e0d2SAndreas Gohr
1624c3e0d2SAndreas Gohr// handle options
17981f048aSGabriel Birke$short_opts = 'hcuq';
18981f048aSGabriel Birke$long_opts  = array('help', 'clear', 'update', 'quiet');
1924c3e0d2SAndreas Gohr$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
2024c3e0d2SAndreas Gohrif ( $OPTS->isError() ) {
2124c3e0d2SAndreas Gohr    fwrite( STDERR, $OPTS->getMessage() . "\n");
2224c3e0d2SAndreas Gohr    _usage();
2324c3e0d2SAndreas Gohr    exit(1);
2424c3e0d2SAndreas Gohr}
2524c3e0d2SAndreas Gohr$CLEAR = false;
26981f048aSGabriel Birke$QUIET = false;
279b41be24STom N Harris$INDEXER = null;
2824c3e0d2SAndreas Gohrforeach ($OPTS->options as $key => $val) {
2924c3e0d2SAndreas Gohr    switch ($key) {
3024c3e0d2SAndreas Gohr        case 'h':
3124c3e0d2SAndreas Gohr        case 'help':
3224c3e0d2SAndreas Gohr            _usage();
3324c3e0d2SAndreas Gohr            exit;
3424c3e0d2SAndreas Gohr        case 'c':
3524c3e0d2SAndreas Gohr        case 'clear':
3624c3e0d2SAndreas Gohr            $CLEAR = true;
3724c3e0d2SAndreas Gohr            break;
38981f048aSGabriel Birke        case 'q':
39981f048aSGabriel Birke        case 'quiet':
40981f048aSGabriel Birke            $QUIET = true;
41981f048aSGabriel Birke            break;
4224c3e0d2SAndreas Gohr    }
4324c3e0d2SAndreas Gohr}
4424c3e0d2SAndreas Gohr
4524c3e0d2SAndreas Gohr#------------------------------------------------------------------------------
4624c3e0d2SAndreas Gohr# Action
4724c3e0d2SAndreas Gohr
4824c3e0d2SAndreas Gohrif($CLEAR) _clearindex();
4924c3e0d2SAndreas Gohr_update();
5024c3e0d2SAndreas Gohr
5124c3e0d2SAndreas Gohr
5224c3e0d2SAndreas Gohr
5324c3e0d2SAndreas Gohr#------------------------------------------------------------------------------
5424c3e0d2SAndreas Gohr
5524c3e0d2SAndreas Gohrfunction _usage() {
5624c3e0d2SAndreas Gohr    print "Usage: indexer.php <options>
5724c3e0d2SAndreas Gohr
5824c3e0d2SAndreas Gohr    Updates the searchindex by indexing all new or changed pages
5924c3e0d2SAndreas Gohr    when the -c option is given the index is cleared first.
6024c3e0d2SAndreas Gohr
6124c3e0d2SAndreas Gohr    OPTIONS
6224c3e0d2SAndreas Gohr        -h, --help     show this help and exit
6324c3e0d2SAndreas Gohr        -c, --clear    clear the index before updating
64981f048aSGabriel Birke        -q, --quiet    don't produce any output
6524c3e0d2SAndreas Gohr";
6624c3e0d2SAndreas Gohr}
6724c3e0d2SAndreas Gohr
6824c3e0d2SAndreas Gohrfunction _update(){
6924c3e0d2SAndreas Gohr    global $conf;
709b41be24STom N Harris    global $INDEXER;
719b41be24STom N Harris
729b41be24STom N Harris    $INDEXER = idx_get_indexer();
73a0c5c349STom N Harris
7424c3e0d2SAndreas Gohr    $data = array();
75981f048aSGabriel Birke    _quietecho("Searching pages... ");
769df39f14SRoland Alder    search($data,$conf['datadir'],'search_allpages',array('skipacl' => true));
77981f048aSGabriel Birke    _quietecho(count($data)." pages found.\n");
7824c3e0d2SAndreas Gohr
7924c3e0d2SAndreas Gohr    foreach($data as $val){
8024c3e0d2SAndreas Gohr        _index($val['id']);
8124c3e0d2SAndreas Gohr    }
8224c3e0d2SAndreas Gohr}
8324c3e0d2SAndreas Gohr
8424c3e0d2SAndreas Gohrfunction _index($id){
859b41be24STom N Harris    global $INDEXER;
8624c3e0d2SAndreas Gohr    global $CLEAR;
879b41be24STom N Harris    global $QUIET;
8824c3e0d2SAndreas Gohr
89981f048aSGabriel Birke    _quietecho("$id... ");
90*d041f8dbSMichael Hamann    idx_addPage($id, !$QUIET, $CLEAR);
91981f048aSGabriel Birke    _quietecho("done.\n");
9224c3e0d2SAndreas Gohr}
9324c3e0d2SAndreas Gohr
9424c3e0d2SAndreas Gohr/**
9524c3e0d2SAndreas Gohr * lock the indexer system
9624c3e0d2SAndreas Gohr */
9724c3e0d2SAndreas Gohrfunction _lock(){
9824c3e0d2SAndreas Gohr    global $conf;
9924c3e0d2SAndreas Gohr    $lock = $conf['lockdir'].'/_indexer.lock';
10024c3e0d2SAndreas Gohr    $said = false;
10144881d27STroels Liebe Bentsen    while(!@mkdir($lock, $conf['dmode'])){
10224c3e0d2SAndreas Gohr        if(time()-@filemtime($lock) > 60*5){
10324c3e0d2SAndreas Gohr            // looks like a stale lock - remove it
10424c3e0d2SAndreas Gohr            @rmdir($lock);
10524c3e0d2SAndreas Gohr        }else{
10624c3e0d2SAndreas Gohr            if($said){
107981f048aSGabriel Birke                _quietecho(".");
10824c3e0d2SAndreas Gohr            }else{
109981f048aSGabriel Birke                _quietecho("Waiting for lockfile (max. 5 min)");
11024c3e0d2SAndreas Gohr                $said = true;
11124c3e0d2SAndreas Gohr            }
11224c3e0d2SAndreas Gohr            sleep(15);
11324c3e0d2SAndreas Gohr        }
11424c3e0d2SAndreas Gohr    }
1151ca31cfeSAndreas Gohr    if($conf['dperm']) chmod($lock, $conf['dperm']);
116981f048aSGabriel Birke    if($said) _quietecho("\n");
11724c3e0d2SAndreas Gohr}
11824c3e0d2SAndreas Gohr
11924c3e0d2SAndreas Gohr/**
12024c3e0d2SAndreas Gohr * unlock the indexer sytem
12124c3e0d2SAndreas Gohr */
12224c3e0d2SAndreas Gohrfunction _unlock(){
12324c3e0d2SAndreas Gohr    global $conf;
12424c3e0d2SAndreas Gohr    $lock = $conf['lockdir'].'/_indexer.lock';
12524c3e0d2SAndreas Gohr    @rmdir($lock);
12624c3e0d2SAndreas Gohr}
12724c3e0d2SAndreas Gohr
12824c3e0d2SAndreas Gohr/**
12924c3e0d2SAndreas Gohr * Clear all index files
13024c3e0d2SAndreas Gohr */
13124c3e0d2SAndreas Gohrfunction _clearindex(){
13224c3e0d2SAndreas Gohr    global $conf;
13324c3e0d2SAndreas Gohr    _lock();
134981f048aSGabriel Birke    _quietecho("Clearing index... ");
13566b95544STom N Harris    io_saveFile($conf['indexdir'].'/page.idx','');
1369b41be24STom N Harris    //io_saveFile($conf['indexdir'].'/title.idx','');
13766b95544STom N Harris    $dir = @opendir($conf['indexdir']);
13866b95544STom N Harris    if($dir!==false){
13966b95544STom N Harris        while(($f = readdir($dir)) !== false){
14066b95544STom N Harris            if(substr($f,-4)=='.idx' &&
14166b95544STom N Harris               (substr($f,0,1)=='i' || substr($f,0,1)=='w'))
14266b95544STom N Harris                @unlink($conf['indexdir']."/$f");
14366b95544STom N Harris        }
14466b95544STom N Harris    }
1459b41be24STom N Harris    @unlink($conf['indexdir'].'/lengths.idx');
146981f048aSGabriel Birke    _quietecho("done.\n");
14724c3e0d2SAndreas Gohr    _unlock();
14824c3e0d2SAndreas Gohr}
14924c3e0d2SAndreas Gohr
150981f048aSGabriel Birkefunction _quietecho($msg) {
151981f048aSGabriel Birke    global $QUIET;
152981f048aSGabriel Birke    if(!$QUIET) echo $msg;
153981f048aSGabriel Birke}
154981f048aSGabriel Birke
155e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
156