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