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{ 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 ? ' document ' : ' documents ') . ' 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 <h3>Matching pagenames</h3> 182 '; 183 printNamespaces($keywords); 184 echo '</div>'; 185 echo '<div class="sphinxsearch_nav">'; 186 if ($start > 1){ 187 //$prev = $start - $this->getConf('maxresults'); 188 //if($prev < 0) $prev = 0; 189 if(false !== strpos($prev, ',')){ 190 $prevArry = explode(",", $prev); 191 $prevNum = $prevArry[count($prevArry)-1]; 192 unset($prevArry[count($prevArry)-1]); 193 $prevString = implode(",", $prevArry); 194 } else { 195 $prevNum = 0; 196 } 197 198 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$prevNum, 'prev'=>$prevString),'false','&'), 199 'prev','wikilink1 gs_prev',$conf['target']['interwiki']); 200 } 201 echo ' '; 202 203 //if($start + $this->getConf('maxresults') < $totalFound){ 204 //$next = $start + $this->getConf('maxresults'); 205 if($start + $search->getOffset()< $totalFound){ 206 $next = $start + $search->getOffset(); 207 if($start > 1){ 208 $prevString = $prev.','.$start; 209 } 210 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$next,'prev'=>$prevString),'false','&'), 211 'next','wikilink1 gs_next',$conf['target']['interwiki']); 212 } 213 echo '</div>'; 214 215 echo '</div>'; 216 } 217 218 219 } 220 221 function _showResult($row, $keywords, $subpages = false) 222 { 223 $page = $row['page']; 224 $bodyExcerpt = $row['bodyExcerpt']; 225 $titleTextExcerpt = $row['titleTextExcerpt']; 226 $hid = $row['hid']; 227 228 $metaData = p_get_metadata($page); 229 230 if (!empty($titleTextExcerpt)){ 231 $titleText = $titleTextExcerpt; 232 } elseif(!empty($row['title_text'])){ 233 $titleText = $row['title_text']; 234 } elseif(!empty($metaData['title'])){ 235 $titleText = hsc($metaData['title']); 236 } else { 237 $titleText = hsc($page); 238 } 239 240 $namespaces = getNsLinks($page, $keywords, $this->_search); 241 $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page); 242 243 if($subpages){ 244 echo '<div class="search_result_row_child">'; 245 } else { 246 echo '<div class="search_result_row">'; 247 } 248 249 echo '<a class="wikilink1 title" href="'.$href.'" title="" >'.$titleText.'</a><br/>'; 250 echo '<div class="search_snippet">'; 251 echo strip_tags($bodyExcerpt, '<b>,<strong>'); 252 echo '</div>'; 253 $sep=':'; 254 $i = 0; 255 echo '<span class="search_nmsp">'; 256 foreach ($namespaces as $name){ 257 $link = $name['link']; 258 $pageTitle = $name['title']; 259 tpl_link($link, $pageTitle); 260 if ($i++ < count($namespaces)-1){ 261 echo $sep; 262 } 263 } 264 if (!empty($hid)){ 265 echo '#'.$hid; 266 } 267 echo '</span>'; 268 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> '; 269 echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> '; 270 if (!empty($row['subpages'])){ 271 echo '<br />'; 272 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>'; 273 }else { 274 echo '<br />'; 275 } 276 echo '<br />'; 277 echo '</div>'; 278 } 279 280 function searchform(){ 281 global $lang; 282 global $ACT; 283 global $QUERY; 284 285 // don't print the search form if search action has been disabled 286 if (!actionOk('search')) return false; 287 288 print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">'; 289 print '<input type="hidden" name="do" value="search" />'; 290 print '<input type="text" '; 291 if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" '; 292 print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />'; 293 print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />'; 294 print '</div></form>'; 295 return true; 296 } 297 298 function _getCategories($query) 299 { 300 $categories = ''; 301 $query = urldecode($query); 302 if (false !== ($pos = strpos($query, "@categories"))){; 303 $categories = substr($query, $pos + strlen("@categories")); 304 } 305 return trim($categories); 306 } 307 308 function _getKeywords($query) 309 { 310 $keywords = $query; 311 $query = urldecode($query); 312 if (false !== ($pos = strpos($query, "@categories"))){; 313 $keywords = substr($keywords, 0, $pos); 314 } 315 return trim($keywords); 316 } 317} 318 319?> 320