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); 4972Syaroslav@ivinco.com if (!empty($categories)){ 5075Syaroslav@ivinco.com $this->_query = "(@categories $categories $starKeyword)"; 5172Syaroslav@ivinco.com } else { 5273Syaroslav@ivinco.com $this->_query = "(@categories $starKeyword)"; 5372Syaroslav@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 7377Syaroslav@ivinco.com public function getPages($keywords) 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); 110*79Syaroslav@ivinco.com if(!empty($data['title_text'])){ 111*79Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 112*79Syaroslav@ivinco.com } else { 113*79Syaroslav@ivinco.com $titleText[$crc] = $data['page']; 114*79Syaroslav@ivinco.com } 11527Syaroslav@ivinco.com $category[$crc] = $data['page']; 11657Syaroslav@ivinco.com } 1170Syaroslav@ivinco.com 11815Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 11910Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 12019Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 1210Syaroslav@ivinco.com $i = 0; 1224Syaroslav@ivinco.com $results = array(); 12310Syaroslav@ivinco.com foreach($body as $crc => $notused){ 12410Syaroslav@ivinco.com $results[$crc] = array( 12510Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 12610Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 12719Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 12810Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 12919Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 13019Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 13110Syaroslav@ivinco.com ); 13210Syaroslav@ivinco.com $i++; 1330Syaroslav@ivinco.com } 1344Syaroslav@ivinco.com return $results; 1350Syaroslav@ivinco.com } 1360Syaroslav@ivinco.com 13769Syaroslav@ivinco.com public function getPagesIds() 13869Syaroslav@ivinco.com { 13969Syaroslav@ivinco.com $pageMapper = new PageMapper(); 14069Syaroslav@ivinco.com 14169Syaroslav@ivinco.com return $pageMapper->getByCrc(array_keys($this->_result['matches'])); 14269Syaroslav@ivinco.com } 14369Syaroslav@ivinco.com 14439Syaroslav@ivinco.com public function getOffset() 14539Syaroslav@ivinco.com { 14639Syaroslav@ivinco.com return $this->_offset; 14739Syaroslav@ivinco.com } 14839Syaroslav@ivinco.com 14928Syaroslav@ivinco.com public function getError() 15028Syaroslav@ivinco.com { 15128Syaroslav@ivinco.com return $this->_sphinx->GetLastError(); 15228Syaroslav@ivinco.com } 15328Syaroslav@ivinco.com 1540Syaroslav@ivinco.com public function getTotalFound() 1550Syaroslav@ivinco.com { 1560Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 1570Syaroslav@ivinco.com } 15810Syaroslav@ivinco.com 15910Syaroslav@ivinco.com public function getExcerpt($data, $query) 16010Syaroslav@ivinco.com { 16116Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 16216Syaroslav@ivinco.com array( 16316Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 16439Syaroslav@ivinco.com 'around' => $this->_aroundKeyword, 16539Syaroslav@ivinco.com 'weight_order' => 1, 16639Syaroslav@ivinco.com 'sp' => 1 16716Syaroslav@ivinco.com ) 16816Syaroslav@ivinco.com ); 16910Syaroslav@ivinco.com } 17010Syaroslav@ivinco.com 17110Syaroslav@ivinco.com public function starQuery($query) 17210Syaroslav@ivinco.com { 17361Syaroslav@ivinco.com $query = $this->removeStars($query); 17410Syaroslav@ivinco.com $words = explode(" ", $query); 17561Syaroslav@ivinco.com foreach($words as $id => $word){ 17661Syaroslav@ivinco.com $words[$id] = "*".$word."*"; 17710Syaroslav@ivinco.com } 17861Syaroslav@ivinco.com return implode(" ", $words); 17961Syaroslav@ivinco.com } 18061Syaroslav@ivinco.com 18161Syaroslav@ivinco.com public function removeStars($query) 18261Syaroslav@ivinco.com { 18361Syaroslav@ivinco.com $words = explode(" ", $query); 18461Syaroslav@ivinco.com foreach($words as $id => $word){ 18561Syaroslav@ivinco.com $words[$id] = trim($word, "*"); 18661Syaroslav@ivinco.com } 18761Syaroslav@ivinco.com return implode(" ", $words); 18810Syaroslav@ivinco.com } 18915Syaroslav@ivinco.com 19015Syaroslav@ivinco.com public function getQuery() 19115Syaroslav@ivinco.com { 19215Syaroslav@ivinco.com return $this->_query; 19315Syaroslav@ivinco.com } 19416Syaroslav@ivinco.com 19516Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 19616Syaroslav@ivinco.com { 19716Syaroslav@ivinco.com $this->_snippetSize = $symbols; 19816Syaroslav@ivinco.com } 19916Syaroslav@ivinco.com 20016Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 20116Syaroslav@ivinco.com { 20216Syaroslav@ivinco.com $this->_aroundKeyword = $words; 20316Syaroslav@ivinco.com } 20419Syaroslav@ivinco.com 20519Syaroslav@ivinco.com public function setTitlePriority($priority) 20619Syaroslav@ivinco.com { 20719Syaroslav@ivinco.com $this->_titlePriority = $priority; 20819Syaroslav@ivinco.com } 20919Syaroslav@ivinco.com 21019Syaroslav@ivinco.com public function setBodyPriority($priority) 21119Syaroslav@ivinco.com { 21219Syaroslav@ivinco.com $this->_bodyPriority = $priority; 21319Syaroslav@ivinco.com } 21419Syaroslav@ivinco.com 21519Syaroslav@ivinco.com public function setCategoriesPriority($priority) 21619Syaroslav@ivinco.com { 21719Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 21819Syaroslav@ivinco.com } 2190Syaroslav@ivinco.com} 220