3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) { exit(1); }
26 function __construct($target, $table) {
27 $this->target = $target;
28 $this->table = $table;
34 function limit($offset, $count, $rss = false) {
35 return $this->target->limit($offset, $count);
38 function set_sort_mode($mode) {
39 if ('chron' === $mode)
40 return $this->target->orderBy('created desc');
44 class SphinxSearch extends SearchEngine {
48 function __construct($target, $table) {
49 $fp = @fsockopen(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
51 $this->connected = false;
55 parent::__construct($target, $table);
56 $this->sphinx = new SphinxClient;
57 $this->sphinx->setServer(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
58 $this->connected = true;
61 function is_connected() {
62 return $this->connected;
65 function limit($offset, $count, $rss = false) {
66 //FIXME without LARGEST_POSSIBLE, the most recent results aren't returned
67 // this probably has a large impact on performance
68 $LARGEST_POSSIBLE = 1e6;
71 $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
74 // return at most 50 pages of results
75 $this->sphinx->setLimits($offset, $count, 50 * ($count - 1), $LARGEST_POSSIBLE);
78 return $this->target->limit(0, $count);
82 $result = $this->sphinx->query($q, $this->table);
83 if (!isset($result['matches'])) return false;
84 $id_set = join(', ', array_keys($result['matches']));
85 $this->target->whereAdd("id in ($id_set)");
89 function set_sort_mode($mode) {
90 if ('chron' === $mode) {
91 $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
92 return $this->target->orderBy('created desc');
97 class MySQLSearch extends SearchEngine {
99 if ('identica_people' === $this->table)
100 return $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
101 'against (\''.addslashes($q).'\')');
102 if ('identica_notices' === $this->table)
103 return $this->target->whereAdd('MATCH(content) ' .
104 'against (\''.addslashes($q).'\')');
108 class PGSearch extends SearchEngine {
110 if ('identica_people' === $this->table)
111 return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
112 if ('identica_notices' === $this->table)
113 return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');