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
3427Syaroslav@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;
44*28Syaroslav@ivinco.com
450Syaroslav@ivinco.com        if (empty($res['matches'])) {
460Syaroslav@ivinco.com            return false;
470Syaroslav@ivinco.com	}
480Syaroslav@ivinco.com
490Syaroslav@ivinco.com        $pageMapper = new PageMapper();
500Syaroslav@ivinco.com
5127Syaroslav@ivinco.com        $pagesIds = $pageMapper->getByCrc(array_keys($res['matches']), $resultsPerPage);
5227Syaroslav@ivinco.com        if (empty($pagesIds)){
5327Syaroslav@ivinco.com
5427Syaroslav@ivinco.com            return false;
5527Syaroslav@ivinco.com        }
560Syaroslav@ivinco.com
570Syaroslav@ivinco.com        $pagesList = array();
5810Syaroslav@ivinco.com        $body = array();
5919Syaroslav@ivinco.com        $titleText = array();
6010Syaroslav@ivinco.com        $category = array();
6127Syaroslav@ivinco.com        foreach ($pagesIds as $crc => $data){
6227Syaroslav@ivinco.com            if (!empty($data['hid'])){
6327Syaroslav@ivinco.com                $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info);
646Syaroslav@ivinco.com            } else {
6527Syaroslav@ivinco.com                $bodyHtml = p_wiki_xhtml($data['page']);
668Syaroslav@ivinco.com            }
6710Syaroslav@ivinco.com            $body[$crc] = strip_tags($bodyHtml);
6827Syaroslav@ivinco.com            $titleText[$crc] = strip_tags($data['title_text']);
6927Syaroslav@ivinco.com            $category[$crc] = $data['page'];
7018Syaroslav@ivinco.com        }
710Syaroslav@ivinco.com
7215Syaroslav@ivinco.com        $starQuery = $this->starQuery($keywords);
7310Syaroslav@ivinco.com        $bodyExcerpt = $this->getExcerpt($body, $starQuery);
7419Syaroslav@ivinco.com        $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery);
750Syaroslav@ivinco.com        $i = 0;
764Syaroslav@ivinco.com        $results = array();
7710Syaroslav@ivinco.com        foreach($body as $crc => $notused){
7810Syaroslav@ivinco.com            $results[$crc] = array(
7910Syaroslav@ivinco.com                'page' => $pagesIds[$crc]['page'],
8010Syaroslav@ivinco.com                'bodyExcerpt' => $bodyExcerpt[$i],
8119Syaroslav@ivinco.com                'titleTextExcerpt' => $titleTextExcerpt[$i],
8210Syaroslav@ivinco.com                'hid' => $pagesIds[$crc]['hid'],
8319Syaroslav@ivinco.com                'title' => $pagesIds[$crc]['title'],
8419Syaroslav@ivinco.com                'title_text' => $pagesIds[$crc]['title_text']
8510Syaroslav@ivinco.com            );
8610Syaroslav@ivinco.com            $i++;
870Syaroslav@ivinco.com        }
884Syaroslav@ivinco.com        return $results;
890Syaroslav@ivinco.com    }
900Syaroslav@ivinco.com
91*28Syaroslav@ivinco.com    public function getError()
92*28Syaroslav@ivinco.com    {
93*28Syaroslav@ivinco.com        return $this->_sphinx->GetLastError();
94*28Syaroslav@ivinco.com    }
95*28Syaroslav@ivinco.com
960Syaroslav@ivinco.com    public function getTotalFound()
970Syaroslav@ivinco.com    {
980Syaroslav@ivinco.com        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
990Syaroslav@ivinco.com    }
10010Syaroslav@ivinco.com
10110Syaroslav@ivinco.com    public function getExcerpt($data, $query)
10210Syaroslav@ivinco.com    {
10316Syaroslav@ivinco.com        return $this->_sphinx->BuildExcerpts($data, $this->_index, $query,
10416Syaroslav@ivinco.com                    array(
10516Syaroslav@ivinco.com                        'limit' => $this->_snippetSize,
10616Syaroslav@ivinco.com                        'around' => $this->_aroundKeyword
10716Syaroslav@ivinco.com                    )
10816Syaroslav@ivinco.com                );
10910Syaroslav@ivinco.com    }
11010Syaroslav@ivinco.com
11110Syaroslav@ivinco.com    public function starQuery($query)
11210Syaroslav@ivinco.com    {
11310Syaroslav@ivinco.com        $words = explode(" ", $query);
11410Syaroslav@ivinco.com        $starQuery = '';
11510Syaroslav@ivinco.com        foreach($words as $word){
11610Syaroslav@ivinco.com            $starQuery .= "*".$word."* ";
11710Syaroslav@ivinco.com        }
11810Syaroslav@ivinco.com        return $starQuery;
11910Syaroslav@ivinco.com    }
12015Syaroslav@ivinco.com
12115Syaroslav@ivinco.com    public function getQuery()
12215Syaroslav@ivinco.com    {
12315Syaroslav@ivinco.com        return $this->_query;
12415Syaroslav@ivinco.com    }
12516Syaroslav@ivinco.com
12616Syaroslav@ivinco.com    public function setSnippetSize($symbols = 256)
12716Syaroslav@ivinco.com    {
12816Syaroslav@ivinco.com        $this->_snippetSize = $symbols;
12916Syaroslav@ivinco.com    }
13016Syaroslav@ivinco.com
13116Syaroslav@ivinco.com    public function setArroundWordsCount($words = 5)
13216Syaroslav@ivinco.com    {
13316Syaroslav@ivinco.com        $this->_aroundKeyword = $words;
13416Syaroslav@ivinco.com    }
13519Syaroslav@ivinco.com
13619Syaroslav@ivinco.com    public function setTitlePriority($priority)
13719Syaroslav@ivinco.com    {
13819Syaroslav@ivinco.com        $this->_titlePriority = $priority;
13919Syaroslav@ivinco.com    }
14019Syaroslav@ivinco.com
14119Syaroslav@ivinco.com    public function setBodyPriority($priority)
14219Syaroslav@ivinco.com    {
14319Syaroslav@ivinco.com        $this->_bodyPriority = $priority;
14419Syaroslav@ivinco.com    }
14519Syaroslav@ivinco.com
14619Syaroslav@ivinco.com    public function setCategoriesPriority($priority)
14719Syaroslav@ivinco.com    {
14819Syaroslav@ivinco.com        $this->_categoriesPriority = $priority;
14919Syaroslav@ivinco.com    }
1500Syaroslav@ivinco.com}
151