xref: /plugin/searchindex/ajax.php (revision ce225b9d73a3938338382ba6f34199d74169313a)
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    io_saveFile($conf['indexdir'].'/pageword.idx','');
78    io_saveFile($conf['indexdir'].'/metadata.idx','');
79    $dir = @opendir($conf['indexdir']);
80    if ($dir!==false) {
81        while (($f = readdir($dir)) !== false) {
82            if (substr($f,-4)=='.idx' &&
83               (substr($f,0,1)=='i' || substr($f,0,1)=='w'
84               || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx'))
85                @unlink($conf['indexdir']."/$f");
86        }
87    }
88    @unlink($conf['indexdir'].'/lengths.idx');
89
90    // we're finished
91    @rmdir($lock);
92
93    print 'true';
94}
95
96/**
97 * Index the given page
98 *
99 * We're doing basicly the same as the real indexer but ignore the
100 * last index time here
101 */
102function ajax_indexpage() {
103    global $conf;
104    $force = false;
105
106    if (!$_POST['page']) {
107        print 'true';
108        exit;
109    }
110    if (isset($_POST['force'])) {
111        $force = $_POST['force'] == 'true';
112    }
113
114    // keep running
115    @ignore_user_abort(true);
116
117    // try to aquire a lock (newer releases do the locking in idx_addPage)
118    if (INDEXER_VERSION < 4){
119        $lock = $conf['lockdir'].'/_indexer.lock';
120        while (!@mkdir($lock)){
121            if (time()-@filemtime($lock) > 60*5) {
122                // looks like a stale lock - remove it
123                @rmdir($lock);
124            } else {
125                print 'indexer is locked.';
126                exit;
127            }
128        }
129    }
130
131    // do the work
132    $success = idx_addPage($_POST['page'], false, $force);
133
134    // we're finished
135    if (INDEXER_VERSION < 4){
136        io_saveFile(metaFN($id,'.indexed'),'');
137        @rmdir($lock);
138    }
139
140    print ($success !== false) ? 'true' : '';
141}
142
143