Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 141 lines (122 sloc) 3.622 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php
/**
* AJAX call handler for searchindex plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/

//fix for Opera XMLHttpRequests
if(!count($_POST) && $HTTP_RAW_POST_DATA){
  parse_str($HTTP_RAW_POST_DATA, $_POST);
}

if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC.'inc/common.php');
require_once(DOKU_INC.'inc/pageutils.php');
require_once(DOKU_INC.'inc/auth.php');
require_once(DOKU_INC.'inc/search.php');
require_once(DOKU_INC.'inc/indexer.php');
//close sesseion
session_write_close();

header('Content-Type: text/plain; charset=utf-8');

//we only work for admins!
if (auth_quickaclcheck($conf['start']) < AUTH_ADMIN){
    die('access denied');
}

//call the requested function
$call = 'ajax_'.$_POST['call'];
if (function_exists($call)) {
    $call();
} else {
    print "The called function '".htmlspecialchars($call)."' does not exist!";
}

/**
* Searches for pages
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function ajax_pagelist(){
    global $conf;
    $data = array();
    search($data,$conf['datadir'], 'search_allpages', array());

    foreach($data as $val) {
        print $val['id']."\n";
    }
}

/**
* Clear all index files
*/
function ajax_clearindex() {
    global $conf;
    // keep running
    @ignore_user_abort(true);

    // acquire a lock
    $lock = $conf['lockdir'] . '/_indexer.lock';
    while (!@mkdir($lock)){
        if (time()-@filemtime($lock) > 60*5) {
            // looks like a stale lock - remove it
            @rmdir($lock);
        } else {
            print 'indexer is locked.';
            exit;
        }
    }

    io_saveFile($conf['indexdir'].'/page.idx','');
    io_saveFile($conf['indexdir'].'/title.idx','');
    io_saveFile($conf['indexdir'].'/pageword.idx','');
    io_saveFile($conf['indexdir'].'/metadata.idx','');
    $dir = @opendir($conf['indexdir']);
    if ($dir!==false) {
        while (($f = readdir($dir)) !== false) {
            if (substr($f,-4)=='.idx' &&
               (substr($f,0,1)=='i' || substr($f,0,1)=='w'
               || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx'))
                @unlink($conf['indexdir']."/$f");
        }
    }
    @unlink($conf['indexdir'].'/lengths.idx');

    // we're finished
    @rmdir($lock);

    print 'true';
}

/**
* Index the given page
*
* We're doing basicly the same as the real indexer but ignore the
* last index time here
*/
function ajax_indexpage() {
    global $conf;
    $force = false;

    if (!$_POST['page']) {
        print 0;
        exit;
    }
    if (isset($_POST['force'])) {
        $force = $_POST['force'] == 'true';
    }

    // keep running
    @ignore_user_abort(true);

    // try to aquire a lock (newer releases do the locking in idx_addPage)
    if (INDEXER_VERSION < 4){
        $lock = $conf['lockdir'].'/_indexer.lock';
        while (!@mkdir($lock)){
            if (time()-@filemtime($lock) > 60*5) {
                // looks like a stale lock - remove it
                @rmdir($lock);
            } else {
                print 'indexer is locked.';
                exit;
            }
        }
    }

    // do the work
    $success = idx_addPage($_POST['page'], false, $force);

    // we're finished
    if (INDEXER_VERSION < 4){
        io_saveFile(metaFN($id,'.indexed'),'');
        @rmdir($lock);
    }

    print ($success !== false) ? 'true' : '';
}
Something went wrong with that request. Please try again.