xref: /plugin/searchindex/ajax.php (revision 4f66af325bcca23ae97b9e2a1fc000b62e788019)
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 1;
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
105    if(!$_POST['page']){
106        print 1;
107        exit;
108    }
109
110    // keep running
111    @ignore_user_abort(true);
112
113    // try to aquire a lock (newer releases do the locking in idx_addPage)
114    if(INDEXER_VERSION < 4){
115        $lock = $conf['lockdir'].'/_indexer.lock';
116        while(!@mkdir($lock)){
117            if(time()-@filemtime($lock) > 60*5){
118                // looks like a stale lock - remove it
119                @rmdir($lock);
120            }else{
121                print 'indexer is locked.';
122                exit;
123            }
124        }
125    }
126
127    // do the work
128    idx_addPage($_POST['page'], false, true);
129
130    // we're finished
131    if(INDEXER_VERSION < 4){
132        io_saveFile(metaFN($id,'.indexed'),'');
133        @rmdir($lock);
134    }
135
136    print 1;
137}
138
139