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 1880Syaroslav@ivinco.com private $_titlePriority = 1; 1980Syaroslav@ivinco.com private $_bodyPriority = 1; 2080Syaroslav@ivinco.com private $_namespacePriority = 1; 2180Syaroslav@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); 3580Syaroslav@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 } 4480Syaroslav@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)){ 5180Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $categories $starKeyword)"; 5272Syaroslav@ivinco.com } else { 5380Syaroslav@ivinco.com $this->_query = "(@(namespace,pagename) $starKeyword)"; 5472Syaroslav@ivinco.com } 5583Syaroslav@ivinco.com 5669Syaroslav@ivinco.com } 5769Syaroslav@ivinco.com 5869Syaroslav@ivinco.com public function search($start, $resultsPerPage = 10) 5969Syaroslav@ivinco.com { 6069Syaroslav@ivinco.com $this->_resultsPerPage = $resultsPerPage; 6169Syaroslav@ivinco.com 6280Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array( 6380Syaroslav@ivinco.com 'namespace' => $this->_namespacePriority, 6480Syaroslav@ivinco.com 'pagename' => $this->_pagenamePriority, 6580Syaroslav@ivinco.com 'title' => $this->_titlePriority, 6680Syaroslav@ivinco.com 'body' => $this->_bodyPriority) 6780Syaroslav@ivinco.com ); 6819Syaroslav@ivinco.com 6927Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage+100); 7069Syaroslav@ivinco.com 7169Syaroslav@ivinco.com $res = $this->_sphinx->Query($this->_query, $this->_index); 720Syaroslav@ivinco.com $this->_result = $res; 7328Syaroslav@ivinco.com 740Syaroslav@ivinco.com if (empty($res['matches'])) { 750Syaroslav@ivinco.com return false; 760Syaroslav@ivinco.com } 7769Syaroslav@ivinco.com return true; 7869Syaroslav@ivinco.com } 790Syaroslav@ivinco.com 8077Syaroslav@ivinco.com public function getPages($keywords) 8169Syaroslav@ivinco.com { 82*92Syaroslav@ivinco.com if (empty($this->_result['matches'])) { 83*92Syaroslav@ivinco.com return false; 84*92Syaroslav@ivinco.com } 8569Syaroslav@ivinco.com $pagesIdsAll = $this->getPagesIds(); 8639Syaroslav@ivinco.com $this->_offset = 0; 8739Syaroslav@ivinco.com $counter = 0; 8843Syaroslav@ivinco.com $tmpRes = array(); 8943Syaroslav@ivinco.com $pagesIds = array(); 9039Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 9139Syaroslav@ivinco.com $this->_offset++; 9239Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 9343Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 9443Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 9543Syaroslav@ivinco.com $counter++; 9643Syaroslav@ivinco.com } 9739Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 9869Syaroslav@ivinco.com if ($counter == $this->_resultsPerPage){ 9939Syaroslav@ivinco.com break; 10039Syaroslav@ivinco.com } 10139Syaroslav@ivinco.com } 10239Syaroslav@ivinco.com } 10339Syaroslav@ivinco.com if (empty($pagesIds)){ 10427Syaroslav@ivinco.com return false; 10527Syaroslav@ivinco.com } 1060Syaroslav@ivinco.com 1070Syaroslav@ivinco.com $pagesList = array(); 10810Syaroslav@ivinco.com $body = array(); 10919Syaroslav@ivinco.com $titleText = array(); 11010Syaroslav@ivinco.com $category = array(); 11127Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 11227Syaroslav@ivinco.com if (!empty($data['hid'])){ 11327Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1146Syaroslav@ivinco.com } else { 11527Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1168Syaroslav@ivinco.com } 11760Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 11857Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 11910Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 12079Syaroslav@ivinco.com if(!empty($data['title_text'])){ 12179Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 12279Syaroslav@ivinco.com } else { 12379Syaroslav@ivinco.com $titleText[$crc] = $data['page']; 12479Syaroslav@ivinco.com } 12527Syaroslav@ivinco.com $category[$crc] = $data['page']; 12657Syaroslav@ivinco.com } 1270Syaroslav@ivinco.com 12880Syaroslav@ivinco.com //$starQuery = $this->starQuery($keywords); 12980Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $keywords); 13080Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $keywords); 1310Syaroslav@ivinco.com $i = 0; 1324Syaroslav@ivinco.com $results = array(); 13310Syaroslav@ivinco.com foreach($body as $crc => $notused){ 13410Syaroslav@ivinco.com $results[$crc] = array( 13510Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 13610Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 13719Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 13810Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 13919Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 14019Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 14110Syaroslav@ivinco.com ); 14210Syaroslav@ivinco.com $i++; 1430Syaroslav@ivinco.com } 1444Syaroslav@ivinco.com return $results; 1450Syaroslav@ivinco.com } 1460Syaroslav@ivinco.com 14769Syaroslav@ivinco.com public function getPagesIds() 14869Syaroslav@ivinco.com { 14969Syaroslav@ivinco.com $pageMapper = new PageMapper(); 15069Syaroslav@ivinco.com 15169Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 15269Syaroslav@ivinco.com } 15369Syaroslav@ivinco.com 15439Syaroslav@ivinco.com public function getOffset() 15539Syaroslav@ivinco.com { 15639Syaroslav@ivinco.com return $this->_offset; 15739Syaroslav@ivinco.com } 15839Syaroslav@ivinco.com 15928Syaroslav@ivinco.com public function getError() 16028Syaroslav@ivinco.com { 16128Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 16228Syaroslav@ivinco.com } 16328Syaroslav@ivinco.com 1640Syaroslav@ivinco.com public function getTotalFound() 1650Syaroslav@ivinco.com { 1660Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1670Syaroslav@ivinco.com } 16810Syaroslav@ivinco.com 16910Syaroslav@ivinco.com public function getExcerpt($data, $query) 17010Syaroslav@ivinco.com { 17116Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 17216Syaroslav@ivinco.com array( 17316Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 17439Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 17539Syaroslav@ivinco.com 'weight_order' => 1, 17639Syaroslav@ivinco.com 'sp' => 1 17716Syaroslav@ivinco.com ) 17816Syaroslav@ivinco.com ); 17910Syaroslav@ivinco.com } 18010Syaroslav@ivinco.com 18110Syaroslav@ivinco.com public function starQuery($query) 18210Syaroslav@ivinco.com { 18361Syaroslav@ivinco.com $query = $this->removeStars($query); 18410Syaroslav@ivinco.com $words = explode(" ", $query); 18561Syaroslav@ivinco.com foreach($words as $id => $word){ 18661Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 18710Syaroslav@ivinco.com } 18861Syaroslav@ivinco.com return implode(" ", $words); 18961Syaroslav@ivinco.com } 19061Syaroslav@ivinco.com 19161Syaroslav@ivinco.com public function removeStars($query) 19261Syaroslav@ivinco.com { 19361Syaroslav@ivinco.com $words = explode(" ", $query); 19461Syaroslav@ivinco.com foreach($words as $id => $word){ 19561Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 19661Syaroslav@ivinco.com } 19761Syaroslav@ivinco.com return implode(" ", $words); 19810Syaroslav@ivinco.com } 19915Syaroslav@ivinco.com 20015Syaroslav@ivinco.com public function getQuery() 20115Syaroslav@ivinco.com { 20215Syaroslav@ivinco.com return $this->_query; 20315Syaroslav@ivinco.com } 20416Syaroslav@ivinco.com 20516Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 20616Syaroslav@ivinco.com { 20716Syaroslav@ivinco.com $this->_snippetSize = $symbols; 20816Syaroslav@ivinco.com } 20916Syaroslav@ivinco.com 21016Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 21116Syaroslav@ivinco.com { 21216Syaroslav@ivinco.com $this->_aroundKeyword = $words; 21316Syaroslav@ivinco.com } 21419Syaroslav@ivinco.com 21519Syaroslav@ivinco.com public function setTitlePriority($priority) 21619Syaroslav@ivinco.com { 21719Syaroslav@ivinco.com $this->_titlePriority = $priority; 21819Syaroslav@ivinco.com } 21919Syaroslav@ivinco.com 22019Syaroslav@ivinco.com public function setBodyPriority($priority) 22119Syaroslav@ivinco.com { 22219Syaroslav@ivinco.com $this->_bodyPriority = $priority; 22319Syaroslav@ivinco.com } 22419Syaroslav@ivinco.com 22580Syaroslav@ivinco.com public function setNamespacePriority($priority) 22619Syaroslav@ivinco.com { 22780Syaroslav@ivinco.com $this->_namespacePriority = $priority; 22880Syaroslav@ivinco.com } 22980Syaroslav@ivinco.com 23080Syaroslav@ivinco.com public function setPagenamePriority($priority) 23180Syaroslav@ivinco.com { 23280Syaroslav@ivinco.com $this->_pagenamePriority = $priority; 23319Syaroslav@ivinco.com } 2340Syaroslav@ivinco.com} 235