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 = ''; 13*19Syaroslav@ivinco.com 1416Syaroslav@ivinco.com private $_snippetSize = 256; 1516Syaroslav@ivinco.com private $_aroundKeyword = 5; 16*19Syaroslav@ivinco.com 17*19Syaroslav@ivinco.com private $_titlePriority = 20; 18*19Syaroslav@ivinco.com private $_bodyPriority = 5; 19*19Syaroslav@ivinco.com private $_categoriesPriority = 10; 2016Syaroslav@ivinco.com 210Syaroslav@ivinco.com public function __construct($host, $port, $index) 220Syaroslav@ivinco.com { 230Syaroslav@ivinco.com $this->_sphinx = new SphinxClient(); 240Syaroslav@ivinco.com $this->_sphinx->SetServer($host, $port); 25*19Syaroslav@ivinco.com $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 260Syaroslav@ivinco.com 270Syaroslav@ivinco.com $this->_index = $index; 280Syaroslav@ivinco.com } 290Syaroslav@ivinco.com 3015Syaroslav@ivinco.com public function search($keywords, $categories, $start, $resultsPerPage = 10) 31*19Syaroslav@ivinco.com { 32*19Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => $this->_categoriesPriority, 'title' => $this->_titlePriority, 'body' => $this->_bodyPriority)); 33*19Syaroslav@ivinco.com 340Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage); 3515Syaroslav@ivinco.com $query = ''; 3615Syaroslav@ivinco.com if (!empty($keywords) && empty($categories)){ 3715Syaroslav@ivinco.com $query = "@(body,title,categories) {$keywords}"; 3815Syaroslav@ivinco.com } else { 3915Syaroslav@ivinco.com $query = "@(body,title) {$keywords} @categories ".$categories; 4015Syaroslav@ivinco.com } 4115Syaroslav@ivinco.com $this->_query = $query; 420Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 430Syaroslav@ivinco.com $this->_result = $res; 440Syaroslav@ivinco.com if (empty($res['matches'])) { 450Syaroslav@ivinco.com return false; 460Syaroslav@ivinco.com } 470Syaroslav@ivinco.com 480Syaroslav@ivinco.com $pageMapper = new PageMapper(); 490Syaroslav@ivinco.com 500Syaroslav@ivinco.com $pageCrcList = array_keys($res['matches']); 510Syaroslav@ivinco.com $pagesIds = $pageMapper->getByCrc($pageCrcList); 520Syaroslav@ivinco.com 530Syaroslav@ivinco.com $pagesList = array(); 5410Syaroslav@ivinco.com $body = array(); 55*19Syaroslav@ivinco.com $titleText = array(); 5610Syaroslav@ivinco.com $category = array(); 570Syaroslav@ivinco.com foreach ($pageCrcList as $crc){ 586Syaroslav@ivinco.com if (!empty($pagesIds[$crc]['hid'])){ 5910Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info); 606Syaroslav@ivinco.com } else { 6110Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($pagesIds[$crc]['page']); 628Syaroslav@ivinco.com } 6310Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 64*19Syaroslav@ivinco.com $titleText[$crc] = strip_tags($pagesIds[$crc]['title_text']); 6510Syaroslav@ivinco.com $category[$crc] = $pagesIds[$crc]['page']; 6618Syaroslav@ivinco.com } 670Syaroslav@ivinco.com 6815Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 6910Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 70*19Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 710Syaroslav@ivinco.com $i = 0; 724Syaroslav@ivinco.com $results = array(); 7310Syaroslav@ivinco.com foreach($body as $crc => $notused){ 7410Syaroslav@ivinco.com $results[$crc] = array( 7510Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 7610Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 77*19Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 7810Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 79*19Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 80*19Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 8110Syaroslav@ivinco.com ); 8210Syaroslav@ivinco.com $i++; 830Syaroslav@ivinco.com } 844Syaroslav@ivinco.com return $results; 850Syaroslav@ivinco.com } 860Syaroslav@ivinco.com 870Syaroslav@ivinco.com public function getTotalFound() 880Syaroslav@ivinco.com { 890Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 900Syaroslav@ivinco.com } 9110Syaroslav@ivinco.com 9210Syaroslav@ivinco.com public function getExcerpt($data, $query) 9310Syaroslav@ivinco.com { 9416Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 9516Syaroslav@ivinco.com array( 9616Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 9716Syaroslav@ivinco.com 'around' => $this->_aroundKeyword 9816Syaroslav@ivinco.com ) 9916Syaroslav@ivinco.com ); 10010Syaroslav@ivinco.com } 10110Syaroslav@ivinco.com 10210Syaroslav@ivinco.com public function starQuery($query) 10310Syaroslav@ivinco.com { 10410Syaroslav@ivinco.com $words = explode(" ", $query); 10510Syaroslav@ivinco.com $starQuery = ''; 10610Syaroslav@ivinco.com foreach($words as $word){ 10710Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 10810Syaroslav@ivinco.com } 10910Syaroslav@ivinco.com return $starQuery; 11010Syaroslav@ivinco.com } 11115Syaroslav@ivinco.com 11215Syaroslav@ivinco.com public function getQuery() 11315Syaroslav@ivinco.com { 11415Syaroslav@ivinco.com return $this->_query; 11515Syaroslav@ivinco.com } 11616Syaroslav@ivinco.com 11716Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 11816Syaroslav@ivinco.com { 11916Syaroslav@ivinco.com $this->_snippetSize = $symbols; 12016Syaroslav@ivinco.com } 12116Syaroslav@ivinco.com 12216Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 12316Syaroslav@ivinco.com { 12416Syaroslav@ivinco.com $this->_aroundKeyword = $words; 12516Syaroslav@ivinco.com } 126*19Syaroslav@ivinco.com 127*19Syaroslav@ivinco.com public function setTitlePriority($priority) 128*19Syaroslav@ivinco.com { 129*19Syaroslav@ivinco.com $this->_titlePriority = $priority; 130*19Syaroslav@ivinco.com } 131*19Syaroslav@ivinco.com 132*19Syaroslav@ivinco.com public function setBodyPriority($priority) 133*19Syaroslav@ivinco.com { 134*19Syaroslav@ivinco.com $this->_bodyPriority = $priority; 135*19Syaroslav@ivinco.com } 136*19Syaroslav@ivinco.com 137*19Syaroslav@ivinco.com public function setCategoriesPriority($priority) 138*19Syaroslav@ivinco.com { 139*19Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 140*19Syaroslav@ivinco.com } 1410Syaroslav@ivinco.com} 142