10Syaroslav@ivinco.com<?php 2124Syaroslav@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; 22124Syaroslav@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); 27124Syaroslav@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); 35124Syaroslav@ivinco.com $keywords = $this->_enableQuotesAndDefis($keywords); 3670Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 3780Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $starKeyword) | (@(body,title) {$keywords})"; 3869Syaroslav@ivinco.com } 3969Syaroslav@ivinco.com 4069Syaroslav@ivinco.com public function setSearchAllQueryWithCategoryFilter($keywords, $categories) 4119Syaroslav@ivinco.com { 4297Syaroslav@ivinco.com $keywords = $this->_sphinx->EscapeString($keywords); 43124Syaroslav@ivinco.com $keywords = $this->_enableQuotesAndDefis($keywords); 4469Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 4569Syaroslav@ivinco.com if(strpos($categories, "-") === 0){ 4669Syaroslav@ivinco.com $categories = '-"'.substr($categories, 1).'"'; 4769Syaroslav@ivinco.com } 4880Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) {$categories}) & ((@(body,title) {$keywords}) | (@(namespace,pagename) {$starKeyword}))"; 4969Syaroslav@ivinco.com } 5069Syaroslav@ivinco.com 5170Syaroslav@ivinco.com public function setSearchCategoryQuery($keywords, $categories) 5269Syaroslav@ivinco.com { 5397Syaroslav@ivinco.com $keywords = $this->_sphinx->EscapeString($keywords); 54124Syaroslav@ivinco.com $keywords = $this->_enableQuotesAndDefis($keywords); 55124Syaroslav@ivinco.com 5670Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 5772Syaroslav@ivinco.com if (!empty($categories)){ 5880Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $categories $starKeyword)"; 5972Syaroslav@ivinco.com } else { 6080Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $starKeyword)"; 6172Syaroslav@ivinco.com } 62124Syaroslav@ivinco.com 6369Syaroslav@ivinco.com } 64124Syaroslav@ivinco.com 65124Syaroslav@ivinco.com public function setSearchOnlyPagename() 66124Syaroslav@ivinco.com { 67124Syaroslav@ivinco.com $this->_query = "(@(pagename) {$this->_query})"; 68124Syaroslav@ivinco.com } 69124Syaroslav@ivinco.com 7069Syaroslav@ivinco.com public function search($start, $resultsPerPage = 10) 7169Syaroslav@ivinco.com { 7269Syaroslav@ivinco.com $this->_resultsPerPage = $resultsPerPage; 7369Syaroslav@ivinco.com 7480Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array( 7580Syaroslav@ivinco.com 'namespace' => $this->_namespacePriority, 7680Syaroslav@ivinco.com 'pagename' => $this->_pagenamePriority, 7780Syaroslav@ivinco.com 'title' => $this->_titlePriority, 7880Syaroslav@ivinco.com 'body' => $this->_bodyPriority) 7980Syaroslav@ivinco.com ); 8019Syaroslav@ivinco.com 81*133Sandrey $this->_sphinx->SetLimits($start, $resultsPerPage+100, 1000); 8269Syaroslav@ivinco.com 83*133Sandrey $this->_result = $this->_sphinx->Query($this->_query, $this->_index); 84119Syaroslav 85*133Sandrey if (empty($this->_result['matches'])) { 860Syaroslav@ivinco.com return false; 87*133Sandrey } 8869Syaroslav@ivinco.com return true; 8969Syaroslav@ivinco.com } 900Syaroslav@ivinco.com 9177Syaroslav@ivinco.com public function getPages($keywords) 9269Syaroslav@ivinco.com { 9392Syaroslav@ivinco.com if (empty($this->_result['matches'])) { 9492Syaroslav@ivinco.com return false; 95*133Sandrey } 96119Syaroslav 9769Syaroslav@ivinco.com $pagesIdsAll = $this->getPagesIds(); 9839Syaroslav@ivinco.com $this->_offset = 0; 9939Syaroslav@ivinco.com $counter = 0; 10043Syaroslav@ivinco.com $tmpRes = array(); 10143Syaroslav@ivinco.com $pagesIds = array(); 10239Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 10339Syaroslav@ivinco.com $this->_offset++; 10439Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 10543Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 10643Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 10743Syaroslav@ivinco.com $counter++; 108124Syaroslav@ivinco.com } 10939Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 11069Syaroslav@ivinco.com if ($counter == $this->_resultsPerPage){ 11139Syaroslav@ivinco.com break; 11239Syaroslav@ivinco.com } 113124Syaroslav@ivinco.com } 114124Syaroslav@ivinco.com } 115124Syaroslav@ivinco.com if (empty($pagesIds)){ 11627Syaroslav@ivinco.com return false; 11727Syaroslav@ivinco.com } 1180Syaroslav@ivinco.com 1190Syaroslav@ivinco.com $pagesList = array(); 12010Syaroslav@ivinco.com $body = array(); 12119Syaroslav@ivinco.com $titleText = array(); 12210Syaroslav@ivinco.com $category = array(); 12327Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 124124Syaroslav@ivinco.com if (empty($data['page'])){ 125124Syaroslav@ivinco.com continue; 126124Syaroslav@ivinco.com } 12727Syaroslav@ivinco.com if (!empty($data['hid'])){ 12827Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1296Syaroslav@ivinco.com } else { 13027Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1318Syaroslav@ivinco.com } 13260Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 13357Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 13410Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 13579Syaroslav@ivinco.com if(!empty($data['title_text'])){ 13679Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 13779Syaroslav@ivinco.com } else { 13879Syaroslav@ivinco.com $titleText[$crc] = $data['page']; 13979Syaroslav@ivinco.com } 14027Syaroslav@ivinco.com $category[$crc] = $data['page']; 14157Syaroslav@ivinco.com } 142124Syaroslav@ivinco.com 14380Syaroslav@ivinco.com //$starQuery = $this->starQuery($keywords); 14480Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $keywords); 14580Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $keywords); 1460Syaroslav@ivinco.com $i = 0; 1474Syaroslav@ivinco.com $results = array(); 14810Syaroslav@ivinco.com foreach($body as $crc => $notused){ 14910Syaroslav@ivinco.com $results[$crc] = array( 15010Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 15110Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 15219Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 15310Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 15419Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 15519Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 15610Syaroslav@ivinco.com ); 15710Syaroslav@ivinco.com $i++; 1580Syaroslav@ivinco.com } 1594Syaroslav@ivinco.com return $results; 1600Syaroslav@ivinco.com } 1610Syaroslav@ivinco.com 16269Syaroslav@ivinco.com public function getPagesIds() 16369Syaroslav@ivinco.com { 16469Syaroslav@ivinco.com $pageMapper = new PageMapper(); 16569Syaroslav@ivinco.com 16669Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 16769Syaroslav@ivinco.com } 16869Syaroslav@ivinco.com 16939Syaroslav@ivinco.com public function getOffset() 17039Syaroslav@ivinco.com { 17139Syaroslav@ivinco.com return $this->_offset; 17239Syaroslav@ivinco.com } 17339Syaroslav@ivinco.com 17428Syaroslav@ivinco.com public function getError() 17528Syaroslav@ivinco.com { 17628Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 17728Syaroslav@ivinco.com } 17828Syaroslav@ivinco.com 1790Syaroslav@ivinco.com public function getTotalFound() 1800Syaroslav@ivinco.com { 1810Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1820Syaroslav@ivinco.com } 18310Syaroslav@ivinco.com 18410Syaroslav@ivinco.com public function getExcerpt($data, $query) 18510Syaroslav@ivinco.com { 18616Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 18716Syaroslav@ivinco.com array( 18816Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 18939Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 19039Syaroslav@ivinco.com 'weight_order' => 1, 19139Syaroslav@ivinco.com 'sp' => 1 19216Syaroslav@ivinco.com ) 19316Syaroslav@ivinco.com ); 19410Syaroslav@ivinco.com } 19510Syaroslav@ivinco.com 19610Syaroslav@ivinco.com public function starQuery($query) 19710Syaroslav@ivinco.com { 19861Syaroslav@ivinco.com $query = $this->removeStars($query); 19910Syaroslav@ivinco.com $words = explode(" ", $query); 20061Syaroslav@ivinco.com foreach($words as $id => $word){ 20161Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 20210Syaroslav@ivinco.com } 20361Syaroslav@ivinco.com return implode(" ", $words); 20461Syaroslav@ivinco.com } 20561Syaroslav@ivinco.com 20661Syaroslav@ivinco.com public function removeStars($query) 20761Syaroslav@ivinco.com { 20861Syaroslav@ivinco.com $words = explode(" ", $query); 20961Syaroslav@ivinco.com foreach($words as $id => $word){ 21061Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 21161Syaroslav@ivinco.com } 21261Syaroslav@ivinco.com return implode(" ", $words); 21310Syaroslav@ivinco.com } 21415Syaroslav@ivinco.com 21515Syaroslav@ivinco.com public function getQuery() 21615Syaroslav@ivinco.com { 21715Syaroslav@ivinco.com return $this->_query; 21815Syaroslav@ivinco.com } 21916Syaroslav@ivinco.com 22016Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 22116Syaroslav@ivinco.com { 22216Syaroslav@ivinco.com $this->_snippetSize = $symbols; 22316Syaroslav@ivinco.com } 22416Syaroslav@ivinco.com 22516Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 22616Syaroslav@ivinco.com { 22716Syaroslav@ivinco.com $this->_aroundKeyword = $words; 22816Syaroslav@ivinco.com } 22919Syaroslav@ivinco.com 23019Syaroslav@ivinco.com public function setTitlePriority($priority) 23119Syaroslav@ivinco.com { 23219Syaroslav@ivinco.com $this->_titlePriority = $priority; 23319Syaroslav@ivinco.com } 23419Syaroslav@ivinco.com 23519Syaroslav@ivinco.com public function setBodyPriority($priority) 23619Syaroslav@ivinco.com { 23719Syaroslav@ivinco.com $this->_bodyPriority = $priority; 23819Syaroslav@ivinco.com } 23919Syaroslav@ivinco.com 24080Syaroslav@ivinco.com public function setNamespacePriority($priority) 24119Syaroslav@ivinco.com { 24280Syaroslav@ivinco.com $this->_namespacePriority = $priority; 24380Syaroslav@ivinco.com } 24480Syaroslav@ivinco.com 24580Syaroslav@ivinco.com public function setPagenamePriority($priority) 24680Syaroslav@ivinco.com { 24780Syaroslav@ivinco.com $this->_pagenamePriority = $priority; 24819Syaroslav@ivinco.com } 249124Syaroslav@ivinco.com 250124Syaroslav@ivinco.com private function _enableQuotesAndDefis($query) 251124Syaroslav@ivinco.com { 252124Syaroslav@ivinco.com $query = ' '. $query; 253124Syaroslav@ivinco.com $quotesCount = count(explode('"', $query))-1; 254124Syaroslav@ivinco.com if ($quotesCount && $quotesCount%2 == 0){ 255124Syaroslav@ivinco.com $query = str_replace('\"', '"', $query); 256124Syaroslav@ivinco.com } 257124Syaroslav@ivinco.com $query = preg_replace("#\s\\\-(\w)#ui", " -$1", $query); 258124Syaroslav@ivinco.com 259124Syaroslav@ivinco.com $query = substr($query, 1); 260124Syaroslav@ivinco.com 261124Syaroslav@ivinco.com return $query; 262124Syaroslav@ivinco.com } 2630Syaroslav@ivinco.com} 264