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
9use dokuwiki\Search\Indexer;
10
11//fix for Opera XMLHttpRequests
12if (!count($_POST) && $HTTP_RAW_POST_DATA) {
13    parse_str($HTTP_RAW_POST_DATA, $_POST);
14}
15
16if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../../../').'/');
17require_once(DOKU_INC.'inc/init.php');
18
19//close session
20session_write_close();
21
22header('Content-Type: text/plain; charset=utf-8');
23
24//we only work for admins!
25if (auth_quickaclcheck($conf['start']) < AUTH_ADMIN) {
26    die('access denied');
27}
28
29//call the requested function
30$call = 'ajax_'.$_POST['call'];
31if (function_exists($call)) {
32    $call();
33} else {
34    print "The called function '".htmlspecialchars($call)."' does not exist!";
35}
36
37/**
38 * Searches for pages
39 *
40 * @author Andreas Gohr <andi@splitbrain.org>
41 */
42function ajax_pagelist()
43{
44    global $conf;
45    $data = array();
46    search($data, $conf['datadir'], 'search_allpages', array());
47
48    foreach ($data as $val) {
49        print $val['id']."\n";
50    }
51}
52
53/**
54 * Clear all index files
55 */
56function ajax_clearindex()
57{
58    global $conf;
59    // keep running
60    @ignore_user_abort(true);
61
62    if (is_callable('dokuwiki\Search\Indexer::getInstance')) {
63        $Indexer = Indexer::getInstance();
64    } elseif (class_exists('Doku_Indexer')) {
65        $Indexer = idx_get_indexer();
66    } else {
67       // Failed to clear index. Your DokuWiki is older than release 2011-05-25 "Rincewind"
68       exit;
69    }
70
71    if (is_callable([$Indexer, 'clear'])) {
72        $success = $Indexer->clear();
73    } else {
74       // Failed to clear index. Your DokuWiki is older than release 2013-05-10 "Weatherwax"
75        $success = false;
76    }
77
78    print ($success !== false) ? 'true' : '';
79}
80
81/**
82 * Index the given page
83 *
84 * We're doing basicly the same as the real indexer but ignore the
85 * last index time here
86 */
87function ajax_indexpage()
88{
89    global $conf;
90    $force = false;
91
92    if (!$_POST['page']) {
93        print 0;
94        exit;
95    }
96    if (isset($_POST['force'])) {
97        $force = $_POST['force'] == 'true';
98    }
99
100    // keep running
101    @ignore_user_abort(true);
102
103    if (is_callable('dokuwiki\Search\Indexer::getInstance')) {
104        $Indexer = Indexer::getInstance();
105        $success = $Indexer->addPage($_POST['page'], false, $force);
106    } elseif (class_exists('Doku_Indexer')) {
107        $success = idx_addPage($_POST['page'], false, $force);
108    } else {
109       // Failed to index the page. Your DokuWiki is older than release 2011-05-25 "Rincewind"
110       exit;
111    }
112
113    print ($success !== false) ? 'true' : '';
114}
115
116