1<?php
2/**
3 * DokuWiki Plugin datasearchform (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6
7 * @author  Hans-Juergen Schuemmer
8 * adapted from the original plugin "Searchform"
9 * @author  Gerrit Uitslag <klapinklapin@gmail.com>
10 */
11
12// must be run within Dokuwiki
13if(!defined('DOKU_INC')) die();
14
15/**
16 * Class action_plugin_datasearchform
17 */
18class action_plugin_datasearchform extends DokuWiki_Action_Plugin {
19
20    /**
21     * Registers a callback function for a given event
22     *
23     * @param Doku_Event_Handler $controller DokuWiki's event controller object
24     * @return void
25     */
26    public function register(Doku_Event_Handler $controller) {
27
28        $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'BEFORE', $this, '_search_query_fullpage');
29        $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'BEFORE', $this, '_search_query_pagelookup');
30
31    }
32
33    /**
34     * Restrict fullpage search to namespace given as url parameter
35     *
36     * @param Doku_Event $event  event object by reference
37     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
38     *                           handler was registered]
39     * @return void
40     */
41    public function _search_query_fullpage(Doku_Event &$event, $param) {
42        $this->_addNamespace2query($event->data['query']);
43    }
44
45    /**
46     * Restrict page lookup search to namespace given as url parameter
47     *
48     * @param Doku_Event $event  event object by reference
49     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
50     *                           handler was registered]
51     * @return void
52     */
53    public function _search_query_pagelookup(Doku_Event &$event, $param) {
54        $this->_addNamespace2query($event->data['id']);
55    }
56
57    /**
58     * Extend query string with namespace, if it doesn't contain a namespace expression
59     *
60     * @param string &$query (reference) search query string
61     */
62    private function _addNamespace2query(&$query) {
63        global $INPUT;
64
65        $ns = cleanID($INPUT->str('ns'));
66        if($ns) {
67            //add namespace if user hasn't already provide one
68            if(!preg_match('/(?:^| )(?:@|ns:)[\w:]+/u', $query, $matches)) {
69                $query .= ' @' . $ns;
70            }
71        }
72    }
73
74}
75
76// vim:ts=4:sw=4:et: