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;
1669Syaroslav@ivinco.com    private $_resultsPerPage = 10;
1719Syaroslav@ivinco.com
1880Syaroslav@ivinco.com    private $_titlePriority = 1;
1980Syaroslav@ivinco.com    private $_bodyPriority = 1;
2080Syaroslav@ivinco.com    private $_namespacePriority = 1;
2180Syaroslav@ivinco.com    private $_pagenamePriority = 1;
2216Syaroslav@ivinco.com
230Syaroslav@ivinco.com    public function  __construct($host, $port, $index)
240Syaroslav@ivinco.com    {
250Syaroslav@ivinco.com        $this->_sphinx = new SphinxClient();
260Syaroslav@ivinco.com        $this->_sphinx->SetServer($host, $port);
2719Syaroslav@ivinco.com        $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
280Syaroslav@ivinco.com
290Syaroslav@ivinco.com        $this->_index = $index;
300Syaroslav@ivinco.com    }
310Syaroslav@ivinco.com
3269Syaroslav@ivinco.com    public function setSearchAllQuery($keywords, $categories)
3369Syaroslav@ivinco.com    {
3470Syaroslav@ivinco.com        $starKeyword = $this->starQuery($keywords);
3580Syaroslav@ivinco.com        $this->_query = "(@(namespace,pagename) $starKeyword) | (@(body,title) {$keywords})";
3669Syaroslav@ivinco.com    }
3769Syaroslav@ivinco.com
3869Syaroslav@ivinco.com    public function setSearchAllQueryWithCategoryFilter($keywords, $categories)
3919Syaroslav@ivinco.com    {
4069Syaroslav@ivinco.com        $starKeyword = $this->starQuery($keywords);
4169Syaroslav@ivinco.com        if(strpos($categories, "-") === 0){
4269Syaroslav@ivinco.com            $categories = '-"'.substr($categories, 1).'"';
4369Syaroslav@ivinco.com        }
4480Syaroslav@ivinco.com        $this->_query = "(@(namespace,pagename) {$categories}) & ((@(body,title) {$keywords}) | (@(namespace,pagename) {$starKeyword}))";
4569Syaroslav@ivinco.com    }
4669Syaroslav@ivinco.com
4770Syaroslav@ivinco.com    public function setSearchCategoryQuery($keywords, $categories)
4869Syaroslav@ivinco.com    {
4970Syaroslav@ivinco.com        $starKeyword = $this->starQuery($keywords);
5072Syaroslav@ivinco.com        if (!empty($categories)){
5180Syaroslav@ivinco.com            $this->_query = "(@(namespace,pagename) $categories $starKeyword)";
5272Syaroslav@ivinco.com        } else {
5380Syaroslav@ivinco.com            $this->_query = "(@(namespace,pagename) $starKeyword)";
5472Syaroslav@ivinco.com        }
55*83Syaroslav@ivinco.com
5669Syaroslav@ivinco.com    }
5769Syaroslav@ivinco.com
5869Syaroslav@ivinco.com    public function search($start, $resultsPerPage = 10)
5969Syaroslav@ivinco.com    {
6069Syaroslav@ivinco.com        $this->_resultsPerPage = $resultsPerPage;
6169Syaroslav@ivinco.com
6280Syaroslav@ivinco.com        $this->_sphinx->SetFieldWeights(array(
6380Syaroslav@ivinco.com            'namespace' => $this->_namespacePriority,
6480Syaroslav@ivinco.com            'pagename' => $this->_pagenamePriority,
6580Syaroslav@ivinco.com            'title' => $this->_titlePriority,
6680Syaroslav@ivinco.com            'body' => $this->_bodyPriority)
6780Syaroslav@ivinco.com        );
6819Syaroslav@ivinco.com
6927Syaroslav@ivinco.com        $this->_sphinx->SetLimits($start, $resultsPerPage+100);
7069Syaroslav@ivinco.com
7169Syaroslav@ivinco.com        $res = $this->_sphinx->Query($this->_query, $this->_index);
720Syaroslav@ivinco.com        $this->_result = $res;
7328Syaroslav@ivinco.com
740Syaroslav@ivinco.com        if (empty($res['matches'])) {
750Syaroslav@ivinco.com            return false;
760Syaroslav@ivinco.com	}
7769Syaroslav@ivinco.com        return true;
7869Syaroslav@ivinco.com    }
790Syaroslav@ivinco.com
8077Syaroslav@ivinco.com    public function getPages($keywords)
8169Syaroslav@ivinco.com    {
8269Syaroslav@ivinco.com        $pagesIdsAll = $this->getPagesIds();
8339Syaroslav@ivinco.com        $this->_offset = 0;
8439Syaroslav@ivinco.com        $counter = 0;
8543Syaroslav@ivinco.com        $tmpRes = array();
8643Syaroslav@ivinco.com        $pagesIds = array();
8739Syaroslav@ivinco.com        foreach($pagesIdsAll as $id => $pageData){
8839Syaroslav@ivinco.com            $this->_offset++;
8939Syaroslav@ivinco.com            if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){
9043Syaroslav@ivinco.com                if(!isset($tmpRes[$pageData['page']])){
9143Syaroslav@ivinco.com                    $tmpRes[$pageData['page']] = 1;
9243Syaroslav@ivinco.com                    $counter++;
9343Syaroslav@ivinco.com                }
9439Syaroslav@ivinco.com                $pagesIds[$id] = $pageData;
9569Syaroslav@ivinco.com                if ($counter == $this->_resultsPerPage){
9639Syaroslav@ivinco.com                    break;
9739Syaroslav@ivinco.com                }
9839Syaroslav@ivinco.com            }
9939Syaroslav@ivinco.com        }
10039Syaroslav@ivinco.com        if (empty($pagesIds)){
10127Syaroslav@ivinco.com            return false;
10227Syaroslav@ivinco.com        }
1030Syaroslav@ivinco.com
1040Syaroslav@ivinco.com        $pagesList = array();
10510Syaroslav@ivinco.com        $body = array();
10619Syaroslav@ivinco.com        $titleText = array();
10710Syaroslav@ivinco.com        $category = array();
10827Syaroslav@ivinco.com        foreach ($pagesIds as $crc => $data){
10927Syaroslav@ivinco.com            if (!empty($data['hid'])){
11027Syaroslav@ivinco.com                $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info);
1116Syaroslav@ivinco.com            } else {
11227Syaroslav@ivinco.com                $bodyHtml = p_wiki_xhtml($data['page']);
1138Syaroslav@ivinco.com            }
11460Syaroslav@ivinco.com            $bodyHtml = preg_replace("#[\s]+?</li>#", "</li>;", $bodyHtml);
11557Syaroslav@ivinco.com            $bodyHtml = htmlspecialchars_decode($bodyHtml);
11610Syaroslav@ivinco.com            $body[$crc] = strip_tags($bodyHtml);
11779Syaroslav@ivinco.com            if(!empty($data['title_text'])){
11879Syaroslav@ivinco.com                $titleText[$crc] = strip_tags($data['title_text']);
11979Syaroslav@ivinco.com            } else {
12079Syaroslav@ivinco.com                $titleText[$crc] = $data['page'];
12179Syaroslav@ivinco.com            }
12227Syaroslav@ivinco.com            $category[$crc] = $data['page'];
12357Syaroslav@ivinco.com        }
1240Syaroslav@ivinco.com
12580Syaroslav@ivinco.com        //$starQuery = $this->starQuery($keywords);
12680Syaroslav@ivinco.com        $bodyExcerpt = $this->getExcerpt($body, $keywords);
12780Syaroslav@ivinco.com        $titleTextExcerpt = $this->getExcerpt($titleText, $keywords);
1280Syaroslav@ivinco.com        $i = 0;
1294Syaroslav@ivinco.com        $results = array();
13010Syaroslav@ivinco.com        foreach($body as $crc => $notused){
13110Syaroslav@ivinco.com            $results[$crc] = array(
13210Syaroslav@ivinco.com                'page' => $pagesIds[$crc]['page'],
13310Syaroslav@ivinco.com                'bodyExcerpt' => $bodyExcerpt[$i],
13419Syaroslav@ivinco.com                'titleTextExcerpt' => $titleTextExcerpt[$i],
13510Syaroslav@ivinco.com                'hid' => $pagesIds[$crc]['hid'],
13619Syaroslav@ivinco.com                'title' => $pagesIds[$crc]['title'],
13719Syaroslav@ivinco.com                'title_text' => $pagesIds[$crc]['title_text']
13810Syaroslav@ivinco.com            );
13910Syaroslav@ivinco.com            $i++;
1400Syaroslav@ivinco.com        }
1414Syaroslav@ivinco.com        return $results;
1420Syaroslav@ivinco.com    }
1430Syaroslav@ivinco.com
14469Syaroslav@ivinco.com    public function getPagesIds()
14569Syaroslav@ivinco.com    {
14669Syaroslav@ivinco.com        $pageMapper = new PageMapper();
14769Syaroslav@ivinco.com
14869Syaroslav@ivinco.com        return $pageMapper->getByCrc(array_keys($this->_result['matches']));
14969Syaroslav@ivinco.com    }
15069Syaroslav@ivinco.com
15139Syaroslav@ivinco.com    public function getOffset()
15239Syaroslav@ivinco.com    {
15339Syaroslav@ivinco.com        return $this->_offset;
15439Syaroslav@ivinco.com    }
15539Syaroslav@ivinco.com
15628Syaroslav@ivinco.com    public function getError()
15728Syaroslav@ivinco.com    {
15828Syaroslav@ivinco.com        return $this->_sphinx->GetLastError();
15928Syaroslav@ivinco.com    }
16028Syaroslav@ivinco.com
1610Syaroslav@ivinco.com    public function getTotalFound()
1620Syaroslav@ivinco.com    {
1630Syaroslav@ivinco.com        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
1640Syaroslav@ivinco.com    }
16510Syaroslav@ivinco.com
16610Syaroslav@ivinco.com    public function getExcerpt($data, $query)
16710Syaroslav@ivinco.com    {
16816Syaroslav@ivinco.com        return $this->_sphinx->BuildExcerpts($data, $this->_index, $query,
16916Syaroslav@ivinco.com                    array(
17016Syaroslav@ivinco.com                        'limit' => $this->_snippetSize,
17139Syaroslav@ivinco.com                        'around' => $this->_aroundKeyword,
17239Syaroslav@ivinco.com                        'weight_order' => 1,
17339Syaroslav@ivinco.com                        'sp' => 1
17416Syaroslav@ivinco.com                    )
17516Syaroslav@ivinco.com                );
17610Syaroslav@ivinco.com    }
17710Syaroslav@ivinco.com
17810Syaroslav@ivinco.com    public function starQuery($query)
17910Syaroslav@ivinco.com    {
18061Syaroslav@ivinco.com        $query = $this->removeStars($query);
18110Syaroslav@ivinco.com        $words = explode(" ", $query);
18261Syaroslav@ivinco.com        foreach($words as $id => $word){
18361Syaroslav@ivinco.com            $words[$id] = "*".$word."*";
18410Syaroslav@ivinco.com        }
18561Syaroslav@ivinco.com        return implode(" ", $words);
18661Syaroslav@ivinco.com    }
18761Syaroslav@ivinco.com
18861Syaroslav@ivinco.com    public function removeStars($query)
18961Syaroslav@ivinco.com    {
19061Syaroslav@ivinco.com        $words = explode(" ", $query);
19161Syaroslav@ivinco.com        foreach($words as $id => $word){
19261Syaroslav@ivinco.com            $words[$id] = trim($word, "*");
19361Syaroslav@ivinco.com        }
19461Syaroslav@ivinco.com        return implode(" ", $words);
19510Syaroslav@ivinco.com    }
19615Syaroslav@ivinco.com
19715Syaroslav@ivinco.com    public function getQuery()
19815Syaroslav@ivinco.com    {
19915Syaroslav@ivinco.com        return $this->_query;
20015Syaroslav@ivinco.com    }
20116Syaroslav@ivinco.com
20216Syaroslav@ivinco.com    public function setSnippetSize($symbols = 256)
20316Syaroslav@ivinco.com    {
20416Syaroslav@ivinco.com        $this->_snippetSize = $symbols;
20516Syaroslav@ivinco.com    }
20616Syaroslav@ivinco.com
20716Syaroslav@ivinco.com    public function setArroundWordsCount($words = 5)
20816Syaroslav@ivinco.com    {
20916Syaroslav@ivinco.com        $this->_aroundKeyword = $words;
21016Syaroslav@ivinco.com    }
21119Syaroslav@ivinco.com
21219Syaroslav@ivinco.com    public function setTitlePriority($priority)
21319Syaroslav@ivinco.com    {
21419Syaroslav@ivinco.com        $this->_titlePriority = $priority;
21519Syaroslav@ivinco.com    }
21619Syaroslav@ivinco.com
21719Syaroslav@ivinco.com    public function setBodyPriority($priority)
21819Syaroslav@ivinco.com    {
21919Syaroslav@ivinco.com        $this->_bodyPriority = $priority;
22019Syaroslav@ivinco.com    }
22119Syaroslav@ivinco.com
22280Syaroslav@ivinco.com    public function setNamespacePriority($priority)
22319Syaroslav@ivinco.com    {
22480Syaroslav@ivinco.com        $this->_namespacePriority = $priority;
22580Syaroslav@ivinco.com    }
22680Syaroslav@ivinco.com
22780Syaroslav@ivinco.com    public function setPagenamePriority($priority)
22880Syaroslav@ivinco.com    {
22980Syaroslav@ivinco.com        $this->_pagenamePriority = $priority;
23019Syaroslav@ivinco.com    }
2310Syaroslav@ivinco.com}
232