xref: /plugin/searchindex/ajax.php (revision 072f05c632a4eb7899af9c9168e50f62760ef503)
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__).'/../../../').'/');
15require_once(DOKU_INC.'inc/init.php');
16require_once(DOKU_INC.'inc/common.php');
17require_once(DOKU_INC.'inc/pageutils.php');
18require_once(DOKU_INC.'inc/auth.php');
19require_once(DOKU_INC.'inc/search.php');
20require_once(DOKU_INC.'inc/indexer.php');
21
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{
47    global $conf;
48    $data = array();
49    search($data,$conf['datadir'], 'search_allpages', array());
50
51    foreach ($data as $val) {
52        print $val['id']."\n";
53    }
54}
55
56/**
57 * Clear all index files
58 */
59function ajax_clearindex()
60{
61    global $conf;
62    // keep running
63    @ignore_user_abort(true);
64
65    // acquire a lock
66    $lock = $conf['lockdir'] . '/_indexer.lock';
67    while (!@mkdir($lock)) {
68        if (time()-@filemtime($lock) > 60*5) {
69            // looks like a stale lock - remove it
70            @rmdir($lock);
71        } else {
72            print 'indexer is locked.';
73            exit;
74        }
75    }
76
77    io_saveFile($conf['indexdir'].'/page.idx','');
78    io_saveFile($conf['indexdir'].'/title.idx','');
79    io_saveFile($conf['indexdir'].'/pageword.idx','');
80    io_saveFile($conf['indexdir'].'/metadata.idx','');
81    $dir = @opendir($conf['indexdir']);
82    if ($dir!==false) {
83        while (($f = readdir($dir)) !== false) {
84            if (substr($f,-4)=='.idx' &&
85               (substr($f,0,1)=='i' || substr($f,0,1)=='w'
86               || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx'))
87                @unlink($conf['indexdir']."/$f");
88        }
89    }
90    @unlink($conf['indexdir'].'/lengths.idx');
91
92    // we're finished
93    @rmdir($lock);
94
95    print 'true';
96}
97
98/**
99 * Index the given page
100 *
101 * We're doing basicly the same as the real indexer but ignore the
102 * last index time here
103 */
104function ajax_indexpage()
105{
106    global $conf;
107    $force = false;
108
109    if (!$_POST['page']) {
110        print 0;
111        exit;
112    }
113    if (isset($_POST['force'])) {
114        $force = $_POST['force'] == 'true';
115    }
116
117    // keep running
118    @ignore_user_abort(true);
119
120    // try to aquire a lock (newer releases do the locking in idx_addPage)
121    if (INDEXER_VERSION < 4) {
122        $lock = $conf['lockdir'].'/_indexer.lock';
123        while (!@mkdir($lock)) {
124            if (time()-@filemtime($lock) > 60*5) {
125                // looks like a stale lock - remove it
126                @rmdir($lock);
127            } else {
128                print 'indexer is locked.';
129                exit;
130            }
131        }
132    }
133
134    // do the work
135    $success = idx_addPage($_POST['page'], false, $force);
136
137    // we're finished
138    if (INDEXER_VERSION < 4) {
139        io_saveFile(metaFN($id,'.indexed'),'');
140        @rmdir($lock);
141    }
142
143    print ($success !== false) ? 'true' : '';
144}
145
146