xref: /plugin/sphinxsearch-was/SphinxSearch.php (revision 27:fbc14c95b41d)
1<?php
2/*
3 * To change this template, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7class SphinxSearch
8{
9    private $_sphinx = null;
10    private $_result = array();
11    private $_index = null;
12    private $_query = '';
13
14    private $_snippetSize = 256;
15    private $_aroundKeyword = 5;
16
17    private $_titlePriority = 20;
18    private $_bodyPriority = 5;
19    private $_categoriesPriority = 10;
20
21    public function  __construct($host, $port, $index)
22    {
23        $this->_sphinx = new SphinxClient();
24        $this->_sphinx->SetServer($host, $port);
25        $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
26
27        $this->_index = $index;
28    }
29
30    public function search($keywords, $categories, $start, $resultsPerPage = 10)
31    {
32        $this->_sphinx->SetFieldWeights(array('categories' => $this->_categoriesPriority, 'title' => $this->_titlePriority, 'body' => $this->_bodyPriority));
33
34        $this->_sphinx->SetLimits($start, $resultsPerPage+100);
35        $query = '';
36        if (!empty($keywords) && empty($categories)){
37            $query = "@(body,title,categories) {$keywords}";
38        } else {
39            $query = "@(body,title) {$keywords} @categories ".$categories;
40        }
41        $this->_query = $query;
42        $res = $this->_sphinx->Query($query, $this->_index);
43        $this->_result = $res;
44        if (empty($res['matches'])) {
45            return false;
46	}
47
48        $pageMapper = new PageMapper();
49
50        $pagesIds = $pageMapper->getByCrc(array_keys($res['matches']), $resultsPerPage);
51        if (empty($pagesIds)){
52
53            return false;
54        }
55
56        $pagesList = array();
57        $body = array();
58        $titleText = array();
59        $category = array();
60        foreach ($pagesIds as $crc => $data){
61            if (!empty($data['hid'])){
62                $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info);
63            } else {
64                $bodyHtml = p_wiki_xhtml($data['page']);
65            }
66            $body[$crc] = strip_tags($bodyHtml);
67            $titleText[$crc] = strip_tags($data['title_text']);
68            $category[$crc] = $data['page'];
69        }
70
71        $starQuery = $this->starQuery($keywords);
72        $bodyExcerpt = $this->getExcerpt($body, $starQuery);
73        $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery);
74        $i = 0;
75        $results = array();
76        foreach($body as $crc => $notused){
77            $results[$crc] = array(
78                'page' => $pagesIds[$crc]['page'],
79                'bodyExcerpt' => $bodyExcerpt[$i],
80                'titleTextExcerpt' => $titleTextExcerpt[$i],
81                'hid' => $pagesIds[$crc]['hid'],
82                'title' => $pagesIds[$crc]['title'],
83                'title_text' => $pagesIds[$crc]['title_text']
84            );
85            $i++;
86        }
87        return $results;
88    }
89
90    public function getTotalFound()
91    {
92        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
93    }
94
95    public function getExcerpt($data, $query)
96    {
97        return $this->_sphinx->BuildExcerpts($data, $this->_index, $query,
98                    array(
99                        'limit' => $this->_snippetSize,
100                        'around' => $this->_aroundKeyword
101                    )
102                );
103    }
104
105    public function starQuery($query)
106    {
107        $words = explode(" ", $query);
108        $starQuery = '';
109        foreach($words as $word){
110            $starQuery .= "*".$word."* ";
111        }
112        return $starQuery;
113    }
114
115    public function getQuery()
116    {
117        return $this->_query;
118    }
119
120    public function setSnippetSize($symbols = 256)
121    {
122        $this->_snippetSize = $symbols;
123    }
124
125    public function setArroundWordsCount($words = 5)
126    {
127        $this->_aroundKeyword = $words;
128    }
129
130    public function setTitlePriority($priority)
131    {
132        $this->_titlePriority = $priority;
133    }
134
135    public function setBodyPriority($priority)
136    {
137        $this->_bodyPriority = $priority;
138    }
139
140    public function setCategoriesPriority($priority)
141    {
142        $this->_categoriesPriority = $priority;
143    }
144}
145