1<?php
2/**
3 * Script to search in uploaded pdf documents
4 *
5 * @author Dominik Eckelmann <eckelmann@cosmocode.de>
6 * @author @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 */
8
9if(!defined('DOKU_INC')) die();
10
11class action_plugin_docsearch_search extends DokuWiki_Action_Plugin {
12
13    private $backupConfig;
14
15    function register(Doku_Event_Handler $controller) {
16        $controller->register_hook('TPL_CONTENT_DISPLAY', 'AFTER', $this, 'display', array());
17    }
18
19    function display(Doku_Event &$event, $param) {
20        global $ACT;
21        global $conf;
22        global $QUERY;
23        global $lang;
24
25        // only work with search
26        if($ACT !== 'search') return;
27
28        // backup the config array
29        $this->backupConfig = $conf;
30
31        // change index/pages folder for DocSearch
32        $conf['indexdir'] = $conf['savedir'] . '/docsearch/index';
33        $conf['datadir'] = $conf['savedir'] . '/docsearch/pages';
34
35        $data = ft_pageSearch($QUERY, $regex);
36
37        if(empty($data)) {
38            $conf = $this->backupConfig;
39            return;
40        }
41
42        $searchResults = array();
43        $runs = 0;
44        foreach($data as $id => $hits) {
45            $searchResults[$id] = array();
46            $searchResults[$id]['hits'] = $hits;
47            if($runs++ < $this->getConf('showSnippets')) {
48                $searchResults[$id]['snippet'] = ft_snippet($id, $regex);
49            }
50        }
51
52        $conf = $this->backupConfig;
53
54        echo '<h2>' . hsc($this->getLang('title')) . '</h2>';
55        echo '<div class="search_result">';
56
57        $num = 0;
58        foreach($searchResults as $id => $data) {
59            if($this->getConf('showUsage') !== 0) {
60                $usages = ft_mediause($id, $this->getConf('showUsage'));
61            } else {
62                $usages = array();
63            }
64
65            echo '<a href="' . ml($id) . '" title="" class="wikilink1">' . hsc($id) . '</a>: ';
66            echo '<span class="search_cnt">' . hsc($data['hits']) . ' ' . hsc($lang['hits']) . '</span>';
67            if(!empty($usages)) {
68                echo '<span class="usage">';
69                echo ', ' . hsc($this->getLang('usage')) . ' ';
70                foreach($usages as $usage) {
71                    echo html_wikilink($usage);
72                }
73                echo '</span>';
74            }
75
76            if(isset($data['snippet'])) {
77                echo '<div class="search_snippet">';
78                echo $data['snippet'];
79                echo '</div>';
80            }
81
82            echo '<br />';
83            $num++;
84        }
85
86        echo '</div>';
87    }
88}
89