1<?php 2/* 3 * To change this template, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 7class SphinxSearch 8{ 9 private $_sphinx = null; 10 private $_result = array(); 11 private $_index = null; 12 private $_query = ''; 13 14 private $_snippetSize = 256; 15 private $_aroundKeyword = 5; 16 private $_resultsPerPage = 10; 17 18 private $_titlePriority = 1; 19 private $_bodyPriority = 1; 20 private $_namespacePriority = 1; 21 private $_pagenamePriority = 1; 22 23 public function __construct($host, $port, $index) 24 { 25 $this->_sphinx = new SphinxClient(); 26 $this->_sphinx->SetServer($host, $port); 27 $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 28 29 $this->_index = $index; 30 } 31 32 public function setSearchAllQuery($keywords, $categories) 33 { 34 $keywords = $this->_sphinx->EscapeString($keywords); 35 $keywords = $this->_enableQuotesAndDefis($keywords); 36 $starKeyword = $this->starQuery($keywords); 37 $this->_query = "(@(namespace,pagename) $starKeyword) | (@(body,title) {$keywords})"; 38 } 39 40 public function setSearchAllQueryWithCategoryFilter($keywords, $categories) 41 { 42 $keywords = $this->_sphinx->EscapeString($keywords); 43 $keywords = $this->_enableQuotesAndDefis($keywords); 44 $starKeyword = $this->starQuery($keywords); 45 if(strpos($categories, "-") === 0){ 46 $categories = '-"'.substr($categories, 1).'"'; 47 } 48 $this->_query = "(@(namespace,pagename) {$categories}) & ((@(body,title) {$keywords}) | (@(namespace,pagename) {$starKeyword}))"; 49 } 50 51 public function setSearchCategoryQuery($keywords, $categories) 52 { 53 $keywords = $this->_sphinx->EscapeString($keywords); 54 $keywords = $this->_enableQuotesAndDefis($keywords); 55 56 $starKeyword = $this->starQuery($keywords); 57 if (!empty($categories)){ 58 $this->_query = "(@(namespace,pagename) $categories $starKeyword)"; 59 } else { 60 $this->_query = "(@(namespace,pagename) $starKeyword)"; 61 } 62 63 } 64 65 public function setSearchOnlyPagename() 66 { 67 $this->_query = "(@(pagename) {$this->_query})"; 68 } 69 70 public function search($start, $resultsPerPage = 10) 71 { 72 $this->_resultsPerPage = $resultsPerPage; 73 74 $this->_sphinx->SetFieldWeights(array( 75 'namespace' => $this->_namespacePriority, 76 'pagename' => $this->_pagenamePriority, 77 'title' => $this->_titlePriority, 78 'body' => $this->_bodyPriority) 79 ); 80 81 $this->_sphinx->SetLimits($start, $resultsPerPage+100, 1000); 82 83 $this->_result = $this->_sphinx->Query($this->_query, $this->_index); 84 85 if (empty($this->_result['matches'])) { 86 return false; 87 } 88 return true; 89 } 90 91 public function getPages($keywords) 92 { 93 if (empty($this->_result['matches'])) { 94 return false; 95 } 96 97 $pagesIdsAll = $this->getPagesIds(); 98 $this->_offset = 0; 99 $counter = 0; 100 $tmpRes = array(); 101 $pagesIds = array(); 102 foreach($pagesIdsAll as $id => $pageData){ 103 $this->_offset++; 104 if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 105 if(!isset($tmpRes[$pageData['page']])){ 106 $tmpRes[$pageData['page']] = 1; 107 $counter++; 108 } 109 $pagesIds[$id] = $pageData; 110 if ($counter == $this->_resultsPerPage){ 111 break; 112 } 113 } 114 } 115 if (empty($pagesIds)){ 116 return false; 117 } 118 119 $pagesList = array(); 120 $body = array(); 121 $titleText = array(); 122 $category = array(); 123 foreach ($pagesIds as $crc => $data){ 124 if (empty($data['page'])){ 125 continue; 126 } 127 if (!empty($data['hid'])){ 128 $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 129 } else { 130 $bodyHtml = p_wiki_xhtml($data['page']); 131 } 132 $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 133 $bodyHtml = htmlspecialchars_decode($bodyHtml); 134 $body[$crc] = strip_tags($bodyHtml); 135 if(!empty($data['title_text'])){ 136 $titleText[$crc] = strip_tags($data['title_text']); 137 } else { 138 $titleText[$crc] = $data['page']; 139 } 140 $category[$crc] = $data['page']; 141 } 142 143 //$starQuery = $this->starQuery($keywords); 144 $bodyExcerpt = $this->getExcerpt($body, $keywords); 145 $titleTextExcerpt = $this->getExcerpt($titleText, $keywords); 146 $i = 0; 147 $results = array(); 148 foreach($body as $crc => $notused){ 149 $results[$crc] = array( 150 'page' => $pagesIds[$crc]['page'], 151 'bodyExcerpt' => $bodyExcerpt[$i], 152 'titleTextExcerpt' => $titleTextExcerpt[$i], 153 'hid' => $pagesIds[$crc]['hid'], 154 'title' => $pagesIds[$crc]['title'], 155 'title_text' => $pagesIds[$crc]['title_text'] 156 ); 157 $i++; 158 } 159 return $results; 160 } 161 162 public function getPagesIds() 163 { 164 $pageMapper = new PageMapper(); 165 166 return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 167 } 168 169 public function getOffset() 170 { 171 return $this->_offset; 172 } 173 174 public function getError() 175 { 176 return $this->_sphinx->GetLastError(); 177 } 178 179 public function getTotalFound() 180 { 181 return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 182 } 183 184 public function getExcerpt($data, $query) 185 { 186 return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 187 array( 188 'limit' => $this->_snippetSize, 189 'around' => $this->_aroundKeyword, 190 'weight_order' => 1, 191 'sp' => 1 192 ) 193 ); 194 } 195 196 public function starQuery($query) 197 { 198 $query = $this->removeStars($query); 199 $words = explode(" ", $query); 200 foreach($words as $id => $word){ 201 $words[$id] = "*".$word."*"; 202 } 203 return implode(" ", $words); 204 } 205 206 public function removeStars($query) 207 { 208 $words = explode(" ", $query); 209 foreach($words as $id => $word){ 210 $words[$id] = trim($word, "*"); 211 } 212 return implode(" ", $words); 213 } 214 215 public function getQuery() 216 { 217 return $this->_query; 218 } 219 220 public function setSnippetSize($symbols = 256) 221 { 222 $this->_snippetSize = $symbols; 223 } 224 225 public function setArroundWordsCount($words = 5) 226 { 227 $this->_aroundKeyword = $words; 228 } 229 230 public function setTitlePriority($priority) 231 { 232 $this->_titlePriority = $priority; 233 } 234 235 public function setBodyPriority($priority) 236 { 237 $this->_bodyPriority = $priority; 238 } 239 240 public function setNamespacePriority($priority) 241 { 242 $this->_namespacePriority = $priority; 243 } 244 245 public function setPagenamePriority($priority) 246 { 247 $this->_pagenamePriority = $priority; 248 } 249 250 private function _enableQuotesAndDefis($query) 251 { 252 $query = ' '. $query; 253 $quotesCount = count(explode('"', $query))-1; 254 if ($quotesCount && $quotesCount%2 == 0){ 255 $query = str_replace('\"', '"', $query); 256 } 257 $query = preg_replace("#\s\\\-(\w)#ui", " -$1", $query); 258 259 $query = substr($query, 1); 260 261 return $query; 262 } 263} 264