xref: /plugin/sphinxsearch-was/SphinxSearch.php (revision 10:b291a212111f)
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    public function  __construct($host, $port, $index)
13    {
14        $this->_sphinx = new SphinxClient();
15        $this->_sphinx->SetServer($host, $port);
16        $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
17        $this->_sphinx->SetFieldWeights(array('categories' => 5, 'title' => 20, 'body' => 3));
18        $this->_sphinx->SetFilter('level', array(1));
19
20        $this->_index = $index;
21    }
22
23    public function search($query, $start, $resultsPerPage = 10)
24    {
25        $this->_sphinx->SetLimits($start, $resultsPerPage);
26        $res = $this->_sphinx->Query($query, $this->_index);
27        $this->_result = $res;
28        if (empty($res['matches'])) {
29            return false;
30	}
31
32        $pageMapper = new PageMapper();
33
34        $pageCrcList = array_keys($res['matches']);
35        $pagesIds = $pageMapper->getByCrc($pageCrcList);
36
37        $pagesList = array();
38        $body = array();
39        $title = array();
40        $category = array();
41        foreach ($pageCrcList as $crc){
42            if (!empty($pagesIds[$crc]['hid'])){
43                $bodyHtml = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info);
44            } else {
45                $bodyHtml = p_wiki_xhtml($pagesIds[$crc]['page']);
46            }
47            $body[$crc] = strip_tags($bodyHtml);
48            $title[$crc] = strip_tags($pagesIds[$crc]['title']);
49            $category[$crc] = $pagesIds[$crc]['page'];
50        }
51
52        $starQuery = $this->starQuery($query);
53        $bodyExcerpt = $this->getExcerpt($body, $starQuery);
54        $titleExcerpt = $this->getExcerpt($title, $starQuery);
55        $i = 0;
56        $results = array();
57        foreach($body as $crc => $notused){
58            $results[$crc] = array(
59                'page' => $pagesIds[$crc]['page'],
60                'bodyExcerpt' => $bodyExcerpt[$i],
61                'titleExcerpt' => $titleExcerpt[$i],
62                'hid' => $pagesIds[$crc]['hid'],
63                'title' => $pagesIds[$crc]['title']
64            );
65            $i++;
66        }
67        return $results;
68    }
69
70    public function getTotalFound()
71    {
72        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
73    }
74
75    public function getExcerpt($data, $query)
76    {
77        return $this->_sphinx->BuildExcerpts($data, $this->_index, $query);
78    }
79
80    public function starQuery($query)
81    {
82        $words = explode(" ", $query);
83        $starQuery = '';
84        foreach($words as $word){
85            $starQuery .= "*".$word."* ";
86        }
87        return $starQuery;
88    }
89}
90