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 } 113*135Sandrey } else { 114*135Sandrey // decrease total found counter for the first page if the page is filtered 115*135Sandrey $this->_result['total_found']--; 116124Syaroslav@ivinco.com } 117124Syaroslav@ivinco.com } 118124Syaroslav@ivinco.com if (empty($pagesIds)){ 11927Syaroslav@ivinco.com return false; 12027Syaroslav@ivinco.com } 1210Syaroslav@ivinco.com 1220Syaroslav@ivinco.com $pagesList = array(); 12310Syaroslav@ivinco.com $body = array(); 12419Syaroslav@ivinco.com $titleText = array(); 12510Syaroslav@ivinco.com $category = array(); 12627Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 127124Syaroslav@ivinco.com if (empty($data['page'])){ 128124Syaroslav@ivinco.com continue; 129124Syaroslav@ivinco.com } 13027Syaroslav@ivinco.com if (!empty($data['hid'])){ 13127Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1326Syaroslav@ivinco.com } else { 13327Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1348Syaroslav@ivinco.com } 13560Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 13657Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 13710Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 13879Syaroslav@ivinco.com if(!empty($data['title_text'])){ 13979Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 14079Syaroslav@ivinco.com } else { 14179Syaroslav@ivinco.com $titleText[$crc] = $data['page']; 14279Syaroslav@ivinco.com } 14327Syaroslav@ivinco.com $category[$crc] = $data['page']; 14457Syaroslav@ivinco.com } 145134Sandrey 14680Syaroslav@ivinco.com //$starQuery = $this->starQuery($keywords); 14780Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $keywords); 14880Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $keywords); 1490Syaroslav@ivinco.com $i = 0; 1504Syaroslav@ivinco.com $results = array(); 15110Syaroslav@ivinco.com foreach($body as $crc => $notused){ 15210Syaroslav@ivinco.com $results[$crc] = array( 15310Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 15410Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 15519Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 15610Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 15719Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 15819Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 15910Syaroslav@ivinco.com ); 16010Syaroslav@ivinco.com $i++; 1610Syaroslav@ivinco.com } 1624Syaroslav@ivinco.com return $results; 1630Syaroslav@ivinco.com } 1640Syaroslav@ivinco.com 16569Syaroslav@ivinco.com public function getPagesIds() 16669Syaroslav@ivinco.com { 16769Syaroslav@ivinco.com $pageMapper = new PageMapper(); 16869Syaroslav@ivinco.com 16969Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 17069Syaroslav@ivinco.com } 17169Syaroslav@ivinco.com 17239Syaroslav@ivinco.com public function getOffset() 17339Syaroslav@ivinco.com { 17439Syaroslav@ivinco.com return $this->_offset; 17539Syaroslav@ivinco.com } 17639Syaroslav@ivinco.com 177134Sandrey public function getPageNames() 178134Sandrey { 179134Sandrey $pageIds = $this->getPagesIds(); 180134Sandrey 181134Sandrey $matchPages = array(); 182134Sandrey foreach ($pageIds as $page) { 183*135Sandrey if (auth_quickaclcheck($page['page']) < AUTH_READ) { 184134Sandrey continue; 185134Sandrey } 186134Sandrey $matchPages[$page['page']] = $page['hid']; 187134Sandrey } 188134Sandrey 189134Sandrey return $matchPages; 190134Sandrey } 191134Sandrey 19228Syaroslav@ivinco.com public function getError() 19328Syaroslav@ivinco.com { 19428Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 19528Syaroslav@ivinco.com } 19628Syaroslav@ivinco.com 1970Syaroslav@ivinco.com public function getTotalFound() 1980Syaroslav@ivinco.com { 1990Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 2000Syaroslav@ivinco.com } 20110Syaroslav@ivinco.com 20210Syaroslav@ivinco.com public function getExcerpt($data, $query) 20310Syaroslav@ivinco.com { 20416Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 20516Syaroslav@ivinco.com array( 20616Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 20739Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 20839Syaroslav@ivinco.com 'weight_order' => 1, 20939Syaroslav@ivinco.com 'sp' => 1 21016Syaroslav@ivinco.com ) 21116Syaroslav@ivinco.com ); 21210Syaroslav@ivinco.com } 21310Syaroslav@ivinco.com 21410Syaroslav@ivinco.com public function starQuery($query) 21510Syaroslav@ivinco.com { 21661Syaroslav@ivinco.com $query = $this->removeStars($query); 21710Syaroslav@ivinco.com $words = explode(" ", $query); 21861Syaroslav@ivinco.com foreach($words as $id => $word){ 21961Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 22010Syaroslav@ivinco.com } 22161Syaroslav@ivinco.com return implode(" ", $words); 22261Syaroslav@ivinco.com } 22361Syaroslav@ivinco.com 22461Syaroslav@ivinco.com public function removeStars($query) 22561Syaroslav@ivinco.com { 22661Syaroslav@ivinco.com $words = explode(" ", $query); 22761Syaroslav@ivinco.com foreach($words as $id => $word){ 22861Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 22961Syaroslav@ivinco.com } 23061Syaroslav@ivinco.com return implode(" ", $words); 23110Syaroslav@ivinco.com } 23215Syaroslav@ivinco.com 23315Syaroslav@ivinco.com public function getQuery() 23415Syaroslav@ivinco.com { 23515Syaroslav@ivinco.com return $this->_query; 23615Syaroslav@ivinco.com } 23716Syaroslav@ivinco.com 23816Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 23916Syaroslav@ivinco.com { 24016Syaroslav@ivinco.com $this->_snippetSize = $symbols; 24116Syaroslav@ivinco.com } 24216Syaroslav@ivinco.com 24316Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 24416Syaroslav@ivinco.com { 24516Syaroslav@ivinco.com $this->_aroundKeyword = $words; 24616Syaroslav@ivinco.com } 24719Syaroslav@ivinco.com 24819Syaroslav@ivinco.com public function setTitlePriority($priority) 24919Syaroslav@ivinco.com { 25019Syaroslav@ivinco.com $this->_titlePriority = $priority; 25119Syaroslav@ivinco.com } 25219Syaroslav@ivinco.com 25319Syaroslav@ivinco.com public function setBodyPriority($priority) 25419Syaroslav@ivinco.com { 25519Syaroslav@ivinco.com $this->_bodyPriority = $priority; 25619Syaroslav@ivinco.com } 25719Syaroslav@ivinco.com 25880Syaroslav@ivinco.com public function setNamespacePriority($priority) 25919Syaroslav@ivinco.com { 26080Syaroslav@ivinco.com $this->_namespacePriority = $priority; 26180Syaroslav@ivinco.com } 26280Syaroslav@ivinco.com 26380Syaroslav@ivinco.com public function setPagenamePriority($priority) 26480Syaroslav@ivinco.com { 26580Syaroslav@ivinco.com $this->_pagenamePriority = $priority; 26619Syaroslav@ivinco.com } 267124Syaroslav@ivinco.com 268124Syaroslav@ivinco.com private function _enableQuotesAndDefis($query) 269124Syaroslav@ivinco.com { 270124Syaroslav@ivinco.com $query = ' '. $query; 271124Syaroslav@ivinco.com $quotesCount = count(explode('"', $query))-1; 272124Syaroslav@ivinco.com if ($quotesCount && $quotesCount%2 == 0){ 273124Syaroslav@ivinco.com $query = str_replace('\"', '"', $query); 274124Syaroslav@ivinco.com } 275124Syaroslav@ivinco.com $query = preg_replace("#\s\\\-(\w)#ui", " -$1", $query); 276124Syaroslav@ivinco.com 277124Syaroslav@ivinco.com $query = substr($query, 1); 278124Syaroslav@ivinco.com 279124Syaroslav@ivinco.com return $query; 280124Syaroslav@ivinco.com } 2810Syaroslav@ivinco.com} 282