1<?php
2if(!defined('DOKU_INC')) die();
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once(DOKU_PLUGIN.'action.php');
5
6class action_plugin_autosearch extends DokuWiki_Action_Plugin {
7  function getInfo(){
8        return array(
9            'author' => 'Ahmet Sacan',
10            'email'  => 'ahmet@ceng.metu.edu.tr',
11            'date'   => '2007-02-07',
12            'name'   => 'Autosearch plugin',
13            'desc'   => 'displays search results when a page does not exist',
14            'url'    => 'http://www.ceng.metu.edu.tr/~ahmet',
15        );
16  }
17
18  function register(&$contr){
19    $contr->register_hook('ACTION_ACT_PREPROCESS',
20                          'BEFORE',
21                          $this,
22                          'handle_act',
23                           array());
24  }
25  function handle_act(&$e, $param){
26    global $ID;
27    if($e->data != 'show' || strpos($ID,'talk:')===0) return;
28
29    if($_SERVER['REMOTE_USER'])
30            $perm = auth_quickaclcheck($ID);
31    else
32            $perm = auth_aclcheck($ID,'',null);
33
34    if(!is_file(wikiFN($ID))){
35      $index_pages = explode(',',$this->getConf('index_pages'));
36      foreach($index_pages as $index){
37        if($index == '@subpage_with_same_name')
38          $index = noNS($ID);
39        if(is_file(wikiFN("$ID:$index"))){
40            header("Location: ".wl("$ID:$index", $perm < AUTH_CREATE ? "" : "auto_search_indexed=1"));
41            die();
42        }
43      }
44      if($this->getConf('pagelist') && is_dir(dirname(wikiFN("$ID:dummy"))) && $indexmenu =& plugin_load('syntax', 'indexmenu')){
45        $handled = $indexmenu->handle("{{indexmenu>$ID#2|js#default}}",null,null,$this);
46        $dummy_renderer = (object) array('doc'=>'');
47        $indexmenu->render('xhtml',$dummy_renderer, $handled);
48        msg("Here are the subpages of [ $ID ]:<br>".$dummy_renderer->doc);
49
50      }
51      if($this->getConf('search'))
52        $e->data = 'search';
53    }
54    elseif($_GET['auto_search_indexed']){
55      $id = getNS($ID);
56      msg("<a href='".wl(getNS($ID), "do=edit")."'>Click here</a> if you wanted to create the [ $id ] page.");
57    }
58  }
59}
60