xref: /plugin/sphinxsearch-was/action.php (revision 50:3feef767fd3a)
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 '<b>Nothing was found by ' . $query . '</b>!';
91            return;
92        } else {
93            echo '<style type="text/css">
94                div.dokuwiki .search_snippet{
95                    color:#000000;
96                    margin-left:0px;
97                    font-size: 13px;
98                }
99                div.dokuwiki .search_result{
100                    width:800px;
101                    float:left;
102                }
103                div.dokuwiki .search_result a.title{
104                    font:16px Arial,Helvetica,sans-serif;
105                }
106                div.dokuwiki .search_result span{
107                    font:14px Arial,Helvetica,sans-serif;
108                }
109                div.dokuwiki .search_sidebar{
110                    width:300px;
111                    float:right;
112                    margin-right: 30px;
113                }
114                div.dokuwiki .search_result_row{
115                    color:#000000;
116                    margin-left:0px;
117                    width:600px;
118                    text-align:left;
119                }
120                div.dokuwiki .search_result_row_child{
121                    color:#000000;
122                    margin-left:30px;
123                    width:600px;
124                    text-align:left;
125                }
126                div.dokuwiki .hide{
127                    display:none;
128                }
129                div.dokuwiki .search_cnt{
130                    color:#909090;
131                    font:14px Arial,Helvetica,sans-serif;
132                }
133                div.dokuwiki .search_nmsp{
134                    font-size: 10px;
135                }
136                div.dokuwiki .sphinxsearch_nav{
137                    clear:both;
138                }
139                </style>
140                <script type="text/javascript">
141function sh(id)
142{
143    var e = document.getElementById(id);
144    if(e.style.display == "block")
145        e.style.display = "none";
146    else
147        e.style.display = "block";
148}
149</script>
150';
151
152            echo '<h2>Found '.$totalFound . ($totalFound == 1  ? ' document ' : ' documents ') . ' for query "' . hsc($query).'"</h2>';
153            echo '<div class="search_result">';
154            // printout the results
155            $pageListGroupByPage = array();
156            foreach ($pagesList as $row) {
157                $page = $row['page'];
158                if(!isset ($pageListGroupByPage[$page])){
159                    $pageListGroupByPage[$page] = $row;
160                } else {
161                    $pageListGroupByPage[$page]['subpages'][] = $row;
162                }
163            }
164            foreach ($pageListGroupByPage as $row) {
165                $this->_showResult($row, $keywords, false);
166                if(!empty($row['subpages'])){
167                    echo '<div id="more'.$row['page'].'" class="hide">';
168                    foreach($row['subpages'] as $sub){
169                        $this->_showResult($sub, $keywords, true);
170                    }
171                    echo '</div>';
172                }
173
174            }
175            echo '</div>';
176            echo '<div class="search_sidebar">
177                <h3>Matching pagenames</h3>
178                ';
179            printNamespaces($keywords);
180            echo '</div>';
181            echo '<div class="sphinxsearch_nav">';
182            if ($start > 1){
183                //$prev = $start - $this->getConf('maxresults');
184                //if($prev < 0) $prev = 0;
185                if(false !== strpos($prev, ',')){
186                    $prevArry = explode(",", $prev);
187                    $prevNum = $prevArry[count($prevArry)-1];
188                    unset($prevArry[count($prevArry)-1]);
189                    $prevString = implode(",", $prevArry);
190                } else {
191                    $prevNum = 0;
192                }
193
194                echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$prevNum, 'prev'=>$prevString),'false','&'),
195                                          'prev','wikilink1 gs_prev',$conf['target']['interwiki']);
196            }
197            echo ' ';
198
199            //if($start + $this->getConf('maxresults') < $totalFound){
200                //$next = $start + $this->getConf('maxresults');
201            if($start + $search->getOffset()< $totalFound){
202                $next = $start + $search->getOffset();
203                if($start > 1){
204                    $prevString = $prev.','.$start;
205                }
206                echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$next,'prev'=>$prevString),'false','&'),
207                                          'next','wikilink1 gs_next',$conf['target']['interwiki']);
208            }
209            echo '</div>';
210        }
211
212    }
213
214    function _showResult($row, $keywords, $subpages = false)
215    {
216        $page = $row['page'];
217        $bodyExcerpt = $row['bodyExcerpt'];
218        $titleTextExcerpt = $row['titleTextExcerpt'];
219        $hid = $row['hid'];
220
221        $metaData = p_get_metadata($page);
222
223        if (!empty($titleTextExcerpt)){
224            $titleText = $titleTextExcerpt;
225        } elseif(!empty($row['title_text'])){
226            $titleText = $row['title_text'];
227        } elseif(!empty($metaData['title'])){
228            $titleText = hsc($metaData['title']);
229        } else {
230            $titleText = hsc($page);
231        }
232
233        $namespaces = getNsLinks($page, $keywords, $this->_search);
234        $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page);
235
236        if($subpages){
237            echo '<div class="search_result_row_child">';
238        } else {
239            echo '<div class="search_result_row">';
240        }
241
242        echo '<a class="wikilink1 title" href="'.$href.'" title="" >'.$titleText.'</a><br/>';
243        echo '<div class="search_snippet">';
244        echo strip_tags($bodyExcerpt, '<b>,<strong>');
245        echo '</div>';
246        $sep=':';
247        $i = 0;
248        echo '<span class="search_nmsp">';
249        foreach ($namespaces as $name){
250            $link = $name['link'];
251            $pageTitle = $name['title'];
252            tpl_link($link, $pageTitle, "class='wikilink1'");
253            if ($i++ < count($namespaces)-1){
254                echo $sep;
255            }
256        }
257        if (!empty($hid)){
258            echo '#'.$hid;
259        }
260        echo '</span>';
261        echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> ';
262        echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> ';
263        if (!empty($row['subpages'])){
264            echo '<br />';
265            echo '<div style="text-align:right"><a href="javascript:void(0)" onClick="sh('."'more".$page."'".');" class="wikilink1">More results from this document</a></div>';
266        }else {
267            echo '<br />';
268        }
269        echo '<br />';
270        echo '</div>';
271    }
272
273     function searchform(){
274          global $lang;
275          global $ACT;
276          global $QUERY;
277
278          // don't print the search form if search action has been disabled
279          if (!actionOk('search')) return false;
280
281          print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
282          print '<input type="hidden" name="do" value="search" />';
283          print '<input type="text" ';
284          if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" ';
285          print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
286          print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
287          print '</div></form>';
288          return true;
289    }
290
291    function _getCategories($query)
292    {
293        $categories = '';
294        $query = urldecode($query);
295        if (false !== ($pos = strpos($query, "@categories"))){;
296            $categories = substr($query, $pos + strlen("@categories"));
297        }
298        return trim($categories);
299    }
300
301    function _getKeywords($query)
302    {
303        $keywords = $query;
304        $query = urldecode($query);
305        if (false !== ($pos = strpos($query, "@categories"))){;
306            $keywords = substr($keywords, 0, $pos);
307        }
308        return trim($keywords);
309    }
310}
311
312?>
313