xref: /plugin/sphinxsearch-was/SphinxSearch.php (revision 8:5afb3def6f44)
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        foreach ($pageCrcList as $crc){
39            if (!empty($pagesIds[$crc]['hid'])){
40                $data[$crc] = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info);
41            } else {
42                $data[$crc] = p_wiki_xhtml($pagesIds[$crc]['page']);
43            }
44            $data[$crc] = strip_tags($data[$crc]);
45        }
46
47        $pagesExcerpt = $this->_sphinx->BuildExcerpts($data, $this->_index, $query);
48        $i = 0;
49        $results = array();
50        foreach($data as $crc => $notused){
51            $results[$crc] = array( 'page' => $pagesIds[$crc]['page'], 'excerpt' => $pagesExcerpt[$i++], 'hid' => $pagesIds[$crc]['hid'], 'title' => $pagesIds[$crc]['title']);
52        }
53        return $results;
54    }
55
56    public function getTotalFound()
57    {
58        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
59    }
60}
61