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*60Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 39*60Syaroslav@ivinco.com $query = "(@(body,title) {$starKeyword} | (@categories {$starCategory}))"; 4041Syaroslav@ivinco.com //echo $query;exit; 4115Syaroslav@ivinco.com } else { 4239Syaroslav@ivinco.com $starCategory = $this->starQuery($categories); 43*60Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 44*60Syaroslav@ivinco.com $query = "(@(body,title,categories) {$starKeyword} (@categories ".$starCategory."))"; 4515Syaroslav@ivinco.com } 4615Syaroslav@ivinco.com $this->_query = $query; 4757Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 480Syaroslav@ivinco.com $this->_result = $res; 4928Syaroslav@ivinco.com 500Syaroslav@ivinco.com if (empty($res['matches'])) { 510Syaroslav@ivinco.com return false; 520Syaroslav@ivinco.com } 530Syaroslav@ivinco.com 540Syaroslav@ivinco.com $pageMapper = new PageMapper(); 550Syaroslav@ivinco.com 5639Syaroslav@ivinco.com $pagesIdsAll = $pageMapper->getByCrc(array_keys($res['matches'])); 5739Syaroslav@ivinco.com $this->_offset = 0; 5839Syaroslav@ivinco.com $counter = 0; 5943Syaroslav@ivinco.com $tmpRes = array(); 6043Syaroslav@ivinco.com $pagesIds = array(); 6139Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 6239Syaroslav@ivinco.com $this->_offset++; 6339Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 6443Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 6543Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 6643Syaroslav@ivinco.com $counter++; 6743Syaroslav@ivinco.com } 6839Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 6939Syaroslav@ivinco.com if ($counter == $resultsPerPage){ 7039Syaroslav@ivinco.com break; 7139Syaroslav@ivinco.com } 7239Syaroslav@ivinco.com } 7339Syaroslav@ivinco.com } 7439Syaroslav@ivinco.com if (empty($pagesIds)){ 7527Syaroslav@ivinco.com return false; 7627Syaroslav@ivinco.com } 770Syaroslav@ivinco.com 780Syaroslav@ivinco.com $pagesList = array(); 7910Syaroslav@ivinco.com $body = array(); 8019Syaroslav@ivinco.com $titleText = array(); 8110Syaroslav@ivinco.com $category = array(); 8227Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 8327Syaroslav@ivinco.com if (!empty($data['hid'])){ 8427Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 856Syaroslav@ivinco.com } else { 8627Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 878Syaroslav@ivinco.com } 88*60Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 8957Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 9010Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 9127Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 9227Syaroslav@ivinco.com $category[$crc] = $data['page']; 9357Syaroslav@ivinco.com } 940Syaroslav@ivinco.com 9515Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 9610Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 9719Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 980Syaroslav@ivinco.com $i = 0; 994Syaroslav@ivinco.com $results = array(); 10010Syaroslav@ivinco.com foreach($body as $crc => $notused){ 10110Syaroslav@ivinco.com $results[$crc] = array( 10210Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 10310Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 10419Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 10510Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 10619Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 10719Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 10810Syaroslav@ivinco.com ); 10910Syaroslav@ivinco.com $i++; 1100Syaroslav@ivinco.com } 1114Syaroslav@ivinco.com return $results; 1120Syaroslav@ivinco.com } 1130Syaroslav@ivinco.com 11439Syaroslav@ivinco.com public function getOffset() 11539Syaroslav@ivinco.com { 11639Syaroslav@ivinco.com return $this->_offset; 11739Syaroslav@ivinco.com } 11839Syaroslav@ivinco.com 11928Syaroslav@ivinco.com public function getError() 12028Syaroslav@ivinco.com { 12128Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 12228Syaroslav@ivinco.com } 12328Syaroslav@ivinco.com 1240Syaroslav@ivinco.com public function getTotalFound() 1250Syaroslav@ivinco.com { 1260Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1270Syaroslav@ivinco.com } 12810Syaroslav@ivinco.com 12910Syaroslav@ivinco.com public function getExcerpt($data, $query) 13010Syaroslav@ivinco.com { 13116Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 13216Syaroslav@ivinco.com array( 13316Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 13439Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 13539Syaroslav@ivinco.com 'weight_order' => 1, 13639Syaroslav@ivinco.com 'sp' => 1 13716Syaroslav@ivinco.com ) 13816Syaroslav@ivinco.com ); 13910Syaroslav@ivinco.com } 14010Syaroslav@ivinco.com 14110Syaroslav@ivinco.com public function starQuery($query) 14210Syaroslav@ivinco.com { 14310Syaroslav@ivinco.com $words = explode(" ", $query); 14410Syaroslav@ivinco.com $starQuery = ''; 14510Syaroslav@ivinco.com foreach($words as $word){ 14640Syaroslav@ivinco.com $word = trim($word, "*"); 14710Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 14810Syaroslav@ivinco.com } 14910Syaroslav@ivinco.com return $starQuery; 15010Syaroslav@ivinco.com } 15115Syaroslav@ivinco.com 15215Syaroslav@ivinco.com public function getQuery() 15315Syaroslav@ivinco.com { 15415Syaroslav@ivinco.com return $this->_query; 15515Syaroslav@ivinco.com } 15616Syaroslav@ivinco.com 15716Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 15816Syaroslav@ivinco.com { 15916Syaroslav@ivinco.com $this->_snippetSize = $symbols; 16016Syaroslav@ivinco.com } 16116Syaroslav@ivinco.com 16216Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 16316Syaroslav@ivinco.com { 16416Syaroslav@ivinco.com $this->_aroundKeyword = $words; 16516Syaroslav@ivinco.com } 16619Syaroslav@ivinco.com 16719Syaroslav@ivinco.com public function setTitlePriority($priority) 16819Syaroslav@ivinco.com { 16919Syaroslav@ivinco.com $this->_titlePriority = $priority; 17019Syaroslav@ivinco.com } 17119Syaroslav@ivinco.com 17219Syaroslav@ivinco.com public function setBodyPriority($priority) 17319Syaroslav@ivinco.com { 17419Syaroslav@ivinco.com $this->_bodyPriority = $priority; 17519Syaroslav@ivinco.com } 17619Syaroslav@ivinco.com 17719Syaroslav@ivinco.com public function setCategoriesPriority($priority) 17819Syaroslav@ivinco.com { 17919Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 18019Syaroslav@ivinco.com } 1810Syaroslav@ivinco.com} 182