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*16Syaroslav@ivinco.com private $_snippetSize = 256; 14*16Syaroslav@ivinco.com private $_aroundKeyword = 5; 15*16Syaroslav@ivinco.com 160Syaroslav@ivinco.com public function __construct($host, $port, $index) 170Syaroslav@ivinco.com { 180Syaroslav@ivinco.com $this->_sphinx = new SphinxClient(); 190Syaroslav@ivinco.com $this->_sphinx->SetServer($host, $port); 200Syaroslav@ivinco.com $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 214Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => 5, 'title' => 20, 'body' => 3)); 224Syaroslav@ivinco.com $this->_sphinx->SetFilter('level', array(1)); 230Syaroslav@ivinco.com 240Syaroslav@ivinco.com $this->_index = $index; 250Syaroslav@ivinco.com } 260Syaroslav@ivinco.com 2715Syaroslav@ivinco.com public function search($keywords, $categories, $start, $resultsPerPage = 10) 2810Syaroslav@ivinco.com { 290Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage); 3015Syaroslav@ivinco.com $query = ''; 3115Syaroslav@ivinco.com if (!empty($keywords) && empty($categories)){ 3215Syaroslav@ivinco.com $query = "@(body,title,categories) {$keywords}"; 3315Syaroslav@ivinco.com } else { 3415Syaroslav@ivinco.com $query = "@(body,title) {$keywords} @categories ".$categories; 3515Syaroslav@ivinco.com } 3615Syaroslav@ivinco.com $this->_query = $query; 370Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 380Syaroslav@ivinco.com $this->_result = $res; 390Syaroslav@ivinco.com if (empty($res['matches'])) { 400Syaroslav@ivinco.com return false; 410Syaroslav@ivinco.com } 420Syaroslav@ivinco.com 430Syaroslav@ivinco.com $pageMapper = new PageMapper(); 440Syaroslav@ivinco.com 450Syaroslav@ivinco.com $pageCrcList = array_keys($res['matches']); 460Syaroslav@ivinco.com $pagesIds = $pageMapper->getByCrc($pageCrcList); 470Syaroslav@ivinco.com 480Syaroslav@ivinco.com $pagesList = array(); 4910Syaroslav@ivinco.com $body = array(); 5010Syaroslav@ivinco.com $title = array(); 5110Syaroslav@ivinco.com $category = array(); 520Syaroslav@ivinco.com foreach ($pageCrcList as $crc){ 536Syaroslav@ivinco.com if (!empty($pagesIds[$crc]['hid'])){ 5410Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info); 556Syaroslav@ivinco.com } else { 5610Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($pagesIds[$crc]['page']); 578Syaroslav@ivinco.com } 5810Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 5910Syaroslav@ivinco.com $title[$crc] = strip_tags($pagesIds[$crc]['title']); 6010Syaroslav@ivinco.com $category[$crc] = $pagesIds[$crc]['page']; 610Syaroslav@ivinco.com } 620Syaroslav@ivinco.com 6315Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 6410Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 6510Syaroslav@ivinco.com $titleExcerpt = $this->getExcerpt($title, $starQuery); 660Syaroslav@ivinco.com $i = 0; 674Syaroslav@ivinco.com $results = array(); 6810Syaroslav@ivinco.com foreach($body as $crc => $notused){ 6910Syaroslav@ivinco.com $results[$crc] = array( 7010Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 7110Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 7210Syaroslav@ivinco.com 'titleExcerpt' => $titleExcerpt[$i], 7310Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 7410Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'] 7510Syaroslav@ivinco.com ); 7610Syaroslav@ivinco.com $i++; 770Syaroslav@ivinco.com } 784Syaroslav@ivinco.com return $results; 790Syaroslav@ivinco.com } 800Syaroslav@ivinco.com 810Syaroslav@ivinco.com public function getTotalFound() 820Syaroslav@ivinco.com { 830Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 840Syaroslav@ivinco.com } 8510Syaroslav@ivinco.com 8610Syaroslav@ivinco.com public function getExcerpt($data, $query) 8710Syaroslav@ivinco.com { 88*16Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 89*16Syaroslav@ivinco.com array( 90*16Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 91*16Syaroslav@ivinco.com 'around' => $this->_aroundKeyword 92*16Syaroslav@ivinco.com ) 93*16Syaroslav@ivinco.com ); 9410Syaroslav@ivinco.com } 9510Syaroslav@ivinco.com 9610Syaroslav@ivinco.com public function starQuery($query) 9710Syaroslav@ivinco.com { 9810Syaroslav@ivinco.com $words = explode(" ", $query); 9910Syaroslav@ivinco.com $starQuery = ''; 10010Syaroslav@ivinco.com foreach($words as $word){ 10110Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 10210Syaroslav@ivinco.com } 10310Syaroslav@ivinco.com return $starQuery; 10410Syaroslav@ivinco.com } 10515Syaroslav@ivinco.com 10615Syaroslav@ivinco.com public function getQuery() 10715Syaroslav@ivinco.com { 10815Syaroslav@ivinco.com return $this->_query; 10915Syaroslav@ivinco.com } 110*16Syaroslav@ivinco.com 111*16Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 112*16Syaroslav@ivinco.com { 113*16Syaroslav@ivinco.com $this->_snippetSize = $symbols; 114*16Syaroslav@ivinco.com } 115*16Syaroslav@ivinco.com 116*16Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 117*16Syaroslav@ivinco.com { 118*16Syaroslav@ivinco.com $this->_aroundKeyword = $words; 119*16Syaroslav@ivinco.com } 1200Syaroslav@ivinco.com} 121