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 1819Syaroslav@ivinco.com private $_titlePriority = 20; 1919Syaroslav@ivinco.com private $_bodyPriority = 5; 2019Syaroslav@ivinco.com private $_categoriesPriority = 10; 2116Syaroslav@ivinco.com 220Syaroslav@ivinco.com public function __construct($host, $port, $index) 230Syaroslav@ivinco.com { 240Syaroslav@ivinco.com $this->_sphinx = new SphinxClient(); 250Syaroslav@ivinco.com $this->_sphinx->SetServer($host, $port); 2619Syaroslav@ivinco.com $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 270Syaroslav@ivinco.com 280Syaroslav@ivinco.com $this->_index = $index; 290Syaroslav@ivinco.com } 300Syaroslav@ivinco.com 3169Syaroslav@ivinco.com public function setSearchAllQuery($keywords, $categories) 3269Syaroslav@ivinco.com { 3370Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 3470Syaroslav@ivinco.com $this->_query = "(@categories $starKeyword) | (@(body,title) {$keywords})"; 3569Syaroslav@ivinco.com } 3669Syaroslav@ivinco.com 3769Syaroslav@ivinco.com public function setSearchAllQueryWithCategoryFilter($keywords, $categories) 3819Syaroslav@ivinco.com { 3969Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 4069Syaroslav@ivinco.com if(strpos($categories, "-") === 0){ 4169Syaroslav@ivinco.com $categories = '-"'.substr($categories, 1).'"'; 4269Syaroslav@ivinco.com } 4369Syaroslav@ivinco.com $this->_query = "(@categories {$categories}) & ((@(body,title) {$keywords}) | (@categories {$starKeyword}))"; 4469Syaroslav@ivinco.com } 4569Syaroslav@ivinco.com 4670Syaroslav@ivinco.com public function setSearchCategoryQuery($keywords, $categories) 4769Syaroslav@ivinco.com { 4870Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 49*72Syaroslav@ivinco.com if (!empty($categories)){ 50*72Syaroslav@ivinco.com $this->_query = "((@categories {$categories}) & (@categories $starKeyword))"; 51*72Syaroslav@ivinco.com } else { 52*72Syaroslav@ivinco.com $this->_query = "@categories $starKeyword"; 53*72Syaroslav@ivinco.com } 5469Syaroslav@ivinco.com } 5569Syaroslav@ivinco.com 5669Syaroslav@ivinco.com public function search($start, $resultsPerPage = 10) 5769Syaroslav@ivinco.com { 5869Syaroslav@ivinco.com $this->_resultsPerPage = $resultsPerPage; 5969Syaroslav@ivinco.com 6019Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => $this->_categoriesPriority, 'title' => $this->_titlePriority, 'body' => $this->_bodyPriority)); 6119Syaroslav@ivinco.com 6227Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage+100); 6369Syaroslav@ivinco.com 6469Syaroslav@ivinco.com $res = $this->_sphinx->Query($this->_query, $this->_index); 650Syaroslav@ivinco.com $this->_result = $res; 6628Syaroslav@ivinco.com 670Syaroslav@ivinco.com if (empty($res['matches'])) { 680Syaroslav@ivinco.com return false; 690Syaroslav@ivinco.com } 7069Syaroslav@ivinco.com return true; 7169Syaroslav@ivinco.com } 720Syaroslav@ivinco.com 7369Syaroslav@ivinco.com public function getPages() 7469Syaroslav@ivinco.com { 7569Syaroslav@ivinco.com $pagesIdsAll = $this->getPagesIds(); 7639Syaroslav@ivinco.com $this->_offset = 0; 7739Syaroslav@ivinco.com $counter = 0; 7843Syaroslav@ivinco.com $tmpRes = array(); 7943Syaroslav@ivinco.com $pagesIds = array(); 8039Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 8139Syaroslav@ivinco.com $this->_offset++; 8239Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 8343Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 8443Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 8543Syaroslav@ivinco.com $counter++; 8643Syaroslav@ivinco.com } 8739Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 8869Syaroslav@ivinco.com if ($counter == $this->_resultsPerPage){ 8939Syaroslav@ivinco.com break; 9039Syaroslav@ivinco.com } 9139Syaroslav@ivinco.com } 9239Syaroslav@ivinco.com } 9339Syaroslav@ivinco.com if (empty($pagesIds)){ 9427Syaroslav@ivinco.com return false; 9527Syaroslav@ivinco.com } 960Syaroslav@ivinco.com 970Syaroslav@ivinco.com $pagesList = array(); 9810Syaroslav@ivinco.com $body = array(); 9919Syaroslav@ivinco.com $titleText = array(); 10010Syaroslav@ivinco.com $category = array(); 10127Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 10227Syaroslav@ivinco.com if (!empty($data['hid'])){ 10327Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1046Syaroslav@ivinco.com } else { 10527Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1068Syaroslav@ivinco.com } 10760Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 10857Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 10910Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 11027Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 11127Syaroslav@ivinco.com $category[$crc] = $data['page']; 11257Syaroslav@ivinco.com } 1130Syaroslav@ivinco.com 11415Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 11510Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 11619Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 1170Syaroslav@ivinco.com $i = 0; 1184Syaroslav@ivinco.com $results = array(); 11910Syaroslav@ivinco.com foreach($body as $crc => $notused){ 12010Syaroslav@ivinco.com $results[$crc] = array( 12110Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 12210Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 12319Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 12410Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 12519Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 12619Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 12710Syaroslav@ivinco.com ); 12810Syaroslav@ivinco.com $i++; 1290Syaroslav@ivinco.com } 1304Syaroslav@ivinco.com return $results; 1310Syaroslav@ivinco.com } 1320Syaroslav@ivinco.com 13369Syaroslav@ivinco.com public function getPagesIds() 13469Syaroslav@ivinco.com { 13569Syaroslav@ivinco.com $pageMapper = new PageMapper(); 13669Syaroslav@ivinco.com 13769Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 13869Syaroslav@ivinco.com } 13969Syaroslav@ivinco.com 14039Syaroslav@ivinco.com public function getOffset() 14139Syaroslav@ivinco.com { 14239Syaroslav@ivinco.com return $this->_offset; 14339Syaroslav@ivinco.com } 14439Syaroslav@ivinco.com 14528Syaroslav@ivinco.com public function getError() 14628Syaroslav@ivinco.com { 14728Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 14828Syaroslav@ivinco.com } 14928Syaroslav@ivinco.com 1500Syaroslav@ivinco.com public function getTotalFound() 1510Syaroslav@ivinco.com { 1520Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1530Syaroslav@ivinco.com } 15410Syaroslav@ivinco.com 15510Syaroslav@ivinco.com public function getExcerpt($data, $query) 15610Syaroslav@ivinco.com { 15716Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 15816Syaroslav@ivinco.com array( 15916Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 16039Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 16139Syaroslav@ivinco.com 'weight_order' => 1, 16239Syaroslav@ivinco.com 'sp' => 1 16316Syaroslav@ivinco.com ) 16416Syaroslav@ivinco.com ); 16510Syaroslav@ivinco.com } 16610Syaroslav@ivinco.com 16710Syaroslav@ivinco.com public function starQuery($query) 16810Syaroslav@ivinco.com { 16961Syaroslav@ivinco.com $query = $this->removeStars($query); 17010Syaroslav@ivinco.com $words = explode(" ", $query); 17161Syaroslav@ivinco.com foreach($words as $id => $word){ 17261Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 17310Syaroslav@ivinco.com } 17461Syaroslav@ivinco.com return implode(" ", $words); 17561Syaroslav@ivinco.com } 17661Syaroslav@ivinco.com 17761Syaroslav@ivinco.com public function removeStars($query) 17861Syaroslav@ivinco.com { 17961Syaroslav@ivinco.com $words = explode(" ", $query); 18061Syaroslav@ivinco.com foreach($words as $id => $word){ 18161Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 18261Syaroslav@ivinco.com } 18361Syaroslav@ivinco.com return implode(" ", $words); 18410Syaroslav@ivinco.com } 18515Syaroslav@ivinco.com 18615Syaroslav@ivinco.com public function getQuery() 18715Syaroslav@ivinco.com { 18815Syaroslav@ivinco.com return $this->_query; 18915Syaroslav@ivinco.com } 19016Syaroslav@ivinco.com 19116Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 19216Syaroslav@ivinco.com { 19316Syaroslav@ivinco.com $this->_snippetSize = $symbols; 19416Syaroslav@ivinco.com } 19516Syaroslav@ivinco.com 19616Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 19716Syaroslav@ivinco.com { 19816Syaroslav@ivinco.com $this->_aroundKeyword = $words; 19916Syaroslav@ivinco.com } 20019Syaroslav@ivinco.com 20119Syaroslav@ivinco.com public function setTitlePriority($priority) 20219Syaroslav@ivinco.com { 20319Syaroslav@ivinco.com $this->_titlePriority = $priority; 20419Syaroslav@ivinco.com } 20519Syaroslav@ivinco.com 20619Syaroslav@ivinco.com public function setBodyPriority($priority) 20719Syaroslav@ivinco.com { 20819Syaroslav@ivinco.com $this->_bodyPriority = $priority; 20919Syaroslav@ivinco.com } 21019Syaroslav@ivinco.com 21119Syaroslav@ivinco.com public function setCategoriesPriority($priority) 21219Syaroslav@ivinco.com { 21319Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 21419Syaroslav@ivinco.com } 2150Syaroslav@ivinco.com} 216