xref: /plugin/sphinxsearch-was/SphinxSearch.php (revision 6:27deea028915)
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        }
45
46        $pagesExcerpt = $this->_sphinx->BuildExcerpts($data, $this->_index, $query);
47        $i = 0;
48        $results = array();
49        foreach($data as $crc => $notused){
50            $results[$crc] = array( 'page' => $pagesIds[$crc]['page'], 'excerpt' => $pagesExcerpt[$i++], 'hid' => $pagesIds[$crc]['hid'], 'title' => $pagesIds[$crc]['title']);
51        }
52        return $results;
53    }
54
55    public function getTotalFound()
56    {
57        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
58    }
59}
60