xref: /plugin/sphinxsearch-was/action.php (revision 0:c723787235e2)
1<?php
2/**
3 * Script to search in uploaded pdf documents
4 *
5 * @author Dominik Eckelmann <eckelmann@cosmocode.de>
6 */
7
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10
11require_once(DOKU_PLUGIN . 'action.php');
12require_once(DOKU_PLUGIN . 'sphinxsearch/sphinxapi.php');
13require_once(DOKU_PLUGIN . 'sphinxsearch/PageMapper.php');
14require_once(DOKU_PLUGIN . 'sphinxsearch/SphinxSearch.php');
15
16
17class action_plugin_sphinxsearch extends DokuWiki_Action_Plugin {
18
19    /**
20	* return some info
21	*/
22    function getInfo() {
23        return confToHash(dirname(__FILE__).'/plugin.info.txt');
24	}
25
26    /**
27	* Register to the content display event to place the results under it.
28	*/
29    /**
30     * register the eventhandlers
31     */
32    function register(&$controller){
33        $controller->register_hook('ACTION_ACT_PREPROCESS',
34                                   'BEFORE',
35                                   $this,
36                                   'handle_act_preprocess',
37                                   array());
38
39        $controller->register_hook('TPL_ACT_UNKNOWN',
40                                   'BEFORE',
41                                   $this,
42                                   'handle_act_unknown',
43                                   array());
44    }
45
46    /**
47     * Checks if 'googlesearch' was given as action, if so we
48     * do handle the event our self and no further checking takes place
49     */
50    function handle_act_preprocess(&$event, $param){
51        if($event->data != 'sphinxsearch') return; // nothing to do for us
52
53        $event->stopPropagation(); // this is our very own action, no need to check other plugins
54        $event->preventDefault();  // we handle it our self, thanks
55    }
56
57    /**
58     * If our own 'googlesearch' action was given we produce our content here
59     */
60    function handle_act_unknown(&$event, $param){
61        if($event->data != 'sphinxsearch') return; // nothing to do for us
62
63        // we can handle it -> prevent others
64        $event->stopPropagation();
65        $event->preventDefault();
66
67        global $QUERY;
68        $this->_search($QUERY,$_REQUEST['start']);
69    }
70
71    /**
72     * do the search and displays the result
73     */
74    function _search($query, $start) {
75
76        $start = (int) $start;
77        if($start < 0) $start = 0;
78
79	// backup the config array
80	$cp = $conf;
81
82        $search = new SphinxSearch($this->getConf('host'), $this->getConf('port'), $this->getConf('index'));
83        $pagesList = $search->search($query, $start, $this->getConf('maxresults'));
84        $totalFound = $search->getTotalFound();
85
86        echo '<h2>Found '.$totalFound .' documents for query "' . hsc($query).'"</h2>';
87	echo '<div class="search_result">';
88        // printout the results
89	foreach ($pagesList as $id => $excerpt) {
90            $metaData = p_get_metadata($id);
91            echo '<a href="/doku.php/'.$id.'" title="" class="wikilink1">'.hsc($id).' ('.hsc($metaData['title']).')'.'</a><br/>';
92            echo '<span class="search_cnt">Last modified:'.date("Y-m-d H:i",$metaData['date']['modified']).'</span>';
93            echo '<div class="search_snippet">';
94            echo $excerpt;
95            echo '</div>';
96            echo '<br />';
97	}
98        echo '</div>';
99        echo '<div class="sphinxsearch_nav">';
100        if ($start > 1){
101            $prev = $start - $this->getConf('maxresults');
102            if($prev < 0) $prev = 0;
103
104            echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$prev),'false','&'),
105                                      'prev','wikilink1 gs_prev',$conf['target']['interwiki']);
106        }
107        echo ' ';
108        if($start + $this->getConf('maxresults') < $totalFound){
109            $next = $start + $this->getConf('maxresults');
110
111            echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$next),'false','&'),
112                                      'next','wikilink1 gs_next',$conf['target']['interwiki']);
113        }
114        echo '</div>';
115
116    }
117
118     function searchform(){
119          global $lang;
120          global $ACT;
121          global $QUERY;
122
123          // don't print the search form if search action has been disabled
124          if (!actionOk('sphinxsearch')) return false;
125
126          print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
127          print '<input type="hidden" name="do" value="sphinxsearch" />';
128          print '<input type="text" ';
129          if($ACT == 'sphinxsearch') print 'value="'.htmlspecialchars($QUERY).'" ';
130          print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
131          print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
132          print '</div></form>';
133          return true;
134    }
135}
136
137?>
138