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 22 /** 23 * return some info 24 */ 25 function getInfo() { 26 return confToHash(dirname(__FILE__).'/plugin.info.txt'); 27 } 28 29 /** 30 * Register to the content display event to place the results under it. 31 */ 32 /** 33 * register the eventhandlers 34 */ 35 function register(&$controller){ 36 $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'handle_act_unknown', array()); 37 } 38 39 /** 40 * If our own 'googlesearch' action was given we produce our content here 41 */ 42 function handle_act_unknown(&$event, $param){ 43 global $ACT; 44 global $QUERY; 45 if($ACT != 'search') return; // nothing to do for us 46 47 // we can handle it -> prevent others 48 $event->stopPropagation(); 49 $event->preventDefault(); 50 51 52 $this->_search($QUERY,$_REQUEST['start']); 53 } 54 55 /** 56 * do the search and displays the result 57 */ 58 function _search($query, $start) { 59 global $conf; 60 61 $start = (int) $start; 62 if($start < 0) $start = 0; 63 64 $categories = $this->_getCategories($query); 65 $keywords = $this->_getKeywords($query); 66 67 $search = new SphinxSearch($this->getConf('host'), $this->getConf('port'), $this->getConf('index')); 68 $search->setSnippetSize($this->getConf('snippetsize')); 69 $search->setArroundWordsCount($this->getConf('aroundwords')); 70 $search->setTitlePriority($this->getConf('title_priority')); 71 $search->setBodyPriority($this->getConf('body_priority')); 72 $search->setCategoriesPriority($this->getConf('categories_priority')); 73 74 $pagesList = $search->search($keywords, $categories, $start, $this->getConf('maxresults')); 75 76 if ($search->getError()){ 77 echo '<b>' . $search->getError() . '</b>!'; 78 return; 79 } 80 81 $totalFound = $search->getTotalFound(); 82 if(empty($pagesList)){ 83 echo '<b>Nothing was found by ' . $query . '</b>!'; 84 return; 85 } else { 86 echo '<style type="text/css"> 87 div.dokuwiki .search_snippet{ 88 color:#000000; 89 margin-left:0px; 90 font-size: 13px; 91 } 92 div.dokuwiki .search_result{ 93 width:800px; 94 float:left; 95 } 96 div.dokuwiki .search_sidebar{ 97 width:200px; 98 float:right; 99 margin-right: 30px; 100 } 101 div.dokuwiki .search_result_row{ 102 color:#000000; 103 margin-left:0px; 104 width:800px; 105 } 106 div.dokuwiki .search_result_row_child{ 107 color:#000000; 108 margin-left:30px; 109 width:800px; 110 } 111 div.dokuwiki .search_cnt{ 112 color:#CCCCCC; 113 font-size: 10px; 114 } 115 div.dokuwiki .search_nmsp{ 116 font-size: 10px; 117 } 118 div.dokuwiki .sphinxsearch_nav{ 119 clear:both; 120 } 121 </style> 122 '; 123 124 echo '<h2>Found '.$totalFound . ($totalFound == 1 ? ' document ' : ' documents ') . ' for query "' . hsc($query).'"</h2>'; 125 echo '<div class="search_result">'; 126 // printout the results 127 $prevPage = ''; 128 foreach ($pagesList as $crc => $row) { 129 $page = $row['page']; 130 $bodyExcerpt = $row['bodyExcerpt']; 131 $titleTextExcerpt = $row['titleTextExcerpt']; 132 $hid = $row['hid']; 133 134 $metaData = p_get_metadata($page); 135 136 if (!empty($titleTextExcerpt)){ 137 $titleText = $titleTextExcerpt; 138 } elseif(!empty($row['title_text'])){ 139 $titleText = $row['title_text']; 140 } elseif(!empty($metaData['title'])){ 141 $titleText = hsc($metaData['title']); 142 } else { 143 $titleText = hsc($page); 144 } 145 146 $namespaces = getNsLinks($page, $keywords, $search); 147 $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page); 148 149 if($page == $prevPage){ 150 echo '<div class="search_result_row_child">'; 151 } else { 152 echo '<div class="search_result_row">'; 153 } 154 155 echo '<a href="'.$href.'" title="" class="wikilink1">'.$titleText.'</a><br/>'; 156 echo '<div class="search_snippet">'; 157 echo strip_tags($bodyExcerpt, '<b>,<strong>'); 158 echo '</div>'; 159 $sep=':'; 160 $i = 0; 161 echo '<span class="search_nmsp">'; 162 foreach ($namespaces as $name){ 163 $link = $name['link']; 164 $pageTitle = $name['title']; 165 tpl_link($link, $pageTitle); 166 if ($i++ < count($namespaces)-1){ 167 echo $sep; 168 } 169 } 170 if (!empty($hid)){ 171 echo '#'.$hid; 172 } 173 echo '</span>'; 174 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> '; 175 echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> '; 176 echo '<br />'; 177 echo '<br />'; 178 echo '</div>'; 179 $prevPage = $page; 180 } 181 echo '</div>'; 182 echo '<div class="search_sidebar">'; 183 printNamespaces($keywords); 184 echo '</div>'; 185 echo '<div class="sphinxsearch_nav">'; 186 /*if ($start > 1){ 187 //$prev = $start - $this->getConf('maxresults'); 188 $prev = $start - $search->getOffset(); 189 if($prev < 0) $prev = 0; 190 191 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$prev),'false','&'), 192 'prev','wikilink1 gs_prev',$conf['target']['interwiki']); 193 } 194 echo ' '; 195 * 196 */ 197 //if($start + $this->getConf('maxresults') < $totalFound){ 198 //$next = $start + $this->getConf('maxresults'); 199 if($start + $search->getOffset() < $totalFound){ 200 $next = $start + $search->getOffset(); 201 202 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$next),'false','&'), 203 'next','wikilink1 gs_next',$conf['target']['interwiki']); 204 } 205 echo '</div>'; 206 } 207 208 } 209 210 function searchform(){ 211 global $lang; 212 global $ACT; 213 global $QUERY; 214 215 // don't print the search form if search action has been disabled 216 if (!actionOk('search')) return false; 217 218 print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">'; 219 print '<input type="hidden" name="do" value="search" />'; 220 print '<input type="text" '; 221 if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" '; 222 print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />'; 223 print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />'; 224 print '</div></form>'; 225 return true; 226 } 227 228 function _getCategories($query) 229 { 230 $categories = ''; 231 $query = urldecode($query); 232 if (false !== ($pos = strpos($query, "@categories"))){; 233 $categories = substr($query, $pos + strlen("@categories")); 234 } 235 return trim($categories); 236 } 237 238 function _getKeywords($query) 239 { 240 $keywords = $query; 241 $query = urldecode($query); 242 if (false !== ($pos = strpos($query, "@categories"))){; 243 $keywords = substr($keywords, 0, $pos); 244 } 245 return trim($keywords); 246 } 247} 248 249?> 250