xref: /plugin/sphinxsearch-was/action.php (revision 4:c8a70a2936eb)
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
85        $totalFound = $search->getTotalFound();
86
87        echo '<h2>Found '.$totalFound .' documents for query "' . hsc($query).'"</h2>';
88	echo '<div class="search_result">';
89        // printout the results
90	foreach ($pagesList as $crc => $row) {
91            $id = $row['page'];
92            $excerpt = $row['excerpt'];
93            $hid = $row['hid'];
94            $metaData = p_get_metadata($id);
95            if (!empty($row['title'])){
96                $hidTitle = $row['title'];
97            } else {
98                $hidTitle = $metaData['title'];
99            }
100
101            if (!empty($hidTitle)){
102                $title = hsc($id).' ('.hsc($hidTitle).')';
103            } else {
104                $title = hsc($id);
105            }
106
107            $href = !empty($hid) ? (wl($id).'#'.$hid) : wl($id);
108
109            echo '<a href="'.$href.'" title="" class="wikilink1">'.$title.'</a><br/>';
110            echo '<span class="search_cnt">Last modified:'.date("Y-m-d H:i",$metaData['date']['modified']).'</span>';
111            echo '<div class="search_snippet">';
112            echo $excerpt;
113            echo '</div>';
114            echo '<br />';
115	}
116        echo '</div>';
117        echo '<div class="sphinxsearch_nav">';
118        if ($start > 1){
119            $prev = $start - $this->getConf('maxresults');
120            if($prev < 0) $prev = 0;
121
122            echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$prev),'false','&'),
123                                      'prev','wikilink1 gs_prev',$conf['target']['interwiki']);
124        }
125        echo ' ';
126        if($start + $this->getConf('maxresults') < $totalFound){
127            $next = $start + $this->getConf('maxresults');
128
129            echo $this->external_link(wl('',array('do'=>'sphinxsearch','id'=>$query,'start'=>$next),'false','&'),
130                                      'next','wikilink1 gs_next',$conf['target']['interwiki']);
131        }
132        echo '</div>';
133
134    }
135
136     function searchform(){
137          global $lang;
138          global $ACT;
139          global $QUERY;
140
141          // don't print the search form if search action has been disabled
142          if (!actionOk('sphinxsearch')) return false;
143
144          print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
145          print '<input type="hidden" name="do" value="sphinxsearch" />';
146          print '<input type="text" ';
147          if($ACT == 'sphinxsearch') print 'value="'.htmlspecialchars($QUERY).'" ';
148          print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
149          print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
150          print '</div></form>';
151          return true;
152    }
153}
154
155?>
156