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 { 3497Syaroslav@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 { 4197Syaroslav@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 { 5197Syaroslav@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); 75*119Syaroslav 760Syaroslav@ivinco.com $this->_result = $res; 7728Syaroslav@ivinco.com 780Syaroslav@ivinco.com if (empty($res['matches'])) { 790Syaroslav@ivinco.com return false; 800Syaroslav@ivinco.com } 8169Syaroslav@ivinco.com return true; 8269Syaroslav@ivinco.com } 830Syaroslav@ivinco.com 8477Syaroslav@ivinco.com public function getPages($keywords) 8569Syaroslav@ivinco.com { 8692Syaroslav@ivinco.com if (empty($this->_result['matches'])) { 8792Syaroslav@ivinco.com return false; 8892Syaroslav@ivinco.com } 89*119Syaroslav 9069Syaroslav@ivinco.com $pagesIdsAll = $this->getPagesIds(); 9139Syaroslav@ivinco.com $this->_offset = 0; 9239Syaroslav@ivinco.com $counter = 0; 9343Syaroslav@ivinco.com $tmpRes = array(); 9443Syaroslav@ivinco.com $pagesIds = array(); 9539Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 9639Syaroslav@ivinco.com $this->_offset++; 9739Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 9843Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 9943Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 10043Syaroslav@ivinco.com $counter++; 10143Syaroslav@ivinco.com } 10239Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 10369Syaroslav@ivinco.com if ($counter == $this->_resultsPerPage){ 10439Syaroslav@ivinco.com break; 10539Syaroslav@ivinco.com } 10639Syaroslav@ivinco.com } 10739Syaroslav@ivinco.com } 10839Syaroslav@ivinco.com if (empty($pagesIds)){ 10927Syaroslav@ivinco.com return false; 11027Syaroslav@ivinco.com } 1110Syaroslav@ivinco.com 1120Syaroslav@ivinco.com $pagesList = array(); 11310Syaroslav@ivinco.com $body = array(); 11419Syaroslav@ivinco.com $titleText = array(); 11510Syaroslav@ivinco.com $category = array(); 11627Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 11727Syaroslav@ivinco.com if (!empty($data['hid'])){ 11827Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1196Syaroslav@ivinco.com } else { 12027Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1218Syaroslav@ivinco.com } 12260Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 12357Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 12410Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 12579Syaroslav@ivinco.com if(!empty($data['title_text'])){ 12679Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 12779Syaroslav@ivinco.com } else { 12879Syaroslav@ivinco.com $titleText[$crc] = $data['page']; 12979Syaroslav@ivinco.com } 13027Syaroslav@ivinco.com $category[$crc] = $data['page']; 13157Syaroslav@ivinco.com } 1320Syaroslav@ivinco.com 13380Syaroslav@ivinco.com //$starQuery = $this->starQuery($keywords); 13480Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $keywords); 13580Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $keywords); 1360Syaroslav@ivinco.com $i = 0; 1374Syaroslav@ivinco.com $results = array(); 13810Syaroslav@ivinco.com foreach($body as $crc => $notused){ 13910Syaroslav@ivinco.com $results[$crc] = array( 14010Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 14110Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 14219Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 14310Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 14419Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 14519Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 14610Syaroslav@ivinco.com ); 14710Syaroslav@ivinco.com $i++; 1480Syaroslav@ivinco.com } 1494Syaroslav@ivinco.com return $results; 1500Syaroslav@ivinco.com } 1510Syaroslav@ivinco.com 15269Syaroslav@ivinco.com public function getPagesIds() 15369Syaroslav@ivinco.com { 15469Syaroslav@ivinco.com $pageMapper = new PageMapper(); 15569Syaroslav@ivinco.com 15669Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 15769Syaroslav@ivinco.com } 15869Syaroslav@ivinco.com 15939Syaroslav@ivinco.com public function getOffset() 16039Syaroslav@ivinco.com { 16139Syaroslav@ivinco.com return $this->_offset; 16239Syaroslav@ivinco.com } 16339Syaroslav@ivinco.com 16428Syaroslav@ivinco.com public function getError() 16528Syaroslav@ivinco.com { 16628Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 16728Syaroslav@ivinco.com } 16828Syaroslav@ivinco.com 1690Syaroslav@ivinco.com public function getTotalFound() 1700Syaroslav@ivinco.com { 1710Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1720Syaroslav@ivinco.com } 17310Syaroslav@ivinco.com 17410Syaroslav@ivinco.com public function getExcerpt($data, $query) 17510Syaroslav@ivinco.com { 17616Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 17716Syaroslav@ivinco.com array( 17816Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 17939Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 18039Syaroslav@ivinco.com 'weight_order' => 1, 18139Syaroslav@ivinco.com 'sp' => 1 18216Syaroslav@ivinco.com ) 18316Syaroslav@ivinco.com ); 18410Syaroslav@ivinco.com } 18510Syaroslav@ivinco.com 18610Syaroslav@ivinco.com public function starQuery($query) 18710Syaroslav@ivinco.com { 18861Syaroslav@ivinco.com $query = $this->removeStars($query); 18910Syaroslav@ivinco.com $words = explode(" ", $query); 19061Syaroslav@ivinco.com foreach($words as $id => $word){ 19161Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 19210Syaroslav@ivinco.com } 19361Syaroslav@ivinco.com return implode(" ", $words); 19461Syaroslav@ivinco.com } 19561Syaroslav@ivinco.com 19661Syaroslav@ivinco.com public function removeStars($query) 19761Syaroslav@ivinco.com { 19861Syaroslav@ivinco.com $words = explode(" ", $query); 19961Syaroslav@ivinco.com foreach($words as $id => $word){ 20061Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 20161Syaroslav@ivinco.com } 20261Syaroslav@ivinco.com return implode(" ", $words); 20310Syaroslav@ivinco.com } 20415Syaroslav@ivinco.com 20515Syaroslav@ivinco.com public function getQuery() 20615Syaroslav@ivinco.com { 20715Syaroslav@ivinco.com return $this->_query; 20815Syaroslav@ivinco.com } 20916Syaroslav@ivinco.com 21016Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 21116Syaroslav@ivinco.com { 21216Syaroslav@ivinco.com $this->_snippetSize = $symbols; 21316Syaroslav@ivinco.com } 21416Syaroslav@ivinco.com 21516Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 21616Syaroslav@ivinco.com { 21716Syaroslav@ivinco.com $this->_aroundKeyword = $words; 21816Syaroslav@ivinco.com } 21919Syaroslav@ivinco.com 22019Syaroslav@ivinco.com public function setTitlePriority($priority) 22119Syaroslav@ivinco.com { 22219Syaroslav@ivinco.com $this->_titlePriority = $priority; 22319Syaroslav@ivinco.com } 22419Syaroslav@ivinco.com 22519Syaroslav@ivinco.com public function setBodyPriority($priority) 22619Syaroslav@ivinco.com { 22719Syaroslav@ivinco.com $this->_bodyPriority = $priority; 22819Syaroslav@ivinco.com } 22919Syaroslav@ivinco.com 23080Syaroslav@ivinco.com public function setNamespacePriority($priority) 23119Syaroslav@ivinco.com { 23280Syaroslav@ivinco.com $this->_namespacePriority = $priority; 23380Syaroslav@ivinco.com } 23480Syaroslav@ivinco.com 23580Syaroslav@ivinco.com public function setPagenamePriority($priority) 23680Syaroslav@ivinco.com { 23780Syaroslav@ivinco.com $this->_pagenamePriority = $priority; 23819Syaroslav@ivinco.com } 2390Syaroslav@ivinco.com} 240