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 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 echo '</div>'; 212 } 213 214 215 } 216 217 function _showResult($row, $keywords, $subpages = false) 218 { 219 $page = $row['page']; 220 $bodyExcerpt = $row['bodyExcerpt']; 221 $titleTextExcerpt = $row['titleTextExcerpt']; 222 $hid = $row['hid']; 223 224 $metaData = p_get_metadata($page); 225 226 if (!empty($titleTextExcerpt)){ 227 $titleText = $titleTextExcerpt; 228 } elseif(!empty($row['title_text'])){ 229 $titleText = $row['title_text']; 230 } elseif(!empty($metaData['title'])){ 231 $titleText = hsc($metaData['title']); 232 } else { 233 $titleText = hsc($page); 234 } 235 236 $namespaces = getNsLinks($page, $keywords, $this->_search); 237 $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page); 238 239 if($subpages){ 240 echo '<div class="search_result_row_child">'; 241 } else { 242 echo '<div class="search_result_row">'; 243 } 244 245 echo '<a class="wikilink1 title" href="'.$href.'" title="" >'.$titleText.'</a><br/>'; 246 echo '<div class="search_snippet">'; 247 echo strip_tags($bodyExcerpt, '<b>,<strong>'); 248 echo '</div>'; 249 $sep=':'; 250 $i = 0; 251 echo '<span class="search_nmsp">'; 252 foreach ($namespaces as $name){ 253 $link = $name['link']; 254 $pageTitle = $name['title']; 255 tpl_link($link, $pageTitle); 256 if ($i++ < count($namespaces)-1){ 257 echo $sep; 258 } 259 } 260 if (!empty($hid)){ 261 echo '#'.$hid; 262 } 263 echo '</span>'; 264 265 if (!empty($metaData['last_change']['date'])){ 266 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['last_change']['date']).'</span> '; 267 } else if (!empty($metaData['date']['created'])){ 268 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['created']).'</span> '; 269 } 270 271 if(!empty($metaData['last_change']['user'])){ 272 echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> '; 273 } else if(!empty($metaData['creator'])){ 274 echo '<span class="search_cnt">by '.$metaData['creator'].'</span> '; 275 } 276 277 if (!empty($row['subpages'])){ 278 echo '<br />'; 279 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>'; 280 }else { 281 echo '<br />'; 282 } 283 echo '<br />'; 284 echo '</div>'; 285 } 286 287 function searchform(){ 288 global $lang; 289 global $ACT; 290 global $QUERY; 291 292 // don't print the search form if search action has been disabled 293 if (!actionOk('search')) return false; 294 295 print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">'; 296 print '<input type="hidden" name="do" value="search" />'; 297 print '<input type="text" '; 298 if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" '; 299 print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />'; 300 print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />'; 301 print '</div></form>'; 302 return true; 303 } 304 305 function _getCategories($query) 306 { 307 $categories = ''; 308 $query = urldecode($query); 309 if (false !== ($pos = strpos($query, "@categories"))){; 310 $categories = substr($query, $pos + strlen("@categories")); 311 } 312 return trim($categories); 313 } 314 315 function _getKeywords($query) 316 { 317 $keywords = $query; 318 $query = urldecode($query); 319 if (false !== ($pos = strpos($query, "-@categories"))){; 320 $keywords = substr($keywords, 0, $pos); 321 }else if (false !== ($pos = strpos($query, "@categories"))){; 322 $keywords = substr($keywords, 0, $pos); 323 } 324 return trim($keywords); 325 } 326} 327 328?> 329