xref: /dokuwiki/bin/indexer.php (revision a0070b52bbd24f6972b819fa8ff4bdbfe81b5bbc)
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
1666b95544STom N Harris// Version tag used to force rebuild on upgrade
1766b95544STom N Harris// Need to keep in sync with lib/exe/indexer.php
18a0c5c349STom N Harrisif(!defined('INDEXER_VERSION')) define('INDEXER_VERSION', 2);
1966b95544STom N Harris
2024c3e0d2SAndreas Gohr// handle options
21981f048aSGabriel Birke$short_opts = 'hcuq';
22981f048aSGabriel Birke$long_opts  = array('help', 'clear', 'update', 'quiet');
2324c3e0d2SAndreas Gohr$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
2424c3e0d2SAndreas Gohrif ( $OPTS->isError() ) {
2524c3e0d2SAndreas Gohr    fwrite( STDERR, $OPTS->getMessage() . "\n");
2624c3e0d2SAndreas Gohr    _usage();
2724c3e0d2SAndreas Gohr    exit(1);
2824c3e0d2SAndreas Gohr}
2924c3e0d2SAndreas Gohr$CLEAR = false;
30981f048aSGabriel Birke$QUIET = false;
3124c3e0d2SAndreas Gohrforeach ($OPTS->options as $key => $val) {
3224c3e0d2SAndreas Gohr    switch ($key) {
3324c3e0d2SAndreas Gohr        case 'h':
3424c3e0d2SAndreas Gohr        case 'help':
3524c3e0d2SAndreas Gohr            _usage();
3624c3e0d2SAndreas Gohr            exit;
3724c3e0d2SAndreas Gohr        case 'c':
3824c3e0d2SAndreas Gohr        case 'clear':
3924c3e0d2SAndreas Gohr            $CLEAR = true;
4024c3e0d2SAndreas Gohr            break;
41981f048aSGabriel Birke        case 'q':
42981f048aSGabriel Birke        case 'quiet':
43981f048aSGabriel Birke            $QUIET = true;
44981f048aSGabriel Birke            break;
4524c3e0d2SAndreas Gohr    }
4624c3e0d2SAndreas Gohr}
4724c3e0d2SAndreas Gohr
4824c3e0d2SAndreas Gohr#------------------------------------------------------------------------------
4924c3e0d2SAndreas Gohr# Action
5024c3e0d2SAndreas Gohr
5124c3e0d2SAndreas Gohrif($CLEAR) _clearindex();
5224c3e0d2SAndreas Gohr_update();
5324c3e0d2SAndreas Gohr
5424c3e0d2SAndreas Gohr
5524c3e0d2SAndreas Gohr
5624c3e0d2SAndreas Gohr#------------------------------------------------------------------------------
5724c3e0d2SAndreas Gohr
5824c3e0d2SAndreas Gohrfunction _usage() {
5924c3e0d2SAndreas Gohr    print "Usage: indexer.php <options>
6024c3e0d2SAndreas Gohr
6124c3e0d2SAndreas Gohr    Updates the searchindex by indexing all new or changed pages
6224c3e0d2SAndreas Gohr    when the -c option is given the index is cleared first.
6324c3e0d2SAndreas Gohr
6424c3e0d2SAndreas Gohr    OPTIONS
6524c3e0d2SAndreas Gohr        -h, --help     show this help and exit
6624c3e0d2SAndreas Gohr        -c, --clear    clear the index before updating
67981f048aSGabriel Birke        -q, --quiet    don't produce any output
6824c3e0d2SAndreas Gohr";
6924c3e0d2SAndreas Gohr}
7024c3e0d2SAndreas Gohr
7124c3e0d2SAndreas Gohrfunction _update(){
7224c3e0d2SAndreas Gohr    global $conf;
73a0c5c349STom N Harris
74a0c5c349STom N Harris    // upgrade to version 2
75a0c5c349STom N Harris    if (!@file_exists($conf['indexdir'].'/pageword.idx')){
76a0c5c349STom N Harris        _lock();
77a0c5c349STom N Harris        idx_upgradePageWords();
78a0c5c349STom N Harris        _unlock();
79a0c5c349STom N Harris    }
80a0c5c349STom N Harris
8124c3e0d2SAndreas Gohr    $data = array();
82981f048aSGabriel Birke    _quietecho("Searching pages... ");
839df39f14SRoland Alder    search($data,$conf['datadir'],'search_allpages',array('skipacl' => true));
84981f048aSGabriel Birke    _quietecho(count($data)." pages found.\n");
8524c3e0d2SAndreas Gohr
8624c3e0d2SAndreas Gohr    foreach($data as $val){
8724c3e0d2SAndreas Gohr        _index($val['id']);
8824c3e0d2SAndreas Gohr    }
8924c3e0d2SAndreas Gohr}
9024c3e0d2SAndreas Gohr
9124c3e0d2SAndreas Gohrfunction _index($id){
9224c3e0d2SAndreas Gohr    global $CLEAR;
9324c3e0d2SAndreas Gohr
9424c3e0d2SAndreas Gohr    // if not cleared only update changed and new files
9524c3e0d2SAndreas Gohr    if(!$CLEAR){
9666b95544STom N Harris        $idxtag = metaFN($id,'.indexed');
9766b95544STom N Harris        if(@file_exists($idxtag)){
9866b95544STom N Harris            if(io_readFile($idxtag) >= INDEXER_VERSION){
9924c3e0d2SAndreas Gohr                $last = @filemtime(metaFN($id,'.indexed'));
10024c3e0d2SAndreas Gohr                if($last > @filemtime(wikiFN($id))) return;
10124c3e0d2SAndreas Gohr            }
10266b95544STom N Harris        }
10366b95544STom N Harris    }
10424c3e0d2SAndreas Gohr
10524c3e0d2SAndreas Gohr    _lock();
106981f048aSGabriel Birke    _quietecho("$id... ");
10724c3e0d2SAndreas Gohr    idx_addPage($id);
10866b95544STom N Harris    io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
109981f048aSGabriel Birke    _quietecho("done.\n");
11024c3e0d2SAndreas Gohr    _unlock();
11124c3e0d2SAndreas Gohr}
11224c3e0d2SAndreas Gohr
11324c3e0d2SAndreas Gohr/**
11424c3e0d2SAndreas Gohr * lock the indexer system
11524c3e0d2SAndreas Gohr */
11624c3e0d2SAndreas Gohrfunction _lock(){
11724c3e0d2SAndreas Gohr    global $conf;
11824c3e0d2SAndreas Gohr    $lock = $conf['lockdir'].'/_indexer.lock';
11924c3e0d2SAndreas Gohr    $said = false;
12044881d27STroels Liebe Bentsen    while(!@mkdir($lock, $conf['dmode'])){
12124c3e0d2SAndreas Gohr        if(time()-@filemtime($lock) > 60*5){
12224c3e0d2SAndreas Gohr            // looks like a stale lock - remove it
12324c3e0d2SAndreas Gohr            @rmdir($lock);
12424c3e0d2SAndreas Gohr        }else{
12524c3e0d2SAndreas Gohr            if($said){
126981f048aSGabriel Birke                _quietecho(".");
12724c3e0d2SAndreas Gohr            }else{
128981f048aSGabriel Birke                _quietecho("Waiting for lockfile (max. 5 min)");
12924c3e0d2SAndreas Gohr                $said = true;
13024c3e0d2SAndreas Gohr            }
13124c3e0d2SAndreas Gohr            sleep(15);
13224c3e0d2SAndreas Gohr        }
13324c3e0d2SAndreas Gohr    }
1341ca31cfeSAndreas Gohr    if($conf['dperm']) chmod($lock, $conf['dperm']);
135981f048aSGabriel Birke    if($said) _quietecho("\n");
13624c3e0d2SAndreas Gohr}
13724c3e0d2SAndreas Gohr
13824c3e0d2SAndreas Gohr/**
13924c3e0d2SAndreas Gohr * unlock the indexer sytem
14024c3e0d2SAndreas Gohr */
14124c3e0d2SAndreas Gohrfunction _unlock(){
14224c3e0d2SAndreas Gohr    global $conf;
14324c3e0d2SAndreas Gohr    $lock = $conf['lockdir'].'/_indexer.lock';
14424c3e0d2SAndreas Gohr    @rmdir($lock);
14524c3e0d2SAndreas Gohr}
14624c3e0d2SAndreas Gohr
14724c3e0d2SAndreas Gohr/**
14824c3e0d2SAndreas Gohr * Clear all index files
14924c3e0d2SAndreas Gohr */
15024c3e0d2SAndreas Gohrfunction _clearindex(){
15124c3e0d2SAndreas Gohr    global $conf;
15224c3e0d2SAndreas Gohr    _lock();
153981f048aSGabriel Birke    _quietecho("Clearing index... ");
15466b95544STom N Harris    io_saveFile($conf['indexdir'].'/page.idx','');
155*a0070b52SAdrian Lang    io_saveFile($conf['indexdir'].'/title.idx','');
15666b95544STom N Harris    $dir = @opendir($conf['indexdir']);
15766b95544STom N Harris    if($dir!==false){
15866b95544STom N Harris        while(($f = readdir($dir)) !== false){
15966b95544STom N Harris            if(substr($f,-4)=='.idx' &&
16066b95544STom N Harris               (substr($f,0,1)=='i' || substr($f,0,1)=='w'))
16166b95544STom N Harris                @unlink($conf['indexdir']."/$f");
16266b95544STom N Harris        }
16366b95544STom N Harris    }
164981f048aSGabriel Birke    _quietecho("done.\n");
16524c3e0d2SAndreas Gohr    _unlock();
16624c3e0d2SAndreas Gohr}
16724c3e0d2SAndreas Gohr
168981f048aSGabriel Birkefunction _quietecho($msg) {
169981f048aSGabriel Birke    global $QUIET;
170981f048aSGabriel Birke    if(!$QUIET) echo $msg;
171981f048aSGabriel Birke}
172981f048aSGabriel Birke
17324c3e0d2SAndreas Gohr//Setup VIM: ex: et ts=2 enc=utf-8 :
174