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 { 33*70Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 34*70Syaroslav@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 46*70Syaroslav@ivinco.com public function setSearchCategoryQuery($keywords, $categories) 4769Syaroslav@ivinco.com { 48*70Syaroslav@ivinco.com $starKeyword = $this->starQuery($keywords); 49*70Syaroslav@ivinco.com $this->_query = "@categories {$categories} $starKeyword"; 5069Syaroslav@ivinco.com } 5169Syaroslav@ivinco.com 5269Syaroslav@ivinco.com public function search($start, $resultsPerPage = 10) 5369Syaroslav@ivinco.com { 5469Syaroslav@ivinco.com $this->_resultsPerPage = $resultsPerPage; 5569Syaroslav@ivinco.com 5619Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => $this->_categoriesPriority, 'title' => $this->_titlePriority, 'body' => $this->_bodyPriority)); 5719Syaroslav@ivinco.com 5827Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage+100); 5969Syaroslav@ivinco.com 6069Syaroslav@ivinco.com $res = $this->_sphinx->Query($this->_query, $this->_index); 610Syaroslav@ivinco.com $this->_result = $res; 6228Syaroslav@ivinco.com 630Syaroslav@ivinco.com if (empty($res['matches'])) { 640Syaroslav@ivinco.com return false; 650Syaroslav@ivinco.com } 6669Syaroslav@ivinco.com return true; 6769Syaroslav@ivinco.com } 680Syaroslav@ivinco.com 6969Syaroslav@ivinco.com public function getPages() 7069Syaroslav@ivinco.com { 7169Syaroslav@ivinco.com $pagesIdsAll = $this->getPagesIds(); 7239Syaroslav@ivinco.com $this->_offset = 0; 7339Syaroslav@ivinco.com $counter = 0; 7443Syaroslav@ivinco.com $tmpRes = array(); 7543Syaroslav@ivinco.com $pagesIds = array(); 7639Syaroslav@ivinco.com foreach($pagesIdsAll as $id => $pageData){ 7739Syaroslav@ivinco.com $this->_offset++; 7839Syaroslav@ivinco.com if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){ 7943Syaroslav@ivinco.com if(!isset($tmpRes[$pageData['page']])){ 8043Syaroslav@ivinco.com $tmpRes[$pageData['page']] = 1; 8143Syaroslav@ivinco.com $counter++; 8243Syaroslav@ivinco.com } 8339Syaroslav@ivinco.com $pagesIds[$id] = $pageData; 8469Syaroslav@ivinco.com if ($counter == $this->_resultsPerPage){ 8539Syaroslav@ivinco.com break; 8639Syaroslav@ivinco.com } 8739Syaroslav@ivinco.com } 8839Syaroslav@ivinco.com } 8939Syaroslav@ivinco.com if (empty($pagesIds)){ 9027Syaroslav@ivinco.com return false; 9127Syaroslav@ivinco.com } 920Syaroslav@ivinco.com 930Syaroslav@ivinco.com $pagesList = array(); 9410Syaroslav@ivinco.com $body = array(); 9519Syaroslav@ivinco.com $titleText = array(); 9610Syaroslav@ivinco.com $category = array(); 9727Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 9827Syaroslav@ivinco.com if (!empty($data['hid'])){ 9927Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 1006Syaroslav@ivinco.com } else { 10127Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 1028Syaroslav@ivinco.com } 10360Syaroslav@ivinco.com $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml); 10457Syaroslav@ivinco.com $bodyHtml = htmlspecialchars_decode($bodyHtml); 10510Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 10627Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 10727Syaroslav@ivinco.com $category[$crc] = $data['page']; 10857Syaroslav@ivinco.com } 1090Syaroslav@ivinco.com 11015Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 11110Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 11219Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 1130Syaroslav@ivinco.com $i = 0; 1144Syaroslav@ivinco.com $results = array(); 11510Syaroslav@ivinco.com foreach($body as $crc => $notused){ 11610Syaroslav@ivinco.com $results[$crc] = array( 11710Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 11810Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 11919Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 12010Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 12119Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 12219Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 12310Syaroslav@ivinco.com ); 12410Syaroslav@ivinco.com $i++; 1250Syaroslav@ivinco.com } 1264Syaroslav@ivinco.com return $results; 1270Syaroslav@ivinco.com } 1280Syaroslav@ivinco.com 12969Syaroslav@ivinco.com public function getPagesIds() 13069Syaroslav@ivinco.com { 13169Syaroslav@ivinco.com $pageMapper = new PageMapper(); 13269Syaroslav@ivinco.com 13369Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 13469Syaroslav@ivinco.com } 13569Syaroslav@ivinco.com 13639Syaroslav@ivinco.com public function getOffset() 13739Syaroslav@ivinco.com { 13839Syaroslav@ivinco.com return $this->_offset; 13939Syaroslav@ivinco.com } 14039Syaroslav@ivinco.com 14128Syaroslav@ivinco.com public function getError() 14228Syaroslav@ivinco.com { 14328Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 14428Syaroslav@ivinco.com } 14528Syaroslav@ivinco.com 1460Syaroslav@ivinco.com public function getTotalFound() 1470Syaroslav@ivinco.com { 1480Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1490Syaroslav@ivinco.com } 15010Syaroslav@ivinco.com 15110Syaroslav@ivinco.com public function getExcerpt($data, $query) 15210Syaroslav@ivinco.com { 15316Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 15416Syaroslav@ivinco.com array( 15516Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 15639Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 15739Syaroslav@ivinco.com 'weight_order' => 1, 15839Syaroslav@ivinco.com 'sp' => 1 15916Syaroslav@ivinco.com ) 16016Syaroslav@ivinco.com ); 16110Syaroslav@ivinco.com } 16210Syaroslav@ivinco.com 16310Syaroslav@ivinco.com public function starQuery($query) 16410Syaroslav@ivinco.com { 16561Syaroslav@ivinco.com $query = $this->removeStars($query); 16610Syaroslav@ivinco.com $words = explode(" ", $query); 16761Syaroslav@ivinco.com foreach($words as $id => $word){ 16861Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 16910Syaroslav@ivinco.com } 17061Syaroslav@ivinco.com return implode(" ", $words); 17161Syaroslav@ivinco.com } 17261Syaroslav@ivinco.com 17361Syaroslav@ivinco.com public function removeStars($query) 17461Syaroslav@ivinco.com { 17561Syaroslav@ivinco.com $words = explode(" ", $query); 17661Syaroslav@ivinco.com foreach($words as $id => $word){ 17761Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 17861Syaroslav@ivinco.com } 17961Syaroslav@ivinco.com return implode(" ", $words); 18010Syaroslav@ivinco.com } 18115Syaroslav@ivinco.com 18215Syaroslav@ivinco.com public function getQuery() 18315Syaroslav@ivinco.com { 18415Syaroslav@ivinco.com return $this->_query; 18515Syaroslav@ivinco.com } 18616Syaroslav@ivinco.com 18716Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 18816Syaroslav@ivinco.com { 18916Syaroslav@ivinco.com $this->_snippetSize = $symbols; 19016Syaroslav@ivinco.com } 19116Syaroslav@ivinco.com 19216Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 19316Syaroslav@ivinco.com { 19416Syaroslav@ivinco.com $this->_aroundKeyword = $words; 19516Syaroslav@ivinco.com } 19619Syaroslav@ivinco.com 19719Syaroslav@ivinco.com public function setTitlePriority($priority) 19819Syaroslav@ivinco.com { 19919Syaroslav@ivinco.com $this->_titlePriority = $priority; 20019Syaroslav@ivinco.com } 20119Syaroslav@ivinco.com 20219Syaroslav@ivinco.com public function setBodyPriority($priority) 20319Syaroslav@ivinco.com { 20419Syaroslav@ivinco.com $this->_bodyPriority = $priority; 20519Syaroslav@ivinco.com } 20619Syaroslav@ivinco.com 20719Syaroslav@ivinco.com public function setCategoriesPriority($priority) 20819Syaroslav@ivinco.com { 20919Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 21019Syaroslav@ivinco.com } 2110Syaroslav@ivinco.com} 212