xref: /plugin/sphinxsearch-was/action.php (revision 62:d7853ac635b4)
1<?php
2/**
3 * Script to search in dokuwiki documents
4 *
5 * @author Yaroslav Vorozhko <yaroslav@ivinco.com>
6 */
7
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10
11require_once(DOKU_INC.'inc/parser/parser.php');
12
13require_once(DOKU_PLUGIN . 'action.php');
14require_once(DOKU_PLUGIN . 'sphinxsearch/sphinxapi.php');
15require_once(DOKU_PLUGIN . 'sphinxsearch/PageMapper.php');
16require_once(DOKU_PLUGIN . 'sphinxsearch/SphinxSearch.php');
17require_once(DOKU_PLUGIN . 'sphinxsearch/functions.php');
18
19
20class action_plugin_sphinxsearch extends DokuWiki_Action_Plugin {
21    var $_search = null;
22
23    /**
24	* return some info
25	*/
26    function getInfo() {
27        return confToHash(dirname(__FILE__).'/plugin.info.txt');
28	}
29
30    /**
31	* Register to the content display event to place the results under it.
32	*/
33    /**
34     * register the eventhandlers
35     */
36    function register(&$controller){
37        $controller->register_hook('TPL_CONTENT_DISPLAY',   'BEFORE', $this, 'handle_act_unknown', array());
38    }
39
40    /**
41     * If our own 'googlesearch' action was given we produce our content here
42     */
43    function handle_act_unknown(&$event, $param){
44        global $ACT;
45        global $QUERY;
46        if($ACT != 'search') return; // nothing to do for us
47
48        // we can handle it -> prevent others
49        $event->stopPropagation();
50        $event->preventDefault();
51
52
53        $this->_search($QUERY,$_REQUEST['start'],$_REQUEST['prev']);
54    }
55
56    /**
57     * do the search and displays the result
58     */
59    function _search($query, $start, $prev) {
60        global $conf;
61
62        $start = (int) $start;
63        if($start < 0){
64            $start = 0;
65        }
66        if(empty($prev)){
67            $prev = 0;
68        }
69
70        $categories = $this->_getCategories($query);
71        $keywords = $this->_getKeywords($query);
72
73        $search = new SphinxSearch($this->getConf('host'), $this->getConf('port'), $this->getConf('index'));
74        $search->setSnippetSize($this->getConf('snippetsize'));
75        $search->setArroundWordsCount($this->getConf('aroundwords'));
76        $search->setTitlePriority($this->getConf('title_priority'));
77        $search->setBodyPriority($this->getConf('body_priority'));
78        $search->setCategoriesPriority($this->getConf('categories_priority'));
79
80        $pagesList = $search->search($keywords, $categories, $start, $this->getConf('maxresults'));
81        $this->_search = $search;
82
83        if ($search->getError()){
84            echo '<b>' . $search->getError() . '</b>!';
85            return;
86        }
87
88        $totalFound = $search->getTotalFound();
89        if(empty($pagesList)){
90            echo 'Your search - <strong>' . $query . '</strong> - did not match any documents.';
91            return;
92        } else {
93            echo '<style type="text/css">
94                div.dokuwiki .search{
95                    width:1024px;
96                }
97                div.dokuwiki .search_snippet{
98                    color:#000000;
99                    margin-left:0px;
100                    font-size: 13px;
101                }
102                div.dokuwiki .search_result{
103                    width:600px;
104                    float:left;
105                }
106                div.dokuwiki .search_result a.title{
107                    font:16px Arial,Helvetica,sans-serif;
108                }
109                div.dokuwiki .search_result span{
110                    font:12px Arial,Helvetica,sans-serif;
111                }
112                div.dokuwiki .search_sidebar{
113                    width:300px;
114                    float:right;
115                    margin-right: 30px;
116                }
117                div.dokuwiki .search_result_row{
118                    color:#000000;
119                    margin-left:0px;
120                    width:600px;
121                    text-align:left;
122                }
123                div.dokuwiki .search_result_row_child{
124                    color:#000000;
125                    margin-left:30px;
126                    width:600px;
127                    text-align:left;
128                }
129                div.dokuwiki .hide{
130                    display:none;
131                }
132                div.dokuwiki .search_cnt{
133                    color:#909090;
134                    font:12px Arial,Helvetica,sans-serif;
135                }
136                div.dokuwiki .search_nmsp{
137                    font-size: 10px;
138                }
139                div.dokuwiki .sphinxsearch_nav{
140                    clear:both;
141                }
142                </style>
143                <script type="text/javascript">
144function sh(id)
145{
146    var e = document.getElementById(id);
147    if(e.style.display == "block")
148        e.style.display = "none";
149    else
150        e.style.display = "block";
151}
152</script>
153';
154
155            echo '<h2>Found '.$totalFound . ($totalFound == 1  ? ' match ' : ' matches ') . ' for query "' . hsc($query).'"</h2>';
156            echo '<div class="search">';
157            echo '<div class="search_result">';
158            // printout the results
159            $pageListGroupByPage = array();
160            foreach ($pagesList as $row) {
161                $page = $row['page'];
162                if(!isset ($pageListGroupByPage[$page])){
163                    $pageListGroupByPage[$page] = $row;
164                } else {
165                    $pageListGroupByPage[$page]['subpages'][] = $row;
166                }
167            }
168            foreach ($pageListGroupByPage as $row) {
169                $this->_showResult($row, $keywords, false);
170                if(!empty($row['subpages'])){
171                    echo '<div id="more'.$row['page'].'" class="hide">';
172                    foreach($row['subpages'] as $sub){
173                        $this->_showResult($sub, $keywords, true);
174                    }
175                    echo '</div>';
176                }
177
178            }
179            echo '</div>';
180            echo '<div class="search_sidebar">';
181            printNamespaces($search->removeStars($keywords));
182            echo '</div>';
183            echo '<div class="sphinxsearch_nav">';
184            if ($start > 1){
185                //$prev = $start - $this->getConf('maxresults');
186                //if($prev < 0) $prev = 0;
187                if(false !== strpos($prev, ',')){
188                    $prevArry = explode(",", $prev);
189                    $prevNum = $prevArry[count($prevArry)-1];
190                    unset($prevArry[count($prevArry)-1]);
191                    $prevString = implode(",", $prevArry);
192                } else {
193                    $prevNum = 0;
194                }
195
196                echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$prevNum, 'prev'=>$prevString),'false','&'),
197                                          'prev','wikilink1 gs_prev',$conf['target']['interwiki']);
198            }
199            echo ' ';
200
201            //if($start + $this->getConf('maxresults') < $totalFound){
202                //$next = $start + $this->getConf('maxresults');
203            if($start + $search->getOffset()< $totalFound){
204                $next = $start + $search->getOffset();
205                if($start > 1){
206                    $prevString = $prev.','.$start;
207                }
208                echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$next,'prev'=>$prevString),'false','&'),
209                                          'next','wikilink1 gs_next',$conf['target']['interwiki']);
210            }
211            echo '</div>';
212
213            echo '</div>';
214        }
215
216
217    }
218
219    function _showResult($row, $keywords, $subpages = false)
220    {
221        $page = $row['page'];
222        $bodyExcerpt = $row['bodyExcerpt'];
223        $titleTextExcerpt = $row['titleTextExcerpt'];
224        $hid = $row['hid'];
225
226        $metaData = p_get_metadata($page);
227
228        if (!empty($titleTextExcerpt)){
229            $titleText = $titleTextExcerpt;
230        } elseif(!empty($row['title_text'])){
231            $titleText = $row['title_text'];
232        } elseif(!empty($metaData['title'])){
233            $titleText = hsc($metaData['title']);
234        } else {
235            $titleText = hsc($page);
236        }
237
238        $namespaces = getNsLinks($page, $keywords, $this->_search);
239        $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page);
240
241        if($subpages){
242            echo '<div class="search_result_row_child">';
243        } else {
244            echo '<div class="search_result_row">';
245        }
246
247        echo '<a class="wikilink1 title" href="'.$href.'" title="" >'.$titleText.'</a><br/>';
248        echo '<div class="search_snippet">';
249        echo strip_tags($bodyExcerpt, '<b>,<strong>');
250        echo '</div>';
251        $sep=':';
252        $i = 0;
253        echo '<span class="search_nmsp">';
254        foreach ($namespaces as $name){
255            $link = $name['link'];
256            $pageTitle = $name['title'];
257            tpl_link($link, $pageTitle);
258            if ($i++ < count($namespaces)-1){
259                echo $sep;
260            }
261        }
262        if (!empty($hid)){
263            echo '#'.$hid;
264        }
265        echo '</span>';
266
267        if (!empty($metaData['last_change']['date'])){
268            echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['last_change']['date']).'</span> ';
269        } else if (!empty($metaData['date']['created'])){
270            echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['created']).'</span> ';
271        }
272
273        if(!empty($metaData['last_change']['user'])){
274            echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> ';
275        } else if(!empty($metaData['creator'])){
276            echo '<span class="search_cnt">by '.$metaData['creator'].'</span> ';
277        }
278
279        if (!empty($row['subpages'])){
280            echo '<br />';
281            echo '<div style="text-align:right;font:12px Arial,Helvetica,sans-serif;text-decoration:underline;"><a href="javascript:void(0)" onClick="sh('."'more".$page."'".');" >More matches in this document</a></div>';
282        }else {
283            echo '<br />';
284        }
285        echo '<br />';
286        echo '</div>';
287    }
288
289     function searchform(){
290          global $lang;
291          global $ACT;
292          global $QUERY;
293
294          // don't print the search form if search action has been disabled
295          if (!actionOk('search')) return false;
296
297          print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
298          print '<input type="hidden" name="do" value="search" />';
299          print '<input type="text" ';
300          if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" ';
301          print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
302          print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
303          print '</div></form>';
304          return true;
305    }
306
307    function _getCategories($query)
308    {
309        $categories = '';
310        $query = urldecode($query);
311        if (false !== ($pos = strpos($query, "@categories"))){;
312            $categories = substr($query, $pos + strlen("@categories"));
313        }
314        return trim($categories);
315    }
316
317    function _getKeywords($query)
318    {
319        $keywords = $query;
320        $query = urldecode($query);
321        if (false !== ($pos = strpos($query, "@categories"))){;
322            $keywords = substr($keywords, 0, $pos);
323        }
324        return trim($keywords);
325    }
326}
327
328?>
329