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