xref: /plugin/searchindex/ajax.php (revision 7befb32abe3d4f5cb157e5c9b5c7ded3ad839a78)
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    // acquire 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    io_saveFile($conf['indexdir'].'/page.idx','');
76    io_saveFile($conf['indexdir'].'/title.idx','');
77    $dir = @opendir($conf['indexdir']);
78    if($dir!==false){
79        while(($f = readdir($dir)) !== false){
80            if(substr($f,-4)=='.idx' &&
81               (substr($f,0,1)=='i' || substr($f,0,1)=='w'))
82                @unlink($conf['indexdir']."/$f");
83        }
84    }
85
86    // we're finished
87    @rmdir($lock);
88
89    print 1;
90}
91
92/**
93 * Index the given page
94 *
95 * We're doing basicly the same as the real indexer but ignore the
96 * last index time here
97 */
98function ajax_indexpage(){
99    global $conf;
100
101    if(!$_POST['page']){
102        print 1;
103        exit;
104    }
105
106    // keep running
107    @ignore_user_abort(true);
108
109    // try to aquire a lock (newer releases do the locking in idx_addPage)
110    if(INDEXER_VERSION < 4){
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'], false, true);
125
126    // we're finished
127    if(INDEXER_VERSION < 4){
128        io_saveFile(metaFN($id,'.indexed'),'');
129        @rmdir($lock);
130    }
131
132    print 1;
133}
134
135