xref: /plugin/searchindex/ajax.php (revision 2394c709c52e6ee630c627c10eab77eb9f6d99e7)
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');
16
17//close sesseion
18session_write_close();
19
20header('Content-Type: text/plain; charset=utf-8');
21
22//we only work for admins!
23if (auth_quickaclcheck($conf['start']) < AUTH_ADMIN) {
24    die('access denied');
25}
26
27//call the requested function
28$call = 'ajax_'.$_POST['call'];
29if (function_exists($call)) {
30    $call();
31} else {
32    print "The called function '".htmlspecialchars($call)."' does not exist!";
33}
34
35/**
36 * Searches for pages
37 *
38 * @author Andreas Gohr <andi@splitbrain.org>
39 */
40function ajax_pagelist()
41{
42    global $conf;
43    $data = array();
44    search($data,$conf['datadir'], 'search_allpages', array());
45
46    foreach ($data as $val) {
47        print $val['id']."\n";
48    }
49}
50
51/**
52 * Clear all index files
53 */
54function ajax_clearindex()
55{
56    global $conf;
57    // keep running
58    @ignore_user_abort(true);
59
60    // acquire a lock
61    $lock = $conf['lockdir'] . '/_indexer.lock';
62    while (!@mkdir($lock)) {
63        if (time()-@filemtime($lock) > 60*5) {
64            // looks like a stale lock - remove it
65            @rmdir($lock);
66        } else {
67            print 'indexer is locked.';
68            exit;
69        }
70    }
71
72    io_saveFile($conf['indexdir'].'/page.idx','');
73    io_saveFile($conf['indexdir'].'/title.idx','');
74    io_saveFile($conf['indexdir'].'/pageword.idx','');
75    io_saveFile($conf['indexdir'].'/metadata.idx','');
76    $dir = @opendir($conf['indexdir']);
77    if ($dir!==false) {
78        while (($f = readdir($dir)) !== false) {
79            if (substr($f,-4)=='.idx' &&
80               (substr($f,0,1)=='i' || substr($f,0,1)=='w'
81               || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx'))
82                @unlink($conf['indexdir']."/$f");
83        }
84    }
85    @unlink($conf['indexdir'].'/lengths.idx');
86
87    // we're finished
88    @rmdir($lock);
89
90    print 'true';
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{
101    global $conf;
102    $force = false;
103
104    if (!$_POST['page']) {
105        print 0;
106        exit;
107    }
108    if (isset($_POST['force'])) {
109        $force = $_POST['force'] == 'true';
110    }
111
112    // keep running
113    @ignore_user_abort(true);
114
115    // try to aquire a lock (newer releases do the locking in idx_addPage)
116    if (INDEXER_VERSION < 4) {
117        $lock = $conf['lockdir'].'/_indexer.lock';
118        while (!@mkdir($lock)) {
119            if (time()-@filemtime($lock) > 60*5) {
120                // looks like a stale lock - remove it
121                @rmdir($lock);
122            } else {
123                print 'indexer is locked.';
124                exit;
125            }
126        }
127    }
128
129    // do the work
130    $success = idx_addPage($_POST['page'], false, $force);
131
132    // we're finished
133    if (INDEXER_VERSION < 4) {
134        io_saveFile(metaFN($id,'.indexed'),'');
135        @rmdir($lock);
136    }
137
138    print ($success !== false) ? 'true' : '';
139}
140
141