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); 38*41Syaroslav@ivinco.com //$query = "@(body,title) {$keywords} | @categories {$starCategory}"; 39*41Syaroslav@ivinco.com $query = "(@(body,title) {$keywords} | (@categories {$starCategory}))"; 40*41Syaroslav@ivinco.com //echo $query;exit; 4115Syaroslav@ivinco.com } else { 4239Syaroslav@ivinco.com $starCategory = $this->starQuery($categories); 4339Syaroslav@ivinco.com $query = "@(body,title,categories) {$keywords} @categories ".$starCategory; 4415Syaroslav@ivinco.com } 4515Syaroslav@ivinco.com $this->_query = $query; 460Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 470Syaroslav@ivinco.com $this->_result = $res; 4828Syaroslav@ivinco.com 490Syaroslav@ivinco.com if (empty($res['matches'])) { 500Syaroslav@ivinco.com return false; 510Syaroslav@ivinco.com } 520Syaroslav@ivinco.com 530Syaroslav@ivinco.com $pageMapper = new PageMapper(); 540Syaroslav@ivinco.com 5539Syaroslav@ivinco.com $pagesIdsAll = $pageMapper->getByCrc(array_keys($res['matches'])); 5639Syaroslav@ivinco.com $this->_offset = 0; 5739Syaroslav@ivinco.com $counter = 0; 5839Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 5939Syaroslav@ivinco.com $this->_offset++; 6039Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 6139Syaroslav@ivinco.com $counter++; 6239Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 6339Syaroslav@ivinco.com if ($counter == $resultsPerPage){ 6439Syaroslav@ivinco.com break; 6539Syaroslav@ivinco.com } 6639Syaroslav@ivinco.com } 6739Syaroslav@ivinco.com } 6839Syaroslav@ivinco.com if (empty($pagesIds)){ 6927Syaroslav@ivinco.com return false; 7027Syaroslav@ivinco.com } 710Syaroslav@ivinco.com 720Syaroslav@ivinco.com $pagesList = array(); 7310Syaroslav@ivinco.com $body = array(); 7419Syaroslav@ivinco.com $titleText = array(); 7510Syaroslav@ivinco.com $category = array(); 7627Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 7727Syaroslav@ivinco.com if (!empty($data['hid'])){ 7827Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 796Syaroslav@ivinco.com } else { 8027Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 818Syaroslav@ivinco.com } 8210Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 8327Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 8427Syaroslav@ivinco.com $category[$crc] = $data['page']; 8518Syaroslav@ivinco.com } 860Syaroslav@ivinco.com 8715Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 8810Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 8919Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 900Syaroslav@ivinco.com $i = 0; 914Syaroslav@ivinco.com $results = array(); 9210Syaroslav@ivinco.com foreach($body as $crc => $notused){ 9310Syaroslav@ivinco.com $results[$crc] = array( 9410Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 9510Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 9619Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 9710Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 9819Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 9919Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 10010Syaroslav@ivinco.com ); 10110Syaroslav@ivinco.com $i++; 1020Syaroslav@ivinco.com } 1034Syaroslav@ivinco.com return $results; 1040Syaroslav@ivinco.com } 1050Syaroslav@ivinco.com 10639Syaroslav@ivinco.com public function getOffset() 10739Syaroslav@ivinco.com { 10839Syaroslav@ivinco.com return $this->_offset; 10939Syaroslav@ivinco.com } 11039Syaroslav@ivinco.com 11128Syaroslav@ivinco.com public function getError() 11228Syaroslav@ivinco.com { 11328Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 11428Syaroslav@ivinco.com } 11528Syaroslav@ivinco.com 1160Syaroslav@ivinco.com public function getTotalFound() 1170Syaroslav@ivinco.com { 1180Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1190Syaroslav@ivinco.com } 12010Syaroslav@ivinco.com 12110Syaroslav@ivinco.com public function getExcerpt($data, $query) 12210Syaroslav@ivinco.com { 12316Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 12416Syaroslav@ivinco.com array( 12516Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 12639Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 12739Syaroslav@ivinco.com 'weight_order' => 1, 12839Syaroslav@ivinco.com 'sp' => 1 12916Syaroslav@ivinco.com ) 13016Syaroslav@ivinco.com ); 13110Syaroslav@ivinco.com } 13210Syaroslav@ivinco.com 13310Syaroslav@ivinco.com public function starQuery($query) 13410Syaroslav@ivinco.com { 13510Syaroslav@ivinco.com $words = explode(" ", $query); 13610Syaroslav@ivinco.com $starQuery = ''; 13710Syaroslav@ivinco.com foreach($words as $word){ 13840Syaroslav@ivinco.com $word = trim($word, "*"); 13910Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 14010Syaroslav@ivinco.com } 14110Syaroslav@ivinco.com return $starQuery; 14210Syaroslav@ivinco.com } 14315Syaroslav@ivinco.com 14415Syaroslav@ivinco.com public function getQuery() 14515Syaroslav@ivinco.com { 14615Syaroslav@ivinco.com return $this->_query; 14715Syaroslav@ivinco.com } 14816Syaroslav@ivinco.com 14916Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 15016Syaroslav@ivinco.com { 15116Syaroslav@ivinco.com $this->_snippetSize = $symbols; 15216Syaroslav@ivinco.com } 15316Syaroslav@ivinco.com 15416Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 15516Syaroslav@ivinco.com { 15616Syaroslav@ivinco.com $this->_aroundKeyword = $words; 15716Syaroslav@ivinco.com } 15819Syaroslav@ivinco.com 15919Syaroslav@ivinco.com public function setTitlePriority($priority) 16019Syaroslav@ivinco.com { 16119Syaroslav@ivinco.com $this->_titlePriority = $priority; 16219Syaroslav@ivinco.com } 16319Syaroslav@ivinco.com 16419Syaroslav@ivinco.com public function setBodyPriority($priority) 16519Syaroslav@ivinco.com { 16619Syaroslav@ivinco.com $this->_bodyPriority = $priority; 16719Syaroslav@ivinco.com } 16819Syaroslav@ivinco.com 16919Syaroslav@ivinco.com public function setCategoriesPriority($priority) 17019Syaroslav@ivinco.com { 17119Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 17219Syaroslav@ivinco.com } 1730Syaroslav@ivinco.com} 174