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