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; 12*15Syaroslav@ivinco.com private $_query = ''; 130Syaroslav@ivinco.com public function __construct($host, $port, $index) 140Syaroslav@ivinco.com { 150Syaroslav@ivinco.com $this->_sphinx = new SphinxClient(); 160Syaroslav@ivinco.com $this->_sphinx->SetServer($host, $port); 170Syaroslav@ivinco.com $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 184Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => 5, 'title' => 20, 'body' => 3)); 194Syaroslav@ivinco.com $this->_sphinx->SetFilter('level', array(1)); 200Syaroslav@ivinco.com 210Syaroslav@ivinco.com $this->_index = $index; 220Syaroslav@ivinco.com } 230Syaroslav@ivinco.com 24*15Syaroslav@ivinco.com public function search($keywords, $categories, $start, $resultsPerPage = 10) 2510Syaroslav@ivinco.com { 260Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage); 27*15Syaroslav@ivinco.com $query = ''; 28*15Syaroslav@ivinco.com if (!empty($keywords) && empty($categories)){ 29*15Syaroslav@ivinco.com $query = "@(body,title,categories) {$keywords}"; 30*15Syaroslav@ivinco.com } else { 31*15Syaroslav@ivinco.com $query = "@(body,title) {$keywords} @categories ".$categories; 32*15Syaroslav@ivinco.com } 33*15Syaroslav@ivinco.com $this->_query = $query; 340Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 350Syaroslav@ivinco.com $this->_result = $res; 360Syaroslav@ivinco.com if (empty($res['matches'])) { 370Syaroslav@ivinco.com return false; 380Syaroslav@ivinco.com } 390Syaroslav@ivinco.com 400Syaroslav@ivinco.com $pageMapper = new PageMapper(); 410Syaroslav@ivinco.com 420Syaroslav@ivinco.com $pageCrcList = array_keys($res['matches']); 430Syaroslav@ivinco.com $pagesIds = $pageMapper->getByCrc($pageCrcList); 440Syaroslav@ivinco.com 450Syaroslav@ivinco.com $pagesList = array(); 4610Syaroslav@ivinco.com $body = array(); 4710Syaroslav@ivinco.com $title = array(); 4810Syaroslav@ivinco.com $category = array(); 490Syaroslav@ivinco.com foreach ($pageCrcList as $crc){ 506Syaroslav@ivinco.com if (!empty($pagesIds[$crc]['hid'])){ 5110Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info); 526Syaroslav@ivinco.com } else { 5310Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($pagesIds[$crc]['page']); 548Syaroslav@ivinco.com } 5510Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 5610Syaroslav@ivinco.com $title[$crc] = strip_tags($pagesIds[$crc]['title']); 5710Syaroslav@ivinco.com $category[$crc] = $pagesIds[$crc]['page']; 580Syaroslav@ivinco.com } 590Syaroslav@ivinco.com 60*15Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 6110Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 6210Syaroslav@ivinco.com $titleExcerpt = $this->getExcerpt($title, $starQuery); 630Syaroslav@ivinco.com $i = 0; 644Syaroslav@ivinco.com $results = array(); 6510Syaroslav@ivinco.com foreach($body as $crc => $notused){ 6610Syaroslav@ivinco.com $results[$crc] = array( 6710Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 6810Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 6910Syaroslav@ivinco.com 'titleExcerpt' => $titleExcerpt[$i], 7010Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 7110Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'] 7210Syaroslav@ivinco.com ); 7310Syaroslav@ivinco.com $i++; 740Syaroslav@ivinco.com } 754Syaroslav@ivinco.com return $results; 760Syaroslav@ivinco.com } 770Syaroslav@ivinco.com 780Syaroslav@ivinco.com public function getTotalFound() 790Syaroslav@ivinco.com { 800Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 810Syaroslav@ivinco.com } 8210Syaroslav@ivinco.com 8310Syaroslav@ivinco.com public function getExcerpt($data, $query) 8410Syaroslav@ivinco.com { 8510Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query); 8610Syaroslav@ivinco.com } 8710Syaroslav@ivinco.com 8810Syaroslav@ivinco.com public function starQuery($query) 8910Syaroslav@ivinco.com { 9010Syaroslav@ivinco.com $words = explode(" ", $query); 9110Syaroslav@ivinco.com $starQuery = ''; 9210Syaroslav@ivinco.com foreach($words as $word){ 9310Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 9410Syaroslav@ivinco.com } 9510Syaroslav@ivinco.com return $starQuery; 9610Syaroslav@ivinco.com } 97*15Syaroslav@ivinco.com 98*15Syaroslav@ivinco.com public function getQuery() 99*15Syaroslav@ivinco.com { 100*15Syaroslav@ivinco.com return $this->_query; 101*15Syaroslav@ivinco.com } 1020Syaroslav@ivinco.com} 103