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 81133Sandrey $this->_sphinx->SetLimits($start, $resultsPerPage+100, 1000); 8269Syaroslav@ivinco.com 83133Sandrey $this->_result = $this->_sphinx->Query($this->_query, $this->_index); 84119Syaroslav 85133Sandrey if (empty($this->_result['matches'])) { 860Syaroslav@ivinco.com return false; 87133Sandrey } 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; 95133Sandrey } 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 } 142*134Sandrey 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 174*134Sandrey public function getPageNames() 175*134Sandrey { 176*134Sandrey $pageIds = $this->getPagesIds(); 177*134Sandrey 178*134Sandrey $matchPages = array(); 179*134Sandrey foreach ($pageIds as $page) { 180*134Sandrey if(auth_quickaclcheck($page['page']) < AUTH_READ){ 181*134Sandrey continue; 182*134Sandrey } 183*134Sandrey /*if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 184*134Sandrey if(!isset($tmpRes[$pageData['page']])){ 185*134Sandrey $tmpRes[$pageData['page']] = 1; 186*134Sandrey $counter++; 187*134Sandrey } 188*134Sandrey $pagesIds[$id] = $pageData; 189*134Sandrey if ($counter == $this->_resultsPerPage){ 190*134Sandrey break; 191*134Sandrey } 192*134Sandrey }*/ 193*134Sandrey 194*134Sandrey $matchPages[$page['page']] = $page['hid']; 195*134Sandrey } 196*134Sandrey 197*134Sandrey return $matchPages; 198*134Sandrey } 199*134Sandrey 20028Syaroslav@ivinco.com public function getError() 20128Syaroslav@ivinco.com { 20228Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 20328Syaroslav@ivinco.com } 20428Syaroslav@ivinco.com 2050Syaroslav@ivinco.com public function getTotalFound() 2060Syaroslav@ivinco.com { 2070Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 2080Syaroslav@ivinco.com } 20910Syaroslav@ivinco.com 21010Syaroslav@ivinco.com public function getExcerpt($data, $query) 21110Syaroslav@ivinco.com { 21216Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 21316Syaroslav@ivinco.com array( 21416Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 21539Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 21639Syaroslav@ivinco.com 'weight_order' => 1, 21739Syaroslav@ivinco.com 'sp' => 1 21816Syaroslav@ivinco.com ) 21916Syaroslav@ivinco.com ); 22010Syaroslav@ivinco.com } 22110Syaroslav@ivinco.com 22210Syaroslav@ivinco.com public function starQuery($query) 22310Syaroslav@ivinco.com { 22461Syaroslav@ivinco.com $query = $this->removeStars($query); 22510Syaroslav@ivinco.com $words = explode(" ", $query); 22661Syaroslav@ivinco.com foreach($words as $id => $word){ 22761Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 22810Syaroslav@ivinco.com } 22961Syaroslav@ivinco.com return implode(" ", $words); 23061Syaroslav@ivinco.com } 23161Syaroslav@ivinco.com 23261Syaroslav@ivinco.com public function removeStars($query) 23361Syaroslav@ivinco.com { 23461Syaroslav@ivinco.com $words = explode(" ", $query); 23561Syaroslav@ivinco.com foreach($words as $id => $word){ 23661Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 23761Syaroslav@ivinco.com } 23861Syaroslav@ivinco.com return implode(" ", $words); 23910Syaroslav@ivinco.com } 24015Syaroslav@ivinco.com 24115Syaroslav@ivinco.com public function getQuery() 24215Syaroslav@ivinco.com { 24315Syaroslav@ivinco.com return $this->_query; 24415Syaroslav@ivinco.com } 24516Syaroslav@ivinco.com 24616Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 24716Syaroslav@ivinco.com { 24816Syaroslav@ivinco.com $this->_snippetSize = $symbols; 24916Syaroslav@ivinco.com } 25016Syaroslav@ivinco.com 25116Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 25216Syaroslav@ivinco.com { 25316Syaroslav@ivinco.com $this->_aroundKeyword = $words; 25416Syaroslav@ivinco.com } 25519Syaroslav@ivinco.com 25619Syaroslav@ivinco.com public function setTitlePriority($priority) 25719Syaroslav@ivinco.com { 25819Syaroslav@ivinco.com $this->_titlePriority = $priority; 25919Syaroslav@ivinco.com } 26019Syaroslav@ivinco.com 26119Syaroslav@ivinco.com public function setBodyPriority($priority) 26219Syaroslav@ivinco.com { 26319Syaroslav@ivinco.com $this->_bodyPriority = $priority; 26419Syaroslav@ivinco.com } 26519Syaroslav@ivinco.com 26680Syaroslav@ivinco.com public function setNamespacePriority($priority) 26719Syaroslav@ivinco.com { 26880Syaroslav@ivinco.com $this->_namespacePriority = $priority; 26980Syaroslav@ivinco.com } 27080Syaroslav@ivinco.com 27180Syaroslav@ivinco.com public function setPagenamePriority($priority) 27280Syaroslav@ivinco.com { 27380Syaroslav@ivinco.com $this->_pagenamePriority = $priority; 27419Syaroslav@ivinco.com } 275124Syaroslav@ivinco.com 276124Syaroslav@ivinco.com private function _enableQuotesAndDefis($query) 277124Syaroslav@ivinco.com { 278124Syaroslav@ivinco.com $query = ' '. $query; 279124Syaroslav@ivinco.com $quotesCount = count(explode('"', $query))-1; 280124Syaroslav@ivinco.com if ($quotesCount && $quotesCount%2 == 0){ 281124Syaroslav@ivinco.com $query = str_replace('\"', '"', $query); 282124Syaroslav@ivinco.com } 283124Syaroslav@ivinco.com $query = preg_replace("#\s\\\-(\w)#ui", " -$1", $query); 284124Syaroslav@ivinco.com 285124Syaroslav@ivinco.com $query = substr($query, 1); 286124Syaroslav@ivinco.com 287124Syaroslav@ivinco.com return $query; 288124Syaroslav@ivinco.com } 2890Syaroslav@ivinco.com} 290