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 17 private $_titlePriority = 20; 18 private $_bodyPriority = 5; 19 private $_categoriesPriority = 10; 20 21 public function __construct($host, $port, $index) 22 { 23 $this->_sphinx = new SphinxClient(); 24 $this->_sphinx->SetServer($host, $port); 25 $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 26 27 $this->_index = $index; 28 } 29 30 public function search($keywords, $categories, $start, $resultsPerPage = 10) 31 { 32 $this->_sphinx->SetFieldWeights(array('categories' => $this->_categoriesPriority, 'title' => $this->_titlePriority, 'body' => $this->_bodyPriority)); 33 34 $this->_sphinx->SetLimits($start, $resultsPerPage+100); 35 $query = ''; 36 if (!empty($keywords) && empty($categories)){ 37 $starCategory = $this->starQuery($keywords); 38 $query = "@categories $starCategory | @(body,title) {$keywords}"; 39 } else { 40 $starKeyword = $this->starQuery($keywords); 41 if(strpos($categories, "-") == 0){ 42 $categories = '-"'.substr($categories, 1).'"'; 43 } 44 $query = "@categories ({$categories} {$starKeyword}) | @(body,title) {$keywords}"; 45 } 46 $this->_query = $query; 47 $res = $this->_sphinx->Query($query, $this->_index); 48 $this->_result = $res; 49 50 if (empty($res['matches'])) { 51 return false; 52 } 53 54 $pageMapper = new PageMapper(); 55 56 $pagesIdsAll = $pageMapper->getByCrc(array_keys($res['matches'])); 57 $this->_offset = 0; 58 $counter = 0; 59 $tmpRes = array(); 60 $pagesIds = array(); 61 foreach($pagesIdsAll as $id => $pageData){ 62 $this->_offset++; 63 if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 64 if(!isset($tmpRes[$pageData['page']])){ 65 $tmpRes[$pageData['page']] = 1; 66 $counter++; 67 } 68 $pagesIds[$id] = $pageData; 69 if ($counter == $resultsPerPage){ 70 break; 71 } 72 } 73 } 74 if (empty($pagesIds)){ 75 return false; 76 } 77 78 $pagesList = array(); 79 $body = array(); 80 $titleText = array(); 81 $category = array(); 82 foreach ($pagesIds as $crc => $data){ 83 if (!empty($data['hid'])){ 84 $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 85 } else { 86 $bodyHtml = p_wiki_xhtml($data['page']); 87 } 88 $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 89 $bodyHtml = htmlspecialchars_decode($bodyHtml); 90 $body[$crc] = strip_tags($bodyHtml); 91 $titleText[$crc] = strip_tags($data['title_text']); 92 $category[$crc] = $data['page']; 93 } 94 95 $starQuery = $this->starQuery($keywords); 96 $bodyExcerpt = $this->getExcerpt($body, $starQuery); 97 $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 98 $i = 0; 99 $results = array(); 100 foreach($body as $crc => $notused){ 101 $results[$crc] = array( 102 'page' => $pagesIds[$crc]['page'], 103 'bodyExcerpt' => $bodyExcerpt[$i], 104 'titleTextExcerpt' => $titleTextExcerpt[$i], 105 'hid' => $pagesIds[$crc]['hid'], 106 'title' => $pagesIds[$crc]['title'], 107 'title_text' => $pagesIds[$crc]['title_text'] 108 ); 109 $i++; 110 } 111 return $results; 112 } 113 114 public function getOffset() 115 { 116 return $this->_offset; 117 } 118 119 public function getError() 120 { 121 return $this->_sphinx->GetLastError(); 122 } 123 124 public function getTotalFound() 125 { 126 return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 127 } 128 129 public function getExcerpt($data, $query) 130 { 131 return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 132 array( 133 'limit' => $this->_snippetSize, 134 'around' => $this->_aroundKeyword, 135 'weight_order' => 1, 136 'sp' => 1 137 ) 138 ); 139 } 140 141 public function starQuery($query) 142 { 143 $query = $this->removeStars($query); 144 $words = explode(" ", $query); 145 foreach($words as $id => $word){ 146 $words[$id] = "*".$word."*"; 147 } 148 return implode(" ", $words); 149 } 150 151 public function removeStars($query) 152 { 153 $words = explode(" ", $query); 154 foreach($words as $id => $word){ 155 $words[$id] = trim($word, "*"); 156 } 157 return implode(" ", $words); 158 } 159 160 public function getQuery() 161 { 162 return $this->_query; 163 } 164 165 public function setSnippetSize($symbols = 256) 166 { 167 $this->_snippetSize = $symbols; 168 } 169 170 public function setArroundWordsCount($words = 5) 171 { 172 $this->_aroundKeyword = $words; 173 } 174 175 public function setTitlePriority($priority) 176 { 177 $this->_titlePriority = $priority; 178 } 179 180 public function setBodyPriority($priority) 181 { 182 $this->_bodyPriority = $priority; 183 } 184 185 public function setCategoriesPriority($priority) 186 { 187 $this->_categoriesPriority = $priority; 188 } 189} 190