10Syaroslav@ivinco.com<?php 2*124Syaroslav@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; 22*124Syaroslav@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); 27*124Syaroslav@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); 35*124Syaroslav@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); 43*124Syaroslav@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); 54*124Syaroslav@ivinco.com $keywords = $this->_enableQuotesAndDefis($keywords); 55*124Syaroslav@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 } 62*124Syaroslav@ivinco.com 6369Syaroslav@ivinco.com } 64*124Syaroslav@ivinco.com 65*124Syaroslav@ivinco.com public function setSearchOnlyPagename() 66*124Syaroslav@ivinco.com { 67*124Syaroslav@ivinco.com $this->_query = "(@(pagename) {$this->_query})"; 68*124Syaroslav@ivinco.com } 69*124Syaroslav@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 8127Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage+100); 8269Syaroslav@ivinco.com 8369Syaroslav@ivinco.com $res = $this->_sphinx->Query($this->_query, $this->_index); 84119Syaroslav 850Syaroslav@ivinco.com $this->_result = $res; 8628Syaroslav@ivinco.com 870Syaroslav@ivinco.com if (empty($res['matches'])) { 880Syaroslav@ivinco.com return false; 890Syaroslav@ivinco.com } 9069Syaroslav@ivinco.com return true; 9169Syaroslav@ivinco.com } 920Syaroslav@ivinco.com 9377Syaroslav@ivinco.com public function getPages($keywords) 9469Syaroslav@ivinco.com { 9592Syaroslav@ivinco.com if (empty($this->_result['matches'])) { 9692Syaroslav@ivinco.com return false; 9792Syaroslav@ivinco.com } 98119Syaroslav 9969Syaroslav@ivinco.com $pagesIdsAll = $this->getPagesIds(); 10039Syaroslav@ivinco.com $this->_offset = 0; 10139Syaroslav@ivinco.com $counter = 0; 10243Syaroslav@ivinco.com $tmpRes = array(); 10343Syaroslav@ivinco.com $pagesIds = array(); 10439Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 10539Syaroslav@ivinco.com $this->_offset++; 10639Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 10743Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 10843Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 10943Syaroslav@ivinco.com $counter++; 110*124Syaroslav@ivinco.com } 11139Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 11269Syaroslav@ivinco.com if ($counter == $this->_resultsPerPage){ 11339Syaroslav@ivinco.com break; 11439Syaroslav@ivinco.com } 115*124Syaroslav@ivinco.com } 116*124Syaroslav@ivinco.com } 117*124Syaroslav@ivinco.com if (empty($pagesIds)){ 11827Syaroslav@ivinco.com return false; 11927Syaroslav@ivinco.com } 1200Syaroslav@ivinco.com 1210Syaroslav@ivinco.com $pagesList = array(); 12210Syaroslav@ivinco.com $body = array(); 12319Syaroslav@ivinco.com $titleText = array(); 12410Syaroslav@ivinco.com $category = array(); 12527Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 126*124Syaroslav@ivinco.com if (empty($data['page'])){ 127*124Syaroslav@ivinco.com continue; 128*124Syaroslav@ivinco.com } 12927Syaroslav@ivinco.com if (!empty($data['hid'])){ 13027Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1316Syaroslav@ivinco.com } else { 13227Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1338Syaroslav@ivinco.com } 13460Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 13557Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 13610Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 13779Syaroslav@ivinco.com if(!empty($data['title_text'])){ 13879Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 13979Syaroslav@ivinco.com } else { 14079Syaroslav@ivinco.com $titleText[$crc] = $data['page']; 14179Syaroslav@ivinco.com } 14227Syaroslav@ivinco.com $category[$crc] = $data['page']; 14357Syaroslav@ivinco.com } 144*124Syaroslav@ivinco.com 14580Syaroslav@ivinco.com //$starQuery = $this->starQuery($keywords); 14680Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $keywords); 14780Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $keywords); 1480Syaroslav@ivinco.com $i = 0; 1494Syaroslav@ivinco.com $results = array(); 15010Syaroslav@ivinco.com foreach($body as $crc => $notused){ 15110Syaroslav@ivinco.com $results[$crc] = array( 15210Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 15310Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 15419Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 15510Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 15619Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 15719Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 15810Syaroslav@ivinco.com ); 15910Syaroslav@ivinco.com $i++; 1600Syaroslav@ivinco.com } 1614Syaroslav@ivinco.com return $results; 1620Syaroslav@ivinco.com } 1630Syaroslav@ivinco.com 16469Syaroslav@ivinco.com public function getPagesIds() 16569Syaroslav@ivinco.com { 16669Syaroslav@ivinco.com $pageMapper = new PageMapper(); 16769Syaroslav@ivinco.com 16869Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 16969Syaroslav@ivinco.com } 17069Syaroslav@ivinco.com 17139Syaroslav@ivinco.com public function getOffset() 17239Syaroslav@ivinco.com { 17339Syaroslav@ivinco.com return $this->_offset; 17439Syaroslav@ivinco.com } 17539Syaroslav@ivinco.com 17628Syaroslav@ivinco.com public function getError() 17728Syaroslav@ivinco.com { 17828Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 17928Syaroslav@ivinco.com } 18028Syaroslav@ivinco.com 1810Syaroslav@ivinco.com public function getTotalFound() 1820Syaroslav@ivinco.com { 1830Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1840Syaroslav@ivinco.com } 18510Syaroslav@ivinco.com 18610Syaroslav@ivinco.com public function getExcerpt($data, $query) 18710Syaroslav@ivinco.com { 18816Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 18916Syaroslav@ivinco.com array( 19016Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 19139Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 19239Syaroslav@ivinco.com 'weight_order' => 1, 19339Syaroslav@ivinco.com 'sp' => 1 19416Syaroslav@ivinco.com ) 19516Syaroslav@ivinco.com ); 19610Syaroslav@ivinco.com } 19710Syaroslav@ivinco.com 19810Syaroslav@ivinco.com public function starQuery($query) 19910Syaroslav@ivinco.com { 20061Syaroslav@ivinco.com $query = $this->removeStars($query); 20110Syaroslav@ivinco.com $words = explode(" ", $query); 20261Syaroslav@ivinco.com foreach($words as $id => $word){ 20361Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 20410Syaroslav@ivinco.com } 20561Syaroslav@ivinco.com return implode(" ", $words); 20661Syaroslav@ivinco.com } 20761Syaroslav@ivinco.com 20861Syaroslav@ivinco.com public function removeStars($query) 20961Syaroslav@ivinco.com { 21061Syaroslav@ivinco.com $words = explode(" ", $query); 21161Syaroslav@ivinco.com foreach($words as $id => $word){ 21261Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 21361Syaroslav@ivinco.com } 21461Syaroslav@ivinco.com return implode(" ", $words); 21510Syaroslav@ivinco.com } 21615Syaroslav@ivinco.com 21715Syaroslav@ivinco.com public function getQuery() 21815Syaroslav@ivinco.com { 21915Syaroslav@ivinco.com return $this->_query; 22015Syaroslav@ivinco.com } 22116Syaroslav@ivinco.com 22216Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 22316Syaroslav@ivinco.com { 22416Syaroslav@ivinco.com $this->_snippetSize = $symbols; 22516Syaroslav@ivinco.com } 22616Syaroslav@ivinco.com 22716Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 22816Syaroslav@ivinco.com { 22916Syaroslav@ivinco.com $this->_aroundKeyword = $words; 23016Syaroslav@ivinco.com } 23119Syaroslav@ivinco.com 23219Syaroslav@ivinco.com public function setTitlePriority($priority) 23319Syaroslav@ivinco.com { 23419Syaroslav@ivinco.com $this->_titlePriority = $priority; 23519Syaroslav@ivinco.com } 23619Syaroslav@ivinco.com 23719Syaroslav@ivinco.com public function setBodyPriority($priority) 23819Syaroslav@ivinco.com { 23919Syaroslav@ivinco.com $this->_bodyPriority = $priority; 24019Syaroslav@ivinco.com } 24119Syaroslav@ivinco.com 24280Syaroslav@ivinco.com public function setNamespacePriority($priority) 24319Syaroslav@ivinco.com { 24480Syaroslav@ivinco.com $this->_namespacePriority = $priority; 24580Syaroslav@ivinco.com } 24680Syaroslav@ivinco.com 24780Syaroslav@ivinco.com public function setPagenamePriority($priority) 24880Syaroslav@ivinco.com { 24980Syaroslav@ivinco.com $this->_pagenamePriority = $priority; 25019Syaroslav@ivinco.com } 251*124Syaroslav@ivinco.com 252*124Syaroslav@ivinco.com private function _enableQuotesAndDefis($query) 253*124Syaroslav@ivinco.com { 254*124Syaroslav@ivinco.com $query = ' '. $query; 255*124Syaroslav@ivinco.com $quotesCount = count(explode('"', $query))-1; 256*124Syaroslav@ivinco.com if ($quotesCount && $quotesCount%2 == 0){ 257*124Syaroslav@ivinco.com $query = str_replace('\"', '"', $query); 258*124Syaroslav@ivinco.com } 259*124Syaroslav@ivinco.com $query = preg_replace("#\s\\\-(\w)#ui", " -$1", $query); 260*124Syaroslav@ivinco.com 261*124Syaroslav@ivinco.com $query = substr($query, 1); 262*124Syaroslav@ivinco.com 263*124Syaroslav@ivinco.com return $query; 264*124Syaroslav@ivinco.com } 2650Syaroslav@ivinco.com} 266