xref: /dokuwiki/bin/indexer.php (revision 44881d272282937c9bb745f462c947319d404dd0)
124c3e0d2SAndreas Gohr#!/usr/bin/php
224c3e0d2SAndreas Gohr<?php
324c3e0d2SAndreas Gohr
424c3e0d2SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
524c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
624c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php');
724c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php');
824c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/search.php');
924c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/indexer.php');
1024c3e0d2SAndreas Gohrrequire_once(DOKU_INC.'inc/cliopts.php');
1124c3e0d2SAndreas Gohrsession_write_close();
1224c3e0d2SAndreas Gohr
1324c3e0d2SAndreas Gohr// handle options
1424c3e0d2SAndreas Gohr$short_opts = 'hcu';
1524c3e0d2SAndreas Gohr$long_opts  = array('help', 'clean', 'update');
1624c3e0d2SAndreas Gohr$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
1724c3e0d2SAndreas Gohrif ( $OPTS->isError() ) {
1824c3e0d2SAndreas Gohr    fwrite( STDERR, $OPTS->getMessage() . "\n");
1924c3e0d2SAndreas Gohr    _usage();
2024c3e0d2SAndreas Gohr    exit(1);
2124c3e0d2SAndreas Gohr}
2224c3e0d2SAndreas Gohr$CLEAR = false;
2324c3e0d2SAndreas Gohrforeach ($OPTS->options as $key => $val) {
2424c3e0d2SAndreas Gohr    switch ($key) {
2524c3e0d2SAndreas Gohr        case 'h':
2624c3e0d2SAndreas Gohr        case 'help':
2724c3e0d2SAndreas Gohr            _usage();
2824c3e0d2SAndreas Gohr            exit;
2924c3e0d2SAndreas Gohr        case 'c':
3024c3e0d2SAndreas Gohr        case 'clear':
3124c3e0d2SAndreas Gohr            $CLEAR = true;
3224c3e0d2SAndreas Gohr            break;
3324c3e0d2SAndreas Gohr    }
3424c3e0d2SAndreas Gohr}
3524c3e0d2SAndreas Gohr
3624c3e0d2SAndreas Gohr#------------------------------------------------------------------------------
3724c3e0d2SAndreas Gohr# Action
3824c3e0d2SAndreas Gohr
3924c3e0d2SAndreas Gohrif($CLEAR) _clearindex();
4024c3e0d2SAndreas Gohr_update();
4124c3e0d2SAndreas Gohr
4224c3e0d2SAndreas Gohr
4324c3e0d2SAndreas Gohr
4424c3e0d2SAndreas Gohr#------------------------------------------------------------------------------
4524c3e0d2SAndreas Gohr
4624c3e0d2SAndreas Gohrfunction _usage() {
4724c3e0d2SAndreas Gohr    print "Usage: indexer.php <options>
4824c3e0d2SAndreas Gohr
4924c3e0d2SAndreas Gohr    Updates the searchindex by indexing all new or changed pages
5024c3e0d2SAndreas Gohr    when the -c option is given the index is cleared first.
5124c3e0d2SAndreas Gohr
5224c3e0d2SAndreas Gohr    OPTIONS
5324c3e0d2SAndreas Gohr        -h, --help     show this help and exit
5424c3e0d2SAndreas Gohr        -c, --clear    clear the index before updating
5524c3e0d2SAndreas Gohr";
5624c3e0d2SAndreas Gohr}
5724c3e0d2SAndreas Gohr
5824c3e0d2SAndreas Gohrfunction _update(){
5924c3e0d2SAndreas Gohr    global $conf;
6024c3e0d2SAndreas Gohr    $data = array();
6124c3e0d2SAndreas Gohr    echo "Searching pages... ";
6224c3e0d2SAndreas Gohr    search($data,$conf['datadir'],'search_allpages',array());
6324c3e0d2SAndreas Gohr    echo count($data)." pages found.\n";
6424c3e0d2SAndreas Gohr
6524c3e0d2SAndreas Gohr    foreach($data as $val){
6624c3e0d2SAndreas Gohr        _index($val['id']);
6724c3e0d2SAndreas Gohr    }
6824c3e0d2SAndreas Gohr}
6924c3e0d2SAndreas Gohr
7024c3e0d2SAndreas Gohrfunction _index($id){
7124c3e0d2SAndreas Gohr    global $CLEAR;
7224c3e0d2SAndreas Gohr
7324c3e0d2SAndreas Gohr    // if not cleared only update changed and new files
7424c3e0d2SAndreas Gohr    if(!$CLEAR){
7524c3e0d2SAndreas Gohr      $last = @filemtime(metaFN($id,'.indexed'));
7624c3e0d2SAndreas Gohr      if($last > @filemtime(wikiFN($id))) return;
7724c3e0d2SAndreas Gohr    }
7824c3e0d2SAndreas Gohr
7924c3e0d2SAndreas Gohr    _lock();
8024c3e0d2SAndreas Gohr    echo "$id... ";
8124c3e0d2SAndreas Gohr    idx_addPage($id);
8224c3e0d2SAndreas Gohr    io_saveFile(metaFN($id,'.indexed'),' ');
8324c3e0d2SAndreas Gohr    echo "done.\n";
8424c3e0d2SAndreas Gohr    _unlock();
8524c3e0d2SAndreas Gohr}
8624c3e0d2SAndreas Gohr
8724c3e0d2SAndreas Gohr/**
8824c3e0d2SAndreas Gohr * lock the indexer system
8924c3e0d2SAndreas Gohr */
9024c3e0d2SAndreas Gohrfunction _lock(){
9124c3e0d2SAndreas Gohr    global $conf;
9224c3e0d2SAndreas Gohr    $lock = $conf['lockdir'].'/_indexer.lock';
9324c3e0d2SAndreas Gohr    $said = false;
94*44881d27STroels Liebe Bentsen    while(!@mkdir($lock, $conf['dmode'])){
9524c3e0d2SAndreas Gohr        if(time()-@filemtime($lock) > 60*5){
9624c3e0d2SAndreas Gohr            // looks like a stale lock - remove it
9724c3e0d2SAndreas Gohr            @rmdir($lock);
9824c3e0d2SAndreas Gohr        }else{
9924c3e0d2SAndreas Gohr            if($said){
10024c3e0d2SAndreas Gohr                echo ".";
10124c3e0d2SAndreas Gohr            }else{
10224c3e0d2SAndreas Gohr                echo "Waiting for lockfile (max. 5 min)";
10324c3e0d2SAndreas Gohr                $said = true;
10424c3e0d2SAndreas Gohr            }
10524c3e0d2SAndreas Gohr            sleep(15);
10624c3e0d2SAndreas Gohr        }
10724c3e0d2SAndreas Gohr    }
108*44881d27STroels Liebe Bentsen    if(isset($conf['dmask'])) { chmod($lock, $conf['dmask']); }
10924c3e0d2SAndreas Gohr    if($said) print "\n";
11024c3e0d2SAndreas Gohr}
11124c3e0d2SAndreas Gohr
11224c3e0d2SAndreas Gohr/**
11324c3e0d2SAndreas Gohr * unlock the indexer sytem
11424c3e0d2SAndreas Gohr */
11524c3e0d2SAndreas Gohrfunction _unlock(){
11624c3e0d2SAndreas Gohr    global $conf;
11724c3e0d2SAndreas Gohr    $lock = $conf['lockdir'].'/_indexer.lock';
11824c3e0d2SAndreas Gohr    @rmdir($lock);
11924c3e0d2SAndreas Gohr}
12024c3e0d2SAndreas Gohr
12124c3e0d2SAndreas Gohr/**
12224c3e0d2SAndreas Gohr * Clear all index files
12324c3e0d2SAndreas Gohr */
12424c3e0d2SAndreas Gohrfunction _clearindex(){
12524c3e0d2SAndreas Gohr    global $conf;
12624c3e0d2SAndreas Gohr    _lock();
12724c3e0d2SAndreas Gohr    echo "Clearing index... ";
12824c3e0d2SAndreas Gohr    io_saveFile($conf['cachedir'].'/word.idx','');
12924c3e0d2SAndreas Gohr    io_saveFile($conf['cachedir'].'/page.idx','');
13024c3e0d2SAndreas Gohr    io_saveFile($conf['cachedir'].'/index.idx','');
13124c3e0d2SAndreas Gohr    echo "done.\n";
13224c3e0d2SAndreas Gohr    _unlock();
13324c3e0d2SAndreas Gohr}
13424c3e0d2SAndreas Gohr
13524c3e0d2SAndreas Gohr//Setup VIM: ex: et ts=2 enc=utf-8 :
136