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 18*80Syaroslav@ivinco.com private $_titlePriority = 1; 19*80Syaroslav@ivinco.com private $_bodyPriority = 1; 20*80Syaroslav@ivinco.com private $_namespacePriority = 1; 21*80Syaroslav@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 { 3470Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 35*80Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $starKeyword) | (@(body,title) {$keywords})"; 3669Syaroslav@ivinco.com } 3769Syaroslav@ivinco.com 3869Syaroslav@ivinco.com public function setSearchAllQueryWithCategoryFilter($keywords, $categories) 3919Syaroslav@ivinco.com { 4069Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 4169Syaroslav@ivinco.com if(strpos($categories, "-") === 0){ 4269Syaroslav@ivinco.com $categories = '-"'.substr($categories, 1).'"'; 4369Syaroslav@ivinco.com } 44*80Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) {$categories}) & ((@(body,title) {$keywords}) | (@(namespace,pagename) {$starKeyword}))"; 4569Syaroslav@ivinco.com } 4669Syaroslav@ivinco.com 4770Syaroslav@ivinco.com public function setSearchCategoryQuery($keywords, $categories) 4869Syaroslav@ivinco.com { 4970Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 5072Syaroslav@ivinco.com if (!empty($categories)){ 51*80Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $categories $starKeyword)"; 5272Syaroslav@ivinco.com } else { 53*80Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $starKeyword)"; 5472Syaroslav@ivinco.com } 5569Syaroslav@ivinco.com } 5669Syaroslav@ivinco.com 5769Syaroslav@ivinco.com public function search($start, $resultsPerPage = 10) 5869Syaroslav@ivinco.com { 5969Syaroslav@ivinco.com $this->_resultsPerPage = $resultsPerPage; 6069Syaroslav@ivinco.com 61*80Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array( 62*80Syaroslav@ivinco.com 'namespace' => $this->_namespacePriority, 63*80Syaroslav@ivinco.com 'pagename' => $this->_pagenamePriority, 64*80Syaroslav@ivinco.com 'title' => $this->_titlePriority, 65*80Syaroslav@ivinco.com 'body' => $this->_bodyPriority) 66*80Syaroslav@ivinco.com ); 6719Syaroslav@ivinco.com 6827Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage+100); 6969Syaroslav@ivinco.com 7069Syaroslav@ivinco.com $res = $this->_sphinx->Query($this->_query, $this->_index); 710Syaroslav@ivinco.com $this->_result = $res; 7228Syaroslav@ivinco.com 730Syaroslav@ivinco.com if (empty($res['matches'])) { 740Syaroslav@ivinco.com return false; 750Syaroslav@ivinco.com } 7669Syaroslav@ivinco.com return true; 7769Syaroslav@ivinco.com } 780Syaroslav@ivinco.com 7977Syaroslav@ivinco.com public function getPages($keywords) 8069Syaroslav@ivinco.com { 8169Syaroslav@ivinco.com $pagesIdsAll = $this->getPagesIds(); 8239Syaroslav@ivinco.com $this->_offset = 0; 8339Syaroslav@ivinco.com $counter = 0; 8443Syaroslav@ivinco.com $tmpRes = array(); 8543Syaroslav@ivinco.com $pagesIds = array(); 8639Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 8739Syaroslav@ivinco.com $this->_offset++; 8839Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 8943Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 9043Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 9143Syaroslav@ivinco.com $counter++; 9243Syaroslav@ivinco.com } 9339Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 9469Syaroslav@ivinco.com if ($counter == $this->_resultsPerPage){ 9539Syaroslav@ivinco.com break; 9639Syaroslav@ivinco.com } 9739Syaroslav@ivinco.com } 9839Syaroslav@ivinco.com } 9939Syaroslav@ivinco.com if (empty($pagesIds)){ 10027Syaroslav@ivinco.com return false; 10127Syaroslav@ivinco.com } 1020Syaroslav@ivinco.com 1030Syaroslav@ivinco.com $pagesList = array(); 10410Syaroslav@ivinco.com $body = array(); 10519Syaroslav@ivinco.com $titleText = array(); 10610Syaroslav@ivinco.com $category = array(); 10727Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 10827Syaroslav@ivinco.com if (!empty($data['hid'])){ 10927Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1106Syaroslav@ivinco.com } else { 11127Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1128Syaroslav@ivinco.com } 11360Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 11457Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 11510Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 11679Syaroslav@ivinco.com if(!empty($data['title_text'])){ 11779Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 11879Syaroslav@ivinco.com } else { 11979Syaroslav@ivinco.com $titleText[$crc] = $data['page']; 12079Syaroslav@ivinco.com } 12127Syaroslav@ivinco.com $category[$crc] = $data['page']; 12257Syaroslav@ivinco.com } 1230Syaroslav@ivinco.com 124*80Syaroslav@ivinco.com //$starQuery = $this->starQuery($keywords); 125*80Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $keywords); 126*80Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $keywords); 1270Syaroslav@ivinco.com $i = 0; 1284Syaroslav@ivinco.com $results = array(); 12910Syaroslav@ivinco.com foreach($body as $crc => $notused){ 13010Syaroslav@ivinco.com $results[$crc] = array( 13110Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 13210Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 13319Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 13410Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 13519Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 13619Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 13710Syaroslav@ivinco.com ); 13810Syaroslav@ivinco.com $i++; 1390Syaroslav@ivinco.com } 1404Syaroslav@ivinco.com return $results; 1410Syaroslav@ivinco.com } 1420Syaroslav@ivinco.com 14369Syaroslav@ivinco.com public function getPagesIds() 14469Syaroslav@ivinco.com { 14569Syaroslav@ivinco.com $pageMapper = new PageMapper(); 14669Syaroslav@ivinco.com 14769Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 14869Syaroslav@ivinco.com } 14969Syaroslav@ivinco.com 15039Syaroslav@ivinco.com public function getOffset() 15139Syaroslav@ivinco.com { 15239Syaroslav@ivinco.com return $this->_offset; 15339Syaroslav@ivinco.com } 15439Syaroslav@ivinco.com 15528Syaroslav@ivinco.com public function getError() 15628Syaroslav@ivinco.com { 15728Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 15828Syaroslav@ivinco.com } 15928Syaroslav@ivinco.com 1600Syaroslav@ivinco.com public function getTotalFound() 1610Syaroslav@ivinco.com { 1620Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1630Syaroslav@ivinco.com } 16410Syaroslav@ivinco.com 16510Syaroslav@ivinco.com public function getExcerpt($data, $query) 16610Syaroslav@ivinco.com { 16716Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 16816Syaroslav@ivinco.com array( 16916Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 17039Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 17139Syaroslav@ivinco.com 'weight_order' => 1, 17239Syaroslav@ivinco.com 'sp' => 1 17316Syaroslav@ivinco.com ) 17416Syaroslav@ivinco.com ); 17510Syaroslav@ivinco.com } 17610Syaroslav@ivinco.com 17710Syaroslav@ivinco.com public function starQuery($query) 17810Syaroslav@ivinco.com { 17961Syaroslav@ivinco.com $query = $this->removeStars($query); 18010Syaroslav@ivinco.com $words = explode(" ", $query); 18161Syaroslav@ivinco.com foreach($words as $id => $word){ 18261Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 18310Syaroslav@ivinco.com } 18461Syaroslav@ivinco.com return implode(" ", $words); 18561Syaroslav@ivinco.com } 18661Syaroslav@ivinco.com 18761Syaroslav@ivinco.com public function removeStars($query) 18861Syaroslav@ivinco.com { 18961Syaroslav@ivinco.com $words = explode(" ", $query); 19061Syaroslav@ivinco.com foreach($words as $id => $word){ 19161Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 19261Syaroslav@ivinco.com } 19361Syaroslav@ivinco.com return implode(" ", $words); 19410Syaroslav@ivinco.com } 19515Syaroslav@ivinco.com 19615Syaroslav@ivinco.com public function getQuery() 19715Syaroslav@ivinco.com { 19815Syaroslav@ivinco.com return $this->_query; 19915Syaroslav@ivinco.com } 20016Syaroslav@ivinco.com 20116Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 20216Syaroslav@ivinco.com { 20316Syaroslav@ivinco.com $this->_snippetSize = $symbols; 20416Syaroslav@ivinco.com } 20516Syaroslav@ivinco.com 20616Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 20716Syaroslav@ivinco.com { 20816Syaroslav@ivinco.com $this->_aroundKeyword = $words; 20916Syaroslav@ivinco.com } 21019Syaroslav@ivinco.com 21119Syaroslav@ivinco.com public function setTitlePriority($priority) 21219Syaroslav@ivinco.com { 21319Syaroslav@ivinco.com $this->_titlePriority = $priority; 21419Syaroslav@ivinco.com } 21519Syaroslav@ivinco.com 21619Syaroslav@ivinco.com public function setBodyPriority($priority) 21719Syaroslav@ivinco.com { 21819Syaroslav@ivinco.com $this->_bodyPriority = $priority; 21919Syaroslav@ivinco.com } 22019Syaroslav@ivinco.com 221*80Syaroslav@ivinco.com public function setNamespacePriority($priority) 22219Syaroslav@ivinco.com { 223*80Syaroslav@ivinco.com $this->_namespacePriority = $priority; 224*80Syaroslav@ivinco.com } 225*80Syaroslav@ivinco.com 226*80Syaroslav@ivinco.com public function setPagenamePriority($priority) 227*80Syaroslav@ivinco.com { 228*80Syaroslav@ivinco.com $this->_pagenamePriority = $priority; 22919Syaroslav@ivinco.com } 2300Syaroslav@ivinco.com} 231