xref: /plugin/sphinxsearch-was/SphinxSearch.php (revision 19:d9760852ad64)
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);
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        $pageCrcList = array_keys($res['matches']);
51        $pagesIds = $pageMapper->getByCrc($pageCrcList);
52
53        $pagesList = array();
54        $body = array();
55        $titleText = array();
56        $category = array();
57        foreach ($pageCrcList as $crc){
58            if (!empty($pagesIds[$crc]['hid'])){
59                $bodyHtml = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info);
60            } else {
61                $bodyHtml = p_wiki_xhtml($pagesIds[$crc]['page']);
62            }
63            $body[$crc] = strip_tags($bodyHtml);
64            $titleText[$crc] = strip_tags($pagesIds[$crc]['title_text']);
65            $category[$crc] = $pagesIds[$crc]['page'];
66        }
67
68        $starQuery = $this->starQuery($keywords);
69        $bodyExcerpt = $this->getExcerpt($body, $starQuery);
70        $titleTextExcerpt = $this->getExcerpt($titleText, $starQuery);
71        $i = 0;
72        $results = array();
73        foreach($body as $crc => $notused){
74            $results[$crc] = array(
75                'page' => $pagesIds[$crc]['page'],
76                'bodyExcerpt' => $bodyExcerpt[$i],
77                'titleTextExcerpt' => $titleTextExcerpt[$i],
78                'hid' => $pagesIds[$crc]['hid'],
79                'title' => $pagesIds[$crc]['title'],
80                'title_text' => $pagesIds[$crc]['title_text']
81            );
82            $i++;
83        }
84        return $results;
85    }
86
87    public function getTotalFound()
88    {
89        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
90    }
91
92    public function getExcerpt($data, $query)
93    {
94        return $this->_sphinx->BuildExcerpts($data, $this->_index, $query,
95                    array(
96                        'limit' => $this->_snippetSize,
97                        'around' => $this->_aroundKeyword
98                    )
99                );
100    }
101
102    public function starQuery($query)
103    {
104        $words = explode(" ", $query);
105        $starQuery = '';
106        foreach($words as $word){
107            $starQuery .= "*".$word."* ";
108        }
109        return $starQuery;
110    }
111
112    public function getQuery()
113    {
114        return $this->_query;
115    }
116
117    public function setSnippetSize($symbols = 256)
118    {
119        $this->_snippetSize = $symbols;
120    }
121
122    public function setArroundWordsCount($words = 5)
123    {
124        $this->_aroundKeyword = $words;
125    }
126
127    public function setTitlePriority($priority)
128    {
129        $this->_titlePriority = $priority;
130    }
131
132    public function setBodyPriority($priority)
133    {
134        $this->_bodyPriority = $priority;
135    }
136
137    public function setCategoriesPriority($priority)
138    {
139        $this->_categoriesPriority = $priority;
140    }
141}
142