]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/search_engines.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / lib / search_engines.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 class SearchEngine
23 {
24     protected $target;
25     protected $table;
26
27     function __construct($target, $table)
28     {
29         $this->target = $target;
30         $this->table = $table;
31     }
32
33     function query($q)
34     {
35     }
36
37     function limit($offset, $count, $rss = false)
38     {
39         return $this->target->limit($offset, $count);
40     }
41
42     function set_sort_mode($mode)
43     {
44         if ('chron' === $mode)
45             return $this->target->orderBy('created desc');
46     }
47 }
48
49 class MySQLSearch extends SearchEngine
50 {
51     function query($q)
52     {
53         if ('profile' === $this->table) {
54             $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
55                                     'AGAINST (\''.addslashes($q).'\' IN BOOLEAN MODE)');
56             if (strtolower($q) != $q) {
57                 $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
58                                         'AGAINST (\''.addslashes(strtolower($q)).'\' IN BOOLEAN MODE)', 'OR');
59             }
60             return true;
61         } else if ('notice' === $this->table) {
62
63             // Don't show imported notices
64             $this->target->whereAdd('notice.is_local != ' . Notice::GATEWAY);
65
66             if (strtolower($q) != $q) {
67                 $this->target->whereAdd("( MATCH(content) AGAINST ('" . addslashes($q) .
68                     "' IN BOOLEAN MODE)) OR ( MATCH(content) " .
69                     "AGAINST ('"  . addslashes(strtolower($q)) .
70                     "' IN BOOLEAN MODE))");
71             } else {
72                 $this->target->whereAdd('MATCH(content) ' .
73                                          'AGAINST (\''.addslashes($q).'\' IN BOOLEAN MODE)');
74             }
75
76             return true;
77         } else {
78             throw new ServerException('Unknown table: ' . $this->table);
79         }
80     }
81 }
82
83 class MySQLLikeSearch extends SearchEngine
84 {
85     function query($q)
86     {
87         if ('profile' === $this->table) {
88             $qry = sprintf('(nickname LIKE "%%%1$s%%" OR '.
89                            ' fullname LIKE "%%%1$s%%" OR '.
90                            ' location LIKE "%%%1$s%%" OR '.
91                            ' bio      LIKE "%%%1$s%%" OR '.
92                            ' homepage LIKE "%%%1$s%%")', addslashes($q));
93         } else if ('notice' === $this->table) {
94             $qry = sprintf('content LIKE "%%%1$s%%"', addslashes($q));
95         } else {
96             throw new ServerException('Unknown table: ' . $this->table);
97         }
98
99         $this->target->whereAdd($qry);
100
101         return true;
102     }
103 }
104
105 class PGSearch extends SearchEngine
106 {
107     function query($q)
108     {
109         if ('profile' === $this->table) {
110             return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
111         } else if ('notice' === $this->table) {
112
113             // XXX: We need to filter out gateway notices (notice.is_local = -2) --Zach
114
115             return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');
116         } else {
117             throw new ServerException('Unknown table: ' . $this->table);
118         }
119     }
120 }
121