3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET')) {
24 class SphinxSearch extends SearchEngine
29 function __construct($target, $table)
31 $fp = @fsockopen(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
33 $this->connected = false;
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;
43 function is_connected()
45 return $this->connected;
48 function limit($offset, $count, $rss = false)
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;
55 $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
58 // return at most 50 pages of results
59 $this->sphinx->setLimits($offset, $count, 50 * ($count - 1), $LARGEST_POSSIBLE);
62 return $this->target->limit(0, $count);
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)");
74 function set_sort_mode($mode)
76 if ('chron' === $mode) {
77 $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
78 return $this->target->orderBy('id desc');
82 function remote_table()
84 return $this->dbname() . '_' . $this->table;
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)) {
95 // TRANS: Server exception thrown when a database name cannot be identified.
96 throw new ServerException(_m("Sphinx search could not identify database name."));