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