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; 1669Syaroslav@ivinco.com private $_resultsPerPage = 10; 1719Syaroslav@ivinco.com 1880Syaroslav@ivinco.com private $_titlePriority = 1; 1980Syaroslav@ivinco.com private $_bodyPriority = 1; 2080Syaroslav@ivinco.com private $_namespacePriority = 1; 2180Syaroslav@ivinco.com private $_pagenamePriority = 1; 2216Syaroslav@ivinco.com 230Syaroslav@ivinco.com public function __construct($host, $port, $index) 240Syaroslav@ivinco.com { 250Syaroslav@ivinco.com $this->_sphinx = new SphinxClient(); 260Syaroslav@ivinco.com $this->_sphinx->SetServer($host, $port); 2719Syaroslav@ivinco.com $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 280Syaroslav@ivinco.com 290Syaroslav@ivinco.com $this->_index = $index; 300Syaroslav@ivinco.com } 310Syaroslav@ivinco.com 3269Syaroslav@ivinco.com public function setSearchAllQuery($keywords, $categories) 3369Syaroslav@ivinco.com { 34*97Syaroslav@ivinco.com $keywords = $this->_sphinx->EscapeString($keywords); 3570Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 3680Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $starKeyword) | (@(body,title) {$keywords})"; 3769Syaroslav@ivinco.com } 3869Syaroslav@ivinco.com 3969Syaroslav@ivinco.com public function setSearchAllQueryWithCategoryFilter($keywords, $categories) 4019Syaroslav@ivinco.com { 41*97Syaroslav@ivinco.com $keywords = $this->_sphinx->EscapeString($keywords); 4269Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 4369Syaroslav@ivinco.com if(strpos($categories, "-") === 0){ 4469Syaroslav@ivinco.com $categories = '-"'.substr($categories, 1).'"'; 4569Syaroslav@ivinco.com } 4680Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) {$categories}) & ((@(body,title) {$keywords}) | (@(namespace,pagename) {$starKeyword}))"; 4769Syaroslav@ivinco.com } 4869Syaroslav@ivinco.com 4970Syaroslav@ivinco.com public function setSearchCategoryQuery($keywords, $categories) 5069Syaroslav@ivinco.com { 51*97Syaroslav@ivinco.com $keywords = $this->_sphinx->EscapeString($keywords); 5270Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 5372Syaroslav@ivinco.com if (!empty($categories)){ 5480Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $categories $starKeyword)"; 5572Syaroslav@ivinco.com } else { 5680Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $starKeyword)"; 5772Syaroslav@ivinco.com } 5883Syaroslav@ivinco.com 5969Syaroslav@ivinco.com } 6069Syaroslav@ivinco.com 6169Syaroslav@ivinco.com public function search($start, $resultsPerPage = 10) 6269Syaroslav@ivinco.com { 6369Syaroslav@ivinco.com $this->_resultsPerPage = $resultsPerPage; 6469Syaroslav@ivinco.com 6580Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array( 6680Syaroslav@ivinco.com 'namespace' => $this->_namespacePriority, 6780Syaroslav@ivinco.com 'pagename' => $this->_pagenamePriority, 6880Syaroslav@ivinco.com 'title' => $this->_titlePriority, 6980Syaroslav@ivinco.com 'body' => $this->_bodyPriority) 7080Syaroslav@ivinco.com ); 7119Syaroslav@ivinco.com 7227Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage+100); 7369Syaroslav@ivinco.com 7469Syaroslav@ivinco.com $res = $this->_sphinx->Query($this->_query, $this->_index); 750Syaroslav@ivinco.com $this->_result = $res; 7628Syaroslav@ivinco.com 770Syaroslav@ivinco.com if (empty($res['matches'])) { 780Syaroslav@ivinco.com return false; 790Syaroslav@ivinco.com } 8069Syaroslav@ivinco.com return true; 8169Syaroslav@ivinco.com } 820Syaroslav@ivinco.com 8377Syaroslav@ivinco.com public function getPages($keywords) 8469Syaroslav@ivinco.com { 8592Syaroslav@ivinco.com if (empty($this->_result['matches'])) { 8692Syaroslav@ivinco.com return false; 8792Syaroslav@ivinco.com } 8869Syaroslav@ivinco.com $pagesIdsAll = $this->getPagesIds(); 8939Syaroslav@ivinco.com $this->_offset = 0; 9039Syaroslav@ivinco.com $counter = 0; 9143Syaroslav@ivinco.com $tmpRes = array(); 9243Syaroslav@ivinco.com $pagesIds = array(); 9339Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 9439Syaroslav@ivinco.com $this->_offset++; 9539Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 9643Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 9743Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 9843Syaroslav@ivinco.com $counter++; 9943Syaroslav@ivinco.com } 10039Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 10169Syaroslav@ivinco.com if ($counter == $this->_resultsPerPage){ 10239Syaroslav@ivinco.com break; 10339Syaroslav@ivinco.com } 10439Syaroslav@ivinco.com } 10539Syaroslav@ivinco.com } 10639Syaroslav@ivinco.com if (empty($pagesIds)){ 10727Syaroslav@ivinco.com return false; 10827Syaroslav@ivinco.com } 1090Syaroslav@ivinco.com 1100Syaroslav@ivinco.com $pagesList = array(); 11110Syaroslav@ivinco.com $body = array(); 11219Syaroslav@ivinco.com $titleText = array(); 11310Syaroslav@ivinco.com $category = array(); 11427Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 11527Syaroslav@ivinco.com if (!empty($data['hid'])){ 11627Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1176Syaroslav@ivinco.com } else { 11827Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1198Syaroslav@ivinco.com } 12060Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 12157Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 12210Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 12379Syaroslav@ivinco.com if(!empty($data['title_text'])){ 12479Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 12579Syaroslav@ivinco.com } else { 12679Syaroslav@ivinco.com $titleText[$crc] = $data['page']; 12779Syaroslav@ivinco.com } 12827Syaroslav@ivinco.com $category[$crc] = $data['page']; 12957Syaroslav@ivinco.com } 1300Syaroslav@ivinco.com 13180Syaroslav@ivinco.com //$starQuery = $this->starQuery($keywords); 13280Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $keywords); 13380Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $keywords); 1340Syaroslav@ivinco.com $i = 0; 1354Syaroslav@ivinco.com $results = array(); 13610Syaroslav@ivinco.com foreach($body as $crc => $notused){ 13710Syaroslav@ivinco.com $results[$crc] = array( 13810Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 13910Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 14019Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 14110Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 14219Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 14319Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 14410Syaroslav@ivinco.com ); 14510Syaroslav@ivinco.com $i++; 1460Syaroslav@ivinco.com } 1474Syaroslav@ivinco.com return $results; 1480Syaroslav@ivinco.com } 1490Syaroslav@ivinco.com 15069Syaroslav@ivinco.com public function getPagesIds() 15169Syaroslav@ivinco.com { 15269Syaroslav@ivinco.com $pageMapper = new PageMapper(); 15369Syaroslav@ivinco.com 15469Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 15569Syaroslav@ivinco.com } 15669Syaroslav@ivinco.com 15739Syaroslav@ivinco.com public function getOffset() 15839Syaroslav@ivinco.com { 15939Syaroslav@ivinco.com return $this->_offset; 16039Syaroslav@ivinco.com } 16139Syaroslav@ivinco.com 16228Syaroslav@ivinco.com public function getError() 16328Syaroslav@ivinco.com { 16428Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 16528Syaroslav@ivinco.com } 16628Syaroslav@ivinco.com 1670Syaroslav@ivinco.com public function getTotalFound() 1680Syaroslav@ivinco.com { 1690Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1700Syaroslav@ivinco.com } 17110Syaroslav@ivinco.com 17210Syaroslav@ivinco.com public function getExcerpt($data, $query) 17310Syaroslav@ivinco.com { 17416Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 17516Syaroslav@ivinco.com array( 17616Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 17739Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 17839Syaroslav@ivinco.com 'weight_order' => 1, 17939Syaroslav@ivinco.com 'sp' => 1 18016Syaroslav@ivinco.com ) 18116Syaroslav@ivinco.com ); 18210Syaroslav@ivinco.com } 18310Syaroslav@ivinco.com 18410Syaroslav@ivinco.com public function starQuery($query) 18510Syaroslav@ivinco.com { 18661Syaroslav@ivinco.com $query = $this->removeStars($query); 18710Syaroslav@ivinco.com $words = explode(" ", $query); 18861Syaroslav@ivinco.com foreach($words as $id => $word){ 18961Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 19010Syaroslav@ivinco.com } 19161Syaroslav@ivinco.com return implode(" ", $words); 19261Syaroslav@ivinco.com } 19361Syaroslav@ivinco.com 19461Syaroslav@ivinco.com public function removeStars($query) 19561Syaroslav@ivinco.com { 19661Syaroslav@ivinco.com $words = explode(" ", $query); 19761Syaroslav@ivinco.com foreach($words as $id => $word){ 19861Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 19961Syaroslav@ivinco.com } 20061Syaroslav@ivinco.com return implode(" ", $words); 20110Syaroslav@ivinco.com } 20215Syaroslav@ivinco.com 20315Syaroslav@ivinco.com public function getQuery() 20415Syaroslav@ivinco.com { 20515Syaroslav@ivinco.com return $this->_query; 20615Syaroslav@ivinco.com } 20716Syaroslav@ivinco.com 20816Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 20916Syaroslav@ivinco.com { 21016Syaroslav@ivinco.com $this->_snippetSize = $symbols; 21116Syaroslav@ivinco.com } 21216Syaroslav@ivinco.com 21316Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 21416Syaroslav@ivinco.com { 21516Syaroslav@ivinco.com $this->_aroundKeyword = $words; 21616Syaroslav@ivinco.com } 21719Syaroslav@ivinco.com 21819Syaroslav@ivinco.com public function setTitlePriority($priority) 21919Syaroslav@ivinco.com { 22019Syaroslav@ivinco.com $this->_titlePriority = $priority; 22119Syaroslav@ivinco.com } 22219Syaroslav@ivinco.com 22319Syaroslav@ivinco.com public function setBodyPriority($priority) 22419Syaroslav@ivinco.com { 22519Syaroslav@ivinco.com $this->_bodyPriority = $priority; 22619Syaroslav@ivinco.com } 22719Syaroslav@ivinco.com 22880Syaroslav@ivinco.com public function setNamespacePriority($priority) 22919Syaroslav@ivinco.com { 23080Syaroslav@ivinco.com $this->_namespacePriority = $priority; 23180Syaroslav@ivinco.com } 23280Syaroslav@ivinco.com 23380Syaroslav@ivinco.com public function setPagenamePriority($priority) 23480Syaroslav@ivinco.com { 23580Syaroslav@ivinco.com $this->_pagenamePriority = $priority; 23619Syaroslav@ivinco.com } 2370Syaroslav@ivinco.com} 238