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)){ 37*39Syaroslav@ivinco.com $starCategory = $this->starQuery($keywords); 38*39Syaroslav@ivinco.com $query = "@(body,title) {$keywords} | @categories {$starCategory}"; 3915Syaroslav@ivinco.com } else { 40*39Syaroslav@ivinco.com $starCategory = $this->starQuery($categories); 41*39Syaroslav@ivinco.com $query = "@(body,title,categories) {$keywords} @categories ".$starCategory; 4215Syaroslav@ivinco.com } 4315Syaroslav@ivinco.com $this->_query = $query; 440Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 450Syaroslav@ivinco.com $this->_result = $res; 4628Syaroslav@ivinco.com 470Syaroslav@ivinco.com if (empty($res['matches'])) { 480Syaroslav@ivinco.com return false; 490Syaroslav@ivinco.com } 500Syaroslav@ivinco.com 510Syaroslav@ivinco.com $pageMapper = new PageMapper(); 520Syaroslav@ivinco.com 53*39Syaroslav@ivinco.com $pagesIdsAll = $pageMapper->getByCrc(array_keys($res['matches'])); 54*39Syaroslav@ivinco.com $this->_offset = 0; 55*39Syaroslav@ivinco.com $counter = 0; 56*39Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 57*39Syaroslav@ivinco.com $this->_offset++; 58*39Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 59*39Syaroslav@ivinco.com $counter++; 60*39Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 61*39Syaroslav@ivinco.com if ($counter == $resultsPerPage){ 62*39Syaroslav@ivinco.com break; 63*39Syaroslav@ivinco.com } 64*39Syaroslav@ivinco.com } 65*39Syaroslav@ivinco.com } 66*39Syaroslav@ivinco.com if (empty($pagesIds)){ 6727Syaroslav@ivinco.com return false; 6827Syaroslav@ivinco.com } 690Syaroslav@ivinco.com 700Syaroslav@ivinco.com $pagesList = array(); 7110Syaroslav@ivinco.com $body = array(); 7219Syaroslav@ivinco.com $titleText = array(); 7310Syaroslav@ivinco.com $category = array(); 7427Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 7527Syaroslav@ivinco.com if (!empty($data['hid'])){ 7627Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 776Syaroslav@ivinco.com } else { 7827Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 798Syaroslav@ivinco.com } 8010Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 8127Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 8227Syaroslav@ivinco.com $category[$crc] = $data['page']; 8318Syaroslav@ivinco.com } 840Syaroslav@ivinco.com 8515Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 8610Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 8719Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 880Syaroslav@ivinco.com $i = 0; 894Syaroslav@ivinco.com $results = array(); 9010Syaroslav@ivinco.com foreach($body as $crc => $notused){ 9110Syaroslav@ivinco.com $results[$crc] = array( 9210Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 9310Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 9419Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 9510Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 9619Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 9719Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 9810Syaroslav@ivinco.com ); 9910Syaroslav@ivinco.com $i++; 1000Syaroslav@ivinco.com } 1014Syaroslav@ivinco.com return $results; 1020Syaroslav@ivinco.com } 1030Syaroslav@ivinco.com 104*39Syaroslav@ivinco.com public function getOffset() 105*39Syaroslav@ivinco.com { 106*39Syaroslav@ivinco.com return $this->_offset; 107*39Syaroslav@ivinco.com } 108*39Syaroslav@ivinco.com 10928Syaroslav@ivinco.com public function getError() 11028Syaroslav@ivinco.com { 11128Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 11228Syaroslav@ivinco.com } 11328Syaroslav@ivinco.com 1140Syaroslav@ivinco.com public function getTotalFound() 1150Syaroslav@ivinco.com { 1160Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1170Syaroslav@ivinco.com } 11810Syaroslav@ivinco.com 11910Syaroslav@ivinco.com public function getExcerpt($data, $query) 12010Syaroslav@ivinco.com { 12116Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 12216Syaroslav@ivinco.com array( 12316Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 124*39Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 125*39Syaroslav@ivinco.com 'weight_order' => 1, 126*39Syaroslav@ivinco.com 'sp' => 1 12716Syaroslav@ivinco.com ) 12816Syaroslav@ivinco.com ); 12910Syaroslav@ivinco.com } 13010Syaroslav@ivinco.com 13110Syaroslav@ivinco.com public function starQuery($query) 13210Syaroslav@ivinco.com { 13310Syaroslav@ivinco.com $words = explode(" ", $query); 13410Syaroslav@ivinco.com $starQuery = ''; 13510Syaroslav@ivinco.com foreach($words as $word){ 13610Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 13710Syaroslav@ivinco.com } 13810Syaroslav@ivinco.com return $starQuery; 13910Syaroslav@ivinco.com } 14015Syaroslav@ivinco.com 14115Syaroslav@ivinco.com public function getQuery() 14215Syaroslav@ivinco.com { 14315Syaroslav@ivinco.com return $this->_query; 14415Syaroslav@ivinco.com } 14516Syaroslav@ivinco.com 14616Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 14716Syaroslav@ivinco.com { 14816Syaroslav@ivinco.com $this->_snippetSize = $symbols; 14916Syaroslav@ivinco.com } 15016Syaroslav@ivinco.com 15116Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 15216Syaroslav@ivinco.com { 15316Syaroslav@ivinco.com $this->_aroundKeyword = $words; 15416Syaroslav@ivinco.com } 15519Syaroslav@ivinco.com 15619Syaroslav@ivinco.com public function setTitlePriority($priority) 15719Syaroslav@ivinco.com { 15819Syaroslav@ivinco.com $this->_titlePriority = $priority; 15919Syaroslav@ivinco.com } 16019Syaroslav@ivinco.com 16119Syaroslav@ivinco.com public function setBodyPriority($priority) 16219Syaroslav@ivinco.com { 16319Syaroslav@ivinco.com $this->_bodyPriority = $priority; 16419Syaroslav@ivinco.com } 16519Syaroslav@ivinco.com 16619Syaroslav@ivinco.com public function setCategoriesPriority($priority) 16719Syaroslav@ivinco.com { 16819Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 16919Syaroslav@ivinco.com } 1700Syaroslav@ivinco.com} 171