]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SphinxSearch/sphinxsearch.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / SphinxSearch / sphinxsearch.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 class SphinxSearch extends SearchEngine
25 {
26     private $sphinx;
27     private $connected;
28
29     function __construct($target, $table)
30     {
31         $fp = @fsockopen(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
32         if (!$fp) {
33             $this->connected = false;
34             return;
35         }
36         fclose($fp);
37         parent::__construct($target, $table);
38         $this->sphinx = new SphinxClient;
39         $this->sphinx->setServer(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
40         $this->connected = true;
41     }
42
43     function is_connected()
44     {
45         return $this->connected;
46     }
47
48     function limit($offset, $count, $rss = false)
49     {
50         //FIXME without LARGEST_POSSIBLE, the most recent results aren't returned
51         //      this probably has a large impact on performance
52         $LARGEST_POSSIBLE = 1e6;
53
54         if ($rss) {
55             $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
56         }
57         else {
58             // return at most 50 pages of results
59             $this->sphinx->setLimits($offset, $count, 50 * ($count - 1), $LARGEST_POSSIBLE);
60         }
61
62         return $this->target->limit(0, $count);
63     }
64
65     function query($q)
66     {
67         $result = $this->sphinx->query($q, $this->remote_table());
68         if (!isset($result['matches'])) return false;
69         $id_set = join(', ', array_keys($result['matches']));
70         $this->target->whereAdd("id in ($id_set)");
71         return true;
72      }
73
74     function set_sort_mode($mode)
75     {
76         if ('chron' === $mode) {
77             $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
78             return $this->target->orderBy('created desc');
79         }
80     }
81
82     function remote_table()
83     {
84         return $this->dbname() . '_' . $this->table;
85     }
86
87     function dbname()
88     {
89         // @fixme there should be a less dreadful way to do this.
90         // DB objects won't give database back until they connect, it's confusing
91         if (preg_match('!^.*?://.*?:.*?@.*?/(.*?)$!', common_config('db', 'database'), $matches)) {
92             return $matches[1];
93         }
94         throw new ServerException("Sphinx search could not identify database name");
95     }
96 }