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('arroundwords')); 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 $totalFound = $search->getTotalFound(); 77 if(empty($pagesList)){ 78 echo '<b>Nothing was found by ' . $query . '</b>!'; 79 return; 80 } else { 81 echo '<style type="text/css"> 82 div.dokuwiki .search_snippet{ 83 color:#000000; 84 margin-left:0px; 85 } 86 div.dokuwiki .search_cnt{ 87 color:#CCCCCC; 88 font-size: 10px; 89 } 90 div.dokuwiki .search_nmsp{ 91 font-size: 10px; 92 } 93 </style> 94 '; 95 96 echo '<h2>Found '.$totalFound . ($totalFound == 1 ? ' document ' : ' documents ') . ' for query "' . hsc($query).'"</h2>'; 97 echo '<div class="search_result">'; 98 // printout the results 99 foreach ($pagesList as $crc => $row) { 100 $page = $row['page']; 101 $bodyExcerpt = $row['bodyExcerpt']; 102 $titleTextExcerpt = $row['titleTextExcerpt']; 103 $hid = $row['hid']; 104 105 $metaData = p_get_metadata($page); 106 107 if (!empty($titleTextExcerpt)){ 108 $titleText = $titleTextExcerpt; 109 } elseif(!empty($row['title_text'])){ 110 $titleText = $row['title_text']; 111 } elseif(!empty($metaData['title'])){ 112 $titleText = hsc($metaData['title']); 113 } else { 114 $titleText = hsc($page); 115 } 116 117 $namespaces = getNsLinks($page, $keywords, $search); 118 $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page); 119 120 echo '<a href="'.$href.'" title="" class="wikilink1">'.$titleText.'</a><br/>'; 121 echo '<div class="search_snippet">'; 122 echo strip_tags($bodyExcerpt, '<b>,<strong>'); 123 echo '</div>'; 124 $sep=':'; 125 $i = 0; 126 echo '<span class="search_nmsp">'; 127 foreach ($namespaces as $name){ 128 $link = $name['link']; 129 $pageTitle = $name['title']; 130 tpl_link($link, $pageTitle); 131 if ($i++ < count($namespaces)-1){ 132 echo $sep; 133 } 134 } 135 if (!empty($hid)){ 136 echo '#'.$hid; 137 } 138 echo '</span>'; 139 echo '<span class="search_cnt"> - Last modified '.date("Y-m-d H:i",$metaData['date']['modified']).'</span> '; 140 echo '<span class="search_cnt">by '.$metaData['last_change']['user'].'</span> '; 141 echo '<br />';echo '<br />'; 142 } 143 echo '</div>'; 144 echo '<div class="sphinxsearch_nav">'; 145 if ($start > 1){ 146 $prev = $start - $this->getConf('maxresults'); 147 if($prev < 0) $prev = 0; 148 149 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$prev),'false','&'), 150 'prev','wikilink1 gs_prev',$conf['target']['interwiki']); 151 } 152 echo ' '; 153 if($start + $this->getConf('maxresults') < $totalFound){ 154 $next = $start + $this->getConf('maxresults'); 155 156 echo $this->external_link(wl('',array('do'=>'search','id'=>$query,'start'=>$next),'false','&'), 157 'next','wikilink1 gs_next',$conf['target']['interwiki']); 158 } 159 echo '</div>'; 160 } 161 162 } 163 164 function searchform(){ 165 global $lang; 166 global $ACT; 167 global $QUERY; 168 169 // don't print the search form if search action has been disabled 170 if (!actionOk('search')) return false; 171 172 print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">'; 173 print '<input type="hidden" name="do" value="search" />'; 174 print '<input type="text" '; 175 if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" '; 176 print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />'; 177 print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />'; 178 print '</div></form>'; 179 return true; 180 } 181 182 function _getCategories($query) 183 { 184 $categories = ''; 185 $query = urldecode($query); 186 if (false !== ($pos = strpos($query, "@categories"))){; 187 $categories = substr($query, $pos + strlen("@categories")); 188 } 189 return trim($categories); 190 } 191 192 function _getKeywords($query) 193 { 194 $keywords = $query; 195 $query = urldecode($query); 196 if (false !== ($pos = strpos($query, "@categories"))){; 197 $keywords = substr($keywords, 0, $pos); 198 } 199 return trim($keywords); 200 } 201} 202 203?> 204