1<?php
2/**
3 * DokuWiki Plugin searchresultswithpath (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Sam B <sam@s-blu.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_searchresultswithpath extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22       $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'handle_search_query_pagelookup');
23
24    }
25
26    /**
27     * [Custom event handler which performs action]
28     *
29     * @param Doku_Event $event  event object by reference
30     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
31     *                           handler was registered]
32     * @return void
33     */
34
35    public function handle_search_query_pagelookup(Doku_Event &$event) {
36        $useNsHeading = $this->getConf('use_ns_heading');
37        $showPathOnlyAsHeading = $this->getConf('show_path_only_as_heading');
38
39        foreach($event->result as $id => $title){
40
41            if (!$useNsHeading) {
42                $ns = getNS($id);
43            } else {
44                //Get Name of the NS Page or NS:start Page, if possible
45                $ns = p_get_first_heading(getNS($id));
46                if(!$ns) $ns = p_get_first_heading(getNS($id). ':start');
47
48                if (!$ns && !$showPathOnlyAsHeading) {
49                    $ns = getNS($id);
50                }
51            }
52
53            if($ns) $ns = ' ['.$ns.']';
54            /*
55             * Fix #1: Add the pageId to the title, if the page has no heading
56             * Normally this get handled by Dokuwiki itself, but we always set an title here and
57             * bypass Dokuwikis handling
58             */
59            if (!$title) $title = noNs($id);
60            $event->result[$id] = $title . $ns;
61        }
62    }
63}
64
65// vim:ts=4:sw=4:et:
66