10Syaroslav@ivinco.com<?php 20Syaroslav@ivinco.com/* 30Syaroslav@ivinco.com * To change this template, choose Tools | Templates 40Syaroslav@ivinco.com * and open the template in the editor. 50Syaroslav@ivinco.com */ 60Syaroslav@ivinco.com 70Syaroslav@ivinco.comclass SphinxSearch 80Syaroslav@ivinco.com{ 90Syaroslav@ivinco.com private $_sphinx = null; 100Syaroslav@ivinco.com private $_result = array(); 110Syaroslav@ivinco.com private $_index = null; 1215Syaroslav@ivinco.com private $_query = ''; 1319Syaroslav@ivinco.com 1416Syaroslav@ivinco.com private $_snippetSize = 256; 1516Syaroslav@ivinco.com private $_aroundKeyword = 5; 1619Syaroslav@ivinco.com 1719Syaroslav@ivinco.com private $_titlePriority = 20; 1819Syaroslav@ivinco.com private $_bodyPriority = 5; 1919Syaroslav@ivinco.com private $_categoriesPriority = 10; 2016Syaroslav@ivinco.com 210Syaroslav@ivinco.com public function __construct($host, $port, $index) 220Syaroslav@ivinco.com { 230Syaroslav@ivinco.com $this->_sphinx = new SphinxClient(); 240Syaroslav@ivinco.com $this->_sphinx->SetServer($host, $port); 2519Syaroslav@ivinco.com $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 260Syaroslav@ivinco.com 270Syaroslav@ivinco.com $this->_index = $index; 280Syaroslav@ivinco.com } 290Syaroslav@ivinco.com 3015Syaroslav@ivinco.com public function search($keywords, $categories, $start, $resultsPerPage = 10) 3119Syaroslav@ivinco.com { 3219Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => $this->_categoriesPriority, 'title' => $this->_titlePriority, 'body' => $this->_bodyPriority)); 3319Syaroslav@ivinco.com 3427Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage+100); 3515Syaroslav@ivinco.com $query = ''; 3615Syaroslav@ivinco.com if (!empty($keywords) && empty($categories)){ 3739Syaroslav@ivinco.com $starCategory = $this->starQuery($keywords); 3841Syaroslav@ivinco.com $query = "(@(body,title) {$keywords} | (@categories {$starCategory}))"; 3941Syaroslav@ivinco.com //echo $query;exit; 4015Syaroslav@ivinco.com } else { 4139Syaroslav@ivinco.com $starCategory = $this->starQuery($categories); 4254Syaroslav@ivinco.com $query = "(@(body,title,categories) {$keywords} (@categories ".$starCategory."))"; 4315Syaroslav@ivinco.com } 4415Syaroslav@ivinco.com $this->_query = $query; 45*57Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 460Syaroslav@ivinco.com $this->_result = $res; 4728Syaroslav@ivinco.com 480Syaroslav@ivinco.com if (empty($res['matches'])) { 490Syaroslav@ivinco.com return false; 500Syaroslav@ivinco.com } 510Syaroslav@ivinco.com 520Syaroslav@ivinco.com $pageMapper = new PageMapper(); 530Syaroslav@ivinco.com 5439Syaroslav@ivinco.com $pagesIdsAll = $pageMapper->getByCrc(array_keys($res['matches'])); 5539Syaroslav@ivinco.com $this->_offset = 0; 5639Syaroslav@ivinco.com $counter = 0; 5743Syaroslav@ivinco.com $tmpRes = array(); 5843Syaroslav@ivinco.com $pagesIds = array(); 5939Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 6039Syaroslav@ivinco.com $this->_offset++; 6139Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 6243Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 6343Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 6443Syaroslav@ivinco.com $counter++; 6543Syaroslav@ivinco.com } 6639Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 6739Syaroslav@ivinco.com if ($counter == $resultsPerPage){ 6839Syaroslav@ivinco.com break; 6939Syaroslav@ivinco.com } 7039Syaroslav@ivinco.com } 7139Syaroslav@ivinco.com } 7239Syaroslav@ivinco.com if (empty($pagesIds)){ 7327Syaroslav@ivinco.com return false; 7427Syaroslav@ivinco.com } 750Syaroslav@ivinco.com 760Syaroslav@ivinco.com $pagesList = array(); 7710Syaroslav@ivinco.com $body = array(); 7819Syaroslav@ivinco.com $titleText = array(); 7910Syaroslav@ivinco.com $category = array(); 8027Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 8127Syaroslav@ivinco.com if (!empty($data['hid'])){ 8227Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 836Syaroslav@ivinco.com } else { 8427Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 858Syaroslav@ivinco.com } 86*57Syaroslav@ivinco.com $bodyHtml = preg_replace("#</li>#", "</li>;", $bodyHtml); 87*57Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 8810Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 8927Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 9027Syaroslav@ivinco.com $category[$crc] = $data['page']; 91*57Syaroslav@ivinco.com } 920Syaroslav@ivinco.com 9315Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 9410Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 9519Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 960Syaroslav@ivinco.com $i = 0; 974Syaroslav@ivinco.com $results = array(); 9810Syaroslav@ivinco.com foreach($body as $crc => $notused){ 9910Syaroslav@ivinco.com $results[$crc] = array( 10010Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 10110Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 10219Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 10310Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 10419Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 10519Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 10610Syaroslav@ivinco.com ); 10710Syaroslav@ivinco.com $i++; 1080Syaroslav@ivinco.com } 1094Syaroslav@ivinco.com return $results; 1100Syaroslav@ivinco.com } 1110Syaroslav@ivinco.com 11239Syaroslav@ivinco.com public function getOffset() 11339Syaroslav@ivinco.com { 11439Syaroslav@ivinco.com return $this->_offset; 11539Syaroslav@ivinco.com } 11639Syaroslav@ivinco.com 11728Syaroslav@ivinco.com public function getError() 11828Syaroslav@ivinco.com { 11928Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 12028Syaroslav@ivinco.com } 12128Syaroslav@ivinco.com 1220Syaroslav@ivinco.com public function getTotalFound() 1230Syaroslav@ivinco.com { 1240Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1250Syaroslav@ivinco.com } 12610Syaroslav@ivinco.com 12710Syaroslav@ivinco.com public function getExcerpt($data, $query) 12810Syaroslav@ivinco.com { 12916Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 13016Syaroslav@ivinco.com array( 13116Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 13239Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 13339Syaroslav@ivinco.com 'weight_order' => 1, 13439Syaroslav@ivinco.com 'sp' => 1 13516Syaroslav@ivinco.com ) 13616Syaroslav@ivinco.com ); 13710Syaroslav@ivinco.com } 13810Syaroslav@ivinco.com 13910Syaroslav@ivinco.com public function starQuery($query) 14010Syaroslav@ivinco.com { 14110Syaroslav@ivinco.com $words = explode(" ", $query); 14210Syaroslav@ivinco.com $starQuery = ''; 14310Syaroslav@ivinco.com foreach($words as $word){ 14440Syaroslav@ivinco.com $word = trim($word, "*"); 14510Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 14610Syaroslav@ivinco.com } 14710Syaroslav@ivinco.com return $starQuery; 14810Syaroslav@ivinco.com } 14915Syaroslav@ivinco.com 15015Syaroslav@ivinco.com public function getQuery() 15115Syaroslav@ivinco.com { 15215Syaroslav@ivinco.com return $this->_query; 15315Syaroslav@ivinco.com } 15416Syaroslav@ivinco.com 15516Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 15616Syaroslav@ivinco.com { 15716Syaroslav@ivinco.com $this->_snippetSize = $symbols; 15816Syaroslav@ivinco.com } 15916Syaroslav@ivinco.com 16016Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 16116Syaroslav@ivinco.com { 16216Syaroslav@ivinco.com $this->_aroundKeyword = $words; 16316Syaroslav@ivinco.com } 16419Syaroslav@ivinco.com 16519Syaroslav@ivinco.com public function setTitlePriority($priority) 16619Syaroslav@ivinco.com { 16719Syaroslav@ivinco.com $this->_titlePriority = $priority; 16819Syaroslav@ivinco.com } 16919Syaroslav@ivinco.com 17019Syaroslav@ivinco.com public function setBodyPriority($priority) 17119Syaroslav@ivinco.com { 17219Syaroslav@ivinco.com $this->_bodyPriority = $priority; 17319Syaroslav@ivinco.com } 17419Syaroslav@ivinco.com 17519Syaroslav@ivinco.com public function setCategoriesPriority($priority) 17619Syaroslav@ivinco.com { 17719Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 17819Syaroslav@ivinco.com } 1790Syaroslav@ivinco.com} 180