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