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