xref: /plugin/searchindex/ajax.php (revision dc6fce527b1d765982edfbc6e7d936d0bd88397c)
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    //clear all index files
77    io_saveFile($conf['cachedir'].'/word.idx','');
78    io_saveFile($conf['cachedir'].'/page.idx','');
79    io_saveFile($conf['cachedir'].'/index.idx','');
80
81    // we're finished
82    @rmdir($lock);
83
84    print 1;
85}
86
87/**
88 * Index the given page
89 *
90 * We're doing basicly the same as the real indexer but ignore the
91 * last index time here
92 */
93function ajax_indexpage(){
94    global $conf;
95
96    if(!$_POST['page']){
97        print 1;
98        exit;
99    }
100
101    // keep running
102    @ignore_user_abort(true);
103
104    // try to aquire a lock
105    $lock = $conf['lockdir'].'/_indexer.lock';
106    while(!@mkdir($lock)){
107        if(time()-@filemtime($lock) > 60*5){
108            // looks like a stale lock - remove it
109            @rmdir($lock);
110        }else{
111            print 'indexer is locked.';
112            exit;
113        }
114
115    }
116
117    // do the work
118    idx_addPage($_POST['page']);
119
120    // we're finished
121    io_saveFile(metaFN($id,'.indexed'),'');
122    @rmdir($lock);
123
124    print 1;
125}
126
127//Setup VIM: ex: et ts=4 enc=utf-8 :
128?>
129