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; 120Syaroslav@ivinco.com public function __construct($host, $port, $index) 130Syaroslav@ivinco.com { 140Syaroslav@ivinco.com $this->_sphinx = new SphinxClient(); 150Syaroslav@ivinco.com $this->_sphinx->SetServer($host, $port); 160Syaroslav@ivinco.com $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 174Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => 5, 'title' => 20, 'body' => 3)); 184Syaroslav@ivinco.com $this->_sphinx->SetFilter('level', array(1)); 190Syaroslav@ivinco.com 200Syaroslav@ivinco.com $this->_index = $index; 210Syaroslav@ivinco.com } 220Syaroslav@ivinco.com 230Syaroslav@ivinco.com public function search($query, $start, $resultsPerPage = 10) 240Syaroslav@ivinco.com { 250Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage); 260Syaroslav@ivinco.com $res = $this->_sphinx->Query($query, $this->_index); 270Syaroslav@ivinco.com $this->_result = $res; 280Syaroslav@ivinco.com if (empty($res['matches'])) { 290Syaroslav@ivinco.com return false; 300Syaroslav@ivinco.com } 310Syaroslav@ivinco.com 320Syaroslav@ivinco.com $pageMapper = new PageMapper(); 330Syaroslav@ivinco.com 340Syaroslav@ivinco.com $pageCrcList = array_keys($res['matches']); 350Syaroslav@ivinco.com $pagesIds = $pageMapper->getByCrc($pageCrcList); 360Syaroslav@ivinco.com 370Syaroslav@ivinco.com $pagesList = array(); 380Syaroslav@ivinco.com foreach ($pageCrcList as $crc){ 396Syaroslav@ivinco.com if (!empty($pagesIds[$crc]['hid'])){ 40*8Syaroslav@ivinco.com $data[$crc] = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info); 416Syaroslav@ivinco.com } else { 426Syaroslav@ivinco.com $data[$crc] = p_wiki_xhtml($pagesIds[$crc]['page']); 43*8Syaroslav@ivinco.com } 44*8Syaroslav@ivinco.com $data[$crc] = strip_tags($data[$crc]); 450Syaroslav@ivinco.com } 460Syaroslav@ivinco.com 470Syaroslav@ivinco.com $pagesExcerpt = $this->_sphinx->BuildExcerpts($data, $this->_index, $query); 480Syaroslav@ivinco.com $i = 0; 494Syaroslav@ivinco.com $results = array(); 504Syaroslav@ivinco.com foreach($data as $crc => $notused){ 514Syaroslav@ivinco.com $results[$crc] = array( 'page' => $pagesIds[$crc]['page'], 'excerpt' => $pagesExcerpt[$i++], 'hid' => $pagesIds[$crc]['hid'], 'title' => $pagesIds[$crc]['title']); 520Syaroslav@ivinco.com } 534Syaroslav@ivinco.com return $results; 540Syaroslav@ivinco.com } 550Syaroslav@ivinco.com 560Syaroslav@ivinco.com public function getTotalFound() 570Syaroslav@ivinco.com { 580Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 590Syaroslav@ivinco.com } 600Syaroslav@ivinco.com} 61