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 = ''; 1319Syaroslav@ivinco.com 1416Syaroslav@ivinco.com private $_snippetSize = 256; 1516Syaroslav@ivinco.com private $_aroundKeyword = 5; 1619Syaroslav@ivinco.com 1719Syaroslav@ivinco.com private $_titlePriority = 20; 1819Syaroslav@ivinco.com private $_bodyPriority = 5; 1919Syaroslav@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); 2519Syaroslav@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) 3119Syaroslav@ivinco.com { 3219Syaroslav@ivinco.com $this->_sphinx->SetFieldWeights(array('categories' => $this->_categoriesPriority, 'title' => $this->_titlePriority, 'body' => $this->_bodyPriority)); 3319Syaroslav@ivinco.com 34*27Syaroslav@ivinco.com $this->_sphinx->SetLimits($start, $resultsPerPage+100); 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 50*27Syaroslav@ivinco.com $pagesIds = $pageMapper->getByCrc(array_keys($res['matches']), $resultsPerPage); 51*27Syaroslav@ivinco.com if (empty($pagesIds)){ 52*27Syaroslav@ivinco.com 53*27Syaroslav@ivinco.com return false; 54*27Syaroslav@ivinco.com } 550Syaroslav@ivinco.com 560Syaroslav@ivinco.com $pagesList = array(); 5710Syaroslav@ivinco.com $body = array(); 5819Syaroslav@ivinco.com $titleText = array(); 5910Syaroslav@ivinco.com $category = array(); 60*27Syaroslav@ivinco.com foreach ($pagesIds as $crc => $data){ 61*27Syaroslav@ivinco.com if (!empty($data['hid'])){ 62*27Syaroslav@ivinco.com $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info); 636Syaroslav@ivinco.com } else { 64*27Syaroslav@ivinco.com $bodyHtml = p_wiki_xhtml($data['page']); 658Syaroslav@ivinco.com } 6610Syaroslav@ivinco.com $body[$crc] = strip_tags($bodyHtml); 67*27Syaroslav@ivinco.com $titleText[$crc] = strip_tags($data['title_text']); 68*27Syaroslav@ivinco.com $category[$crc] = $data['page']; 6918Syaroslav@ivinco.com } 700Syaroslav@ivinco.com 7115Syaroslav@ivinco.com $starQuery = $this->starQuery($keywords); 7210Syaroslav@ivinco.com $bodyExcerpt = $this->getExcerpt($body, $starQuery); 7319Syaroslav@ivinco.com $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery); 740Syaroslav@ivinco.com $i = 0; 754Syaroslav@ivinco.com $results = array(); 7610Syaroslav@ivinco.com foreach($body as $crc => $notused){ 7710Syaroslav@ivinco.com $results[$crc] = array( 7810Syaroslav@ivinco.com 'page' => $pagesIds[$crc]['page'], 7910Syaroslav@ivinco.com 'bodyExcerpt' => $bodyExcerpt[$i], 8019Syaroslav@ivinco.com 'titleTextExcerpt' => $titleTextExcerpt[$i], 8110Syaroslav@ivinco.com 'hid' => $pagesIds[$crc]['hid'], 8219Syaroslav@ivinco.com 'title' => $pagesIds[$crc]['title'], 8319Syaroslav@ivinco.com 'title_text' => $pagesIds[$crc]['title_text'] 8410Syaroslav@ivinco.com ); 8510Syaroslav@ivinco.com $i++; 860Syaroslav@ivinco.com } 874Syaroslav@ivinco.com return $results; 880Syaroslav@ivinco.com } 890Syaroslav@ivinco.com 900Syaroslav@ivinco.com public function getTotalFound() 910Syaroslav@ivinco.com { 920Syaroslav@ivinco.com return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 930Syaroslav@ivinco.com } 9410Syaroslav@ivinco.com 9510Syaroslav@ivinco.com public function getExcerpt($data, $query) 9610Syaroslav@ivinco.com { 9716Syaroslav@ivinco.com return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 9816Syaroslav@ivinco.com array( 9916Syaroslav@ivinco.com 'limit' => $this->_snippetSize, 10016Syaroslav@ivinco.com 'around' => $this->_aroundKeyword 10116Syaroslav@ivinco.com ) 10216Syaroslav@ivinco.com ); 10310Syaroslav@ivinco.com } 10410Syaroslav@ivinco.com 10510Syaroslav@ivinco.com public function starQuery($query) 10610Syaroslav@ivinco.com { 10710Syaroslav@ivinco.com $words = explode(" ", $query); 10810Syaroslav@ivinco.com $starQuery = ''; 10910Syaroslav@ivinco.com foreach($words as $word){ 11010Syaroslav@ivinco.com $starQuery .= "*".$word."* "; 11110Syaroslav@ivinco.com } 11210Syaroslav@ivinco.com return $starQuery; 11310Syaroslav@ivinco.com } 11415Syaroslav@ivinco.com 11515Syaroslav@ivinco.com public function getQuery() 11615Syaroslav@ivinco.com { 11715Syaroslav@ivinco.com return $this->_query; 11815Syaroslav@ivinco.com } 11916Syaroslav@ivinco.com 12016Syaroslav@ivinco.com public function setSnippetSize($symbols = 256) 12116Syaroslav@ivinco.com { 12216Syaroslav@ivinco.com $this->_snippetSize = $symbols; 12316Syaroslav@ivinco.com } 12416Syaroslav@ivinco.com 12516Syaroslav@ivinco.com public function setArroundWordsCount($words = 5) 12616Syaroslav@ivinco.com { 12716Syaroslav@ivinco.com $this->_aroundKeyword = $words; 12816Syaroslav@ivinco.com } 12919Syaroslav@ivinco.com 13019Syaroslav@ivinco.com public function setTitlePriority($priority) 13119Syaroslav@ivinco.com { 13219Syaroslav@ivinco.com $this->_titlePriority = $priority; 13319Syaroslav@ivinco.com } 13419Syaroslav@ivinco.com 13519Syaroslav@ivinco.com public function setBodyPriority($priority) 13619Syaroslav@ivinco.com { 13719Syaroslav@ivinco.com $this->_bodyPriority = $priority; 13819Syaroslav@ivinco.com } 13919Syaroslav@ivinco.com 14019Syaroslav@ivinco.com public function setCategoriesPriority($priority) 14119Syaroslav@ivinco.com { 14219Syaroslav@ivinco.com $this->_categoriesPriority = $priority; 14319Syaroslav@ivinco.com } 1440Syaroslav@ivinco.com} 145