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); }
27 function __construct($target, $table)
29 $this->target = $target;
30 $this->table = $table;
37 function limit($offset, $count, $rss = false)
39 return $this->target->limit($offset, $count);
42 function set_sort_mode($mode)
44 if ('chron' === $mode)
45 return $this->target->orderBy('created desc');
49 class SphinxSearch extends SearchEngine
54 function __construct($target, $table)
56 $fp = @fsockopen(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
58 $this->connected = false;
62 parent::__construct($target, $table);
63 $this->sphinx = new SphinxClient;
64 $this->sphinx->setServer(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
65 $this->connected = true;
68 function is_connected()
70 return $this->connected;
73 function limit($offset, $count, $rss = false)
75 //FIXME without LARGEST_POSSIBLE, the most recent results aren't returned
76 // this probably has a large impact on performance
77 $LARGEST_POSSIBLE = 1e6;
80 $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
83 // return at most 50 pages of results
84 $this->sphinx->setLimits($offset, $count, 50 * ($count - 1), $LARGEST_POSSIBLE);
87 return $this->target->limit(0, $count);
92 $result = $this->sphinx->query($q, $this->table);
93 if (!isset($result['matches'])) return false;
94 $id_set = join(', ', array_keys($result['matches']));
95 $this->target->whereAdd("id in ($id_set)");
99 function set_sort_mode($mode)
101 if ('chron' === $mode) {
102 $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
103 return $this->target->orderBy('created desc');
108 class MySQLSearch extends SearchEngine
112 if ('identica_people' === $this->table)
113 return $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
114 'against (\''.addslashes($q).'\')');
115 if ('identica_notices' === $this->table)
116 return $this->target->whereAdd('MATCH(content) ' .
117 'against (\''.addslashes($q).'\')');
121 class PGSearch extends SearchEngine
125 if ('identica_people' === $this->table)
126 return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
127 if ('identica_notices' === $this->table)
128 return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');