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) 24*10Syaroslav@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(); 38*10Syaroslav@ivinco.com $body = array(); 39*10Syaroslav@ivinco.com $title = array(); 40*10Syaroslav@ivinco.com $category = array(); 410Syaroslav@ivinco.com foreach ($pageCrcList as $crc){ 426Syaroslav@ivinco.com if (!empty($pagesIds[$crc]['hid'])){ 43*10Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info); 446Syaroslav@ivinco.com } else { 45*10Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($pagesIds[$crc]['page']); 468Syaroslav@ivinco.com } 47*10Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 48*10Syaroslav@ivinco.com $title[$crc] = strip_tags($pagesIds[$crc]['title']); 49*10Syaroslav@ivinco.com $category[$crc] = $pagesIds[$crc]['page']; 500Syaroslav@ivinco.com } 510Syaroslav@ivinco.com 52*10Syaroslav@ivinco.com $starQuery = $this->starQuery($query); 53*10Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 54*10Syaroslav@ivinco.com $titleExcerpt = $this->getExcerpt($title, $starQuery); 550Syaroslav@ivinco.com $i = 0; 564Syaroslav@ivinco.com $results = array(); 57*10Syaroslav@ivinco.com foreach($body as $crc => $notused){ 58*10Syaroslav@ivinco.com $results[$crc] = array( 59*10Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 60*10Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 61*10Syaroslav@ivinco.com 'titleExcerpt' => $titleExcerpt[$i], 62*10Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 63*10Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'] 64*10Syaroslav@ivinco.com ); 65*10Syaroslav@ivinco.com $i++; 660Syaroslav@ivinco.com } 674Syaroslav@ivinco.com return $results; 680Syaroslav@ivinco.com } 690Syaroslav@ivinco.com 700Syaroslav@ivinco.com public function getTotalFound() 710Syaroslav@ivinco.com { 720Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 730Syaroslav@ivinco.com } 74*10Syaroslav@ivinco.com 75*10Syaroslav@ivinco.com public function getExcerpt($data, $query) 76*10Syaroslav@ivinco.com { 77*10Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query); 78*10Syaroslav@ivinco.com } 79*10Syaroslav@ivinco.com 80*10Syaroslav@ivinco.com public function starQuery($query) 81*10Syaroslav@ivinco.com { 82*10Syaroslav@ivinco.com $words = explode(" ", $query); 83*10Syaroslav@ivinco.com $starQuery = ''; 84*10Syaroslav@ivinco.com foreach($words as $word){ 85*10Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 86*10Syaroslav@ivinco.com } 87*10Syaroslav@ivinco.com return $starQuery; 88*10Syaroslav@ivinco.com } 890Syaroslav@ivinco.com} 90