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 $prev = $prev.','.$start; 179 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$next,'prev'=>$prev),'false','&'), 180 'next','wikilink1 gs_next',$conf['target']['interwiki']); 181 } 182 echo '</div>'; 183 } 184 185 } 186 187 function _showResult($row) 188 { 189 $page = $row['page']; 190 $bodyExcerpt = $row['bodyExcerpt']; 191 $titleTextExcerpt = $row['titleTextExcerpt']; 192 $hid = $row['hid']; 193 194 $metaData = p_get_metadata($page); 195 196 if (!empty($titleTextExcerpt)){ 197 $titleText = $titleTextExcerpt; 198 } elseif(!empty($row['title_text'])){ 199 $titleText = $row['title_text']; 200 } elseif(!empty($metaData['title'])){ 201 $titleText = hsc($metaData['title']); 202 } else { 203 $titleText = hsc($page); 204 } 205 206 $namespaces = getNsLinks($page, $keywords, $this->_search); 207 $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page); 208 209 echo '<div class="search_result_row">'; 210 211 echo '<a href="'.$href.'" title="" class="wikilink1">'.$titleText.'</a><br/>'; 212 echo '<div class="search_snippet">'; 213 echo strip_tags($bodyExcerpt, '<b>,<strong>'); 214 echo '</div>'; 215 $sep=':'; 216 $i = 0; 217 echo '<span class="search_nmsp">'; 218 foreach ($namespaces as $name){ 219 $link = $name['link']; 220 $pageTitle = $name['title']; 221 tpl_link($link, $pageTitle); 222 if ($i++ < count($namespaces)-1){ 223 echo $sep; 224 } 225 } 226 if (!empty($hid)){ 227 echo '#'.$hid; 228 } 229 echo '</span>'; 230 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> '; 231 echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> '; 232 echo '<br />'; 233 echo '<br />'; 234 echo '</div>'; 235 } 236 237 function _showSubResult($row) 238 { 239 $page = $row['page']; 240 $bodyExcerpt = $row['bodyExcerpt']; 241 $titleTextExcerpt = $row['titleTextExcerpt']; 242 $hid = $row['hid']; 243 244 $metaData = p_get_metadata($page); 245 246 if (!empty($titleTextExcerpt)){ 247 $titleText = $titleTextExcerpt; 248 } elseif(!empty($row['title_text'])){ 249 $titleText = $row['title_text']; 250 } elseif(!empty($metaData['title'])){ 251 $titleText = hsc($metaData['title']); 252 } else { 253 $titleText = hsc($page); 254 } 255 256 $namespaces = getNsLinks($page, $keywords, $this->_search); 257 $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page); 258 259 echo '<div class="search_result_row_child">'; 260 261 echo '<a href="'.$href.'" title="" class="wikilink1">'.$titleText.'</a><br/>'; 262 echo '<div class="search_snippet">'; 263 echo strip_tags($bodyExcerpt, '<b>,<strong>'); 264 echo '</div>'; 265 $sep=':'; 266 $i = 0; 267 echo '<span class="search_nmsp">'; 268 foreach ($namespaces as $name){ 269 $link = $name['link']; 270 $pageTitle = $name['title']; 271 tpl_link($link, $pageTitle); 272 if ($i++ < count($namespaces)-1){ 273 echo $sep; 274 } 275 } 276 if (!empty($hid)){ 277 echo '#'.$hid; 278 } 279 echo '</span>'; 280 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> '; 281 echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> '; 282 echo '<br />'; 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 } 322 return trim($keywords); 323 } 324} 325 326?> 327