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') && !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)
46 return $this->target->orderBy('created DESC');
49 return $this->target->orderBy('created ASC');
52 if ($this->table != 'profile') {
54 'nickname_desc sort mode can only be use when searching profile.'
57 return $this->target->orderBy('nickname DESC');
61 if ($this->table != 'profile') {
63 'nickname_desc sort mode can only be use when searching profile.'
66 return $this->target->orderBy('nickname ASC');
70 return $this->target->orderBy('created DESC');
76 class MySQLSearch extends SearchEngine
80 if ('profile' === $this->table) {
81 $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
82 'AGAINST (\''.$this->target->escape($q).'\' IN BOOLEAN MODE)');
83 if (strtolower($q) != $q) {
84 $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
85 'AGAINST (\''.$this->target->escape(strtolower($q)).'\' IN BOOLEAN MODE)', 'OR');
88 } else if ('notice' === $this->table) {
90 // Don't show imported notices
91 $this->target->whereAdd('notice.is_local != ' . Notice::GATEWAY);
93 if (strtolower($q) != $q) {
94 $this->target->whereAdd("( MATCH(content) AGAINST ('" . $this->target->escape($q) .
95 "' IN BOOLEAN MODE)) OR ( MATCH(content) " .
96 "AGAINST ('" . $this->target->escape(strtolower($q)) .
97 "' IN BOOLEAN MODE))");
99 $this->target->whereAdd('MATCH(content) ' .
100 'AGAINST (\''.$this->target->escape($q).'\' IN BOOLEAN MODE)');
105 throw new ServerException('Unknown table: ' . $this->table);
110 class MySQLLikeSearch extends SearchEngine
114 if ('profile' === $this->table) {
115 $qry = sprintf('(nickname LIKE "%%%1$s%%" OR '.
116 ' fullname LIKE "%%%1$s%%" OR '.
117 ' location LIKE "%%%1$s%%" OR '.
118 ' bio LIKE "%%%1$s%%" OR '.
119 ' homepage LIKE "%%%1$s%%")', $this->target->escape($q, true));
120 } else if ('notice' === $this->table) {
121 $qry = sprintf('content LIKE "%%%1$s%%"', $this->target->escape($q, true));
123 throw new ServerException('Unknown table: ' . $this->table);
126 $this->target->whereAdd($qry);
132 class PGSearch extends SearchEngine
136 if ('profile' === $this->table) {
137 return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.$this->target->escape($q).'\')');
138 } else if ('notice' === $this->table) {
140 // XXX: We need to filter out gateway notices (notice.is_local = -2) --Zach
142 return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.$this->target->escape($q).'\')');
144 throw new ServerException('Unknown table: ' . $this->table);