xref: /plugin/sphinxsearch-was/SphinxSearch.php (revision 57:828502681ea9)
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            $starCategory = $this->starQuery($keywords);
38            $query = "(@(body,title) {$keywords} | (@categories {$starCategory}))";
39            //echo $query;exit;
40        } else {
41            $starCategory = $this->starQuery($categories);
42            $query = "(@(body,title,categories) {$keywords} (@categories ".$starCategory."))";
43        }
44        $this->_query = $query;
45        $res = $this->_sphinx->Query($query, $this->_index);
46        $this->_result = $res;
47
48        if (empty($res['matches'])) {
49            return false;
50	}
51
52        $pageMapper = new PageMapper();
53
54        $pagesIdsAll = $pageMapper->getByCrc(array_keys($res['matches']));
55        $this->_offset = 0;
56        $counter = 0;
57        $tmpRes = array();
58        $pagesIds = array();
59        foreach($pagesIdsAll as $id => $pageData){
60            $this->_offset++;
61            if(auth_quickaclcheck($pageData['page']) >= AUTH_READ){
62                if(!isset($tmpRes[$pageData['page']])){
63                    $tmpRes[$pageData['page']] = 1;
64                    $counter++;
65                }
66                $pagesIds[$id] = $pageData;
67                if ($counter == $resultsPerPage){
68                    break;
69                }
70            }
71        }
72        if (empty($pagesIds)){
73            return false;
74        }
75
76        $pagesList = array();
77        $body = array();
78        $titleText = array();
79        $category = array();
80        foreach ($pagesIds as $crc => $data){
81            if (!empty($data['hid'])){
82                $bodyHtml = p_render('xhtml',p_get_instructions(getSectionByTitleLevel($data['page'], $data['title'], true)),$info);
83            } else {
84                $bodyHtml = p_wiki_xhtml($data['page']);
85            }
86            $bodyHtml = preg_replace("#</li>#", "</li>;", $bodyHtml);
87            $bodyHtml = htmlspecialchars_decode($bodyHtml);
88            $body[$crc] = strip_tags($bodyHtml);
89            $titleText[$crc] = strip_tags($data['title_text']);
90            $category[$crc] = $data['page'];
91        }
92
93        $starQuery = $this->starQuery($keywords);
94        $bodyExcerpt = $this->getExcerpt($body, $starQuery);
95        $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery);
96        $i = 0;
97        $results = array();
98        foreach($body as $crc => $notused){
99            $results[$crc] = array(
100                'page' => $pagesIds[$crc]['page'],
101                'bodyExcerpt' => $bodyExcerpt[$i],
102                'titleTextExcerpt' => $titleTextExcerpt[$i],
103                'hid' => $pagesIds[$crc]['hid'],
104                'title' => $pagesIds[$crc]['title'],
105                'title_text' => $pagesIds[$crc]['title_text']
106            );
107            $i++;
108        }
109        return $results;
110    }
111
112    public function getOffset()
113    {
114        return $this->_offset;
115    }
116
117    public function getError()
118    {
119        return $this->_sphinx->GetLastError();
120    }
121
122    public function getTotalFound()
123    {
124        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
125    }
126
127    public function getExcerpt($data, $query)
128    {
129        return $this->_sphinx->BuildExcerpts($data, $this->_index, $query,
130                    array(
131                        'limit' => $this->_snippetSize,
132                        'around' => $this->_aroundKeyword,
133                        'weight_order' => 1,
134                        'sp' => 1
135                    )
136                );
137    }
138
139    public function starQuery($query)
140    {
141        $words = explode(" ", $query);
142        $starQuery = '';
143        foreach($words as $word){
144            $word = trim($word, "*");
145            $starQuery .= "*".$word."* ";
146        }
147        return $starQuery;
148    }
149
150    public function getQuery()
151    {
152        return $this->_query;
153    }
154
155    public function setSnippetSize($symbols = 256)
156    {
157        $this->_snippetSize = $symbols;
158    }
159
160    public function setArroundWordsCount($words = 5)
161    {
162        $this->_aroundKeyword = $words;
163    }
164
165    public function setTitlePriority($priority)
166    {
167        $this->_titlePriority = $priority;
168    }
169
170    public function setBodyPriority($priority)
171    {
172        $this->_bodyPriority = $priority;
173    }
174
175    public function setCategoriesPriority($priority)
176    {
177        $this->_categoriesPriority = $priority;
178    }
179}
180