xref: /plugin/searchindex/ajax.php (revision 96a81f1d5d91db188deba6051cdca88c65896b8b)
1<?php
2/**
3 * AJAX call handler for searchindex plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9//fix for Opera XMLHttpRequests
10if(!count($_POST) && $HTTP_RAW_POST_DATA){
11  parse_str($HTTP_RAW_POST_DATA, $_POST);
12}
13
14if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16require_once(DOKU_INC.'inc/init.php');
17require_once(DOKU_INC.'inc/common.php');
18require_once(DOKU_INC.'inc/pageutils.php');
19require_once(DOKU_INC.'inc/auth.php');
20require_once(DOKU_INC.'inc/search.php');
21require_once(DOKU_INC.'inc/indexer.php');
22//close sesseion
23session_write_close();
24
25header('Content-Type: text/plain; charset=utf-8');
26
27//we only work for admins!
28if (auth_quickaclcheck($conf['start']) < AUTH_ADMIN){
29    die('access denied');
30}
31
32//call the requested function
33$call = 'ajax_'.$_POST['call'];
34if(function_exists($call)){
35    $call();
36}else{
37    print "The called function '".htmlspecialchars($call)."' does not exist!";
38}
39
40/**
41 * Searches for pages
42 *
43 * @author Andreas Gohr <andi@splitbrain.org>
44 */
45function ajax_pagelist(){
46    global $conf;
47    $data = array();
48    search($data,$conf['datadir'],'search_allpages',array());
49
50    foreach($data as $val){
51        print $val['id']."\n";
52    }
53}
54
55/**
56 * Clear all index files
57 */
58function ajax_clearindex(){
59    global $conf;
60    // keep running
61    @ignore_user_abort(true);
62
63    // try to aquire a lock
64    $lock = $conf['lockdir'].'/_indexer.lock';
65    while(!@mkdir($lock)){
66        if(time()-@filemtime($lock) > 60*5){
67            // looks like a stale lock - remove it
68            @rmdir($lock);
69        }else{
70            print 'indexer is locked.';
71            exit;
72        }
73
74    }
75
76    io_saveFile($conf['indexdir'].'/page.idx','');
77    io_saveFile($conf['indexdir'].'/title.idx','');
78    $dir = @opendir($conf['indexdir']);
79    if($dir!==false){
80        while(($f = readdir($dir)) !== false){
81            if(substr($f,-4)=='.idx' &&
82               (substr($f,0,1)=='i' || substr($f,0,1)=='w'))
83                @unlink($conf['indexdir']."/$f");
84        }
85    }
86
87    // we're finished
88    @rmdir($lock);
89
90    print 1;
91}
92
93/**
94 * Index the given page
95 *
96 * We're doing basicly the same as the real indexer but ignore the
97 * last index time here
98 */
99function ajax_indexpage(){
100    global $conf;
101
102    if(!$_POST['page']){
103        print 1;
104        exit;
105    }
106
107    // keep running
108    @ignore_user_abort(true);
109
110    // try to aquire a lock
111    $lock = $conf['lockdir'].'/_indexer.lock';
112    while(!@mkdir($lock)){
113        if(time()-@filemtime($lock) > 60*5){
114            // looks like a stale lock - remove it
115            @rmdir($lock);
116        }else{
117            print 'indexer is locked.';
118            exit;
119        }
120
121    }
122
123    // do the work
124    idx_addPage($_POST['page']);
125
126    // we're finished
127    io_saveFile(metaFN($id,'.indexed'),'');
128    @rmdir($lock);
129
130    print 1;
131}
132
133//Setup VIM: ex: et ts=4 enc=utf-8 :
134