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)
28 $this->target = $target;
29 $this->table = $table;
36 function limit($offset, $count, $rss = false)
38 return $this->target->limit($offset, $count);
41 function set_sort_mode($mode)
43 if ('chron' === $mode)
44 return $this->target->orderBy('created desc');
48 class SphinxSearch extends SearchEngine {
52 function __construct($target, $table)
54 $fp = @fsockopen(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
56 $this->connected = false;
60 parent::__construct($target, $table);
61 $this->sphinx = new SphinxClient;
62 $this->sphinx->setServer(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
63 $this->connected = true;
66 function is_connected()
68 return $this->connected;
71 function limit($offset, $count, $rss = false)
73 //FIXME without LARGEST_POSSIBLE, the most recent results aren't returned
74 // this probably has a large impact on performance
75 $LARGEST_POSSIBLE = 1e6;
78 $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
81 // return at most 50 pages of results
82 $this->sphinx->setLimits($offset, $count, 50 * ($count - 1), $LARGEST_POSSIBLE);
85 return $this->target->limit(0, $count);
90 $result = $this->sphinx->query($q, $this->table);
91 if (!isset($result['matches'])) return false;
92 $id_set = join(', ', array_keys($result['matches']));
93 $this->target->whereAdd("id in ($id_set)");
97 function set_sort_mode($mode)
99 if ('chron' === $mode) {
100 $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
101 return $this->target->orderBy('created desc');
106 class MySQLSearch extends SearchEngine {
109 if ('identica_people' === $this->table)
110 return $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
111 'against (\''.addslashes($q).'\')');
112 if ('identica_notices' === $this->table)
113 return $this->target->whereAdd('MATCH(content) ' .
114 'against (\''.addslashes($q).'\')');
118 class PGSearch extends SearchEngine {
121 if ('identica_people' === $this->table)
122 return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
123 if ('identica_notices' === $this->table)
124 return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');