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