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); 145 if(!empty($row['subpages'])){ 146 foreach($row['subpages'] as $sub){ 147 $this->_showSubResult($sub); 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 $prevAr = explode(",", $prev); 162 $prevNum = $prevAr[count($prevAr)-1]; 163 unset($prevAr[count($prevAr)-1]); 164 $prevPrev = implode(",", $prevAr); 165 } else { 166 $prevNum = 0; 167 } 168 169 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$prevNum, 'prev'=>$prevPrev),'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 $prev = $prev.','.$start; 180 } 181 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$next,'prev'=>$prev),'false','&'), 182 'next','wikilink1 gs_next',$conf['target']['interwiki']); 183 } 184 echo '</div>'; 185 } 186 187 } 188 189 function _showResult($row) 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 echo '<div class="search_result_row">'; 212 213 echo '<a href="'.$href.'" title="" class="wikilink1">'.$titleText.'</a><br/>'; 214 echo '<div class="search_snippet">'; 215 echo strip_tags($bodyExcerpt, '<b>,<strong>'); 216 echo '</div>'; 217 $sep=':'; 218 $i = 0; 219 echo '<span class="search_nmsp">'; 220 foreach ($namespaces as $name){ 221 $link = $name['link']; 222 $pageTitle = $name['title']; 223 tpl_link($link, $pageTitle); 224 if ($i++ < count($namespaces)-1){ 225 echo $sep; 226 } 227 } 228 if (!empty($hid)){ 229 echo '#'.$hid; 230 } 231 echo '</span>'; 232 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> '; 233 echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> '; 234 echo '<br />'; 235 echo '<br />'; 236 echo '</div>'; 237 } 238 239 function _showSubResult($row) 240 { 241 $page = $row['page']; 242 $bodyExcerpt = $row['bodyExcerpt']; 243 $titleTextExcerpt = $row['titleTextExcerpt']; 244 $hid = $row['hid']; 245 246 $metaData = p_get_metadata($page); 247 248 if (!empty($titleTextExcerpt)){ 249 $titleText = $titleTextExcerpt; 250 } elseif(!empty($row['title_text'])){ 251 $titleText = $row['title_text']; 252 } elseif(!empty($metaData['title'])){ 253 $titleText = hsc($metaData['title']); 254 } else { 255 $titleText = hsc($page); 256 } 257 258 $namespaces = getNsLinks($page, $keywords, $this->_search); 259 $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page); 260 261 echo '<div class="search_result_row_child">'; 262 263 echo '<a href="'.$href.'" title="" class="wikilink1">'.$titleText.'</a><br/>'; 264 echo '<div class="search_snippet">'; 265 echo strip_tags($bodyExcerpt, '<b>,<strong>'); 266 echo '</div>'; 267 $sep=':'; 268 $i = 0; 269 echo '<span class="search_nmsp">'; 270 foreach ($namespaces as $name){ 271 $link = $name['link']; 272 $pageTitle = $name['title']; 273 tpl_link($link, $pageTitle); 274 if ($i++ < count($namespaces)-1){ 275 echo $sep; 276 } 277 } 278 if (!empty($hid)){ 279 echo '#'.$hid; 280 } 281 echo '</span>'; 282 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> '; 283 echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> '; 284 echo '<br />'; 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