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