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