]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/search_engines.php
Merge remote-tracking branch 'upstream/master' into social-master
[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         switch ($mode) {
45         case 'chron':
46             return $this->target->orderBy('created DESC');
47             break;
48         case 'reverse_chron':
49             return $this->target->orderBy('created ASC');
50             break;
51         case 'nickname_desc':
52             if ($this->table != 'profile') {
53                 throw new Exception(
54                     'nickname_desc sort mode can only be use when searching profile.'
55                 );
56             } else {
57                 return $this->target->orderBy(sprintf('%1$s.nickname DESC', $this->table));
58             }
59             break;
60         case 'nickname_asc':
61             if ($this->table != 'profile') {
62                 throw new Exception(
63                     'nickname_desc sort mode can only be use when searching profile.'
64                 );
65             } else {
66                 return $this->target->orderBy(sprintf('%1$s.nickname ASC', $this->table));
67             }
68             break;
69         default:
70             return $this->target->orderBy('created DESC');
71             break;
72         }
73     }
74 }
75
76 class MySQLSearch extends SearchEngine
77 {
78     function query($q)
79     {
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');
86             }
87         } else if ('notice' === $this->table) {
88
89             // Don't show imported notices
90             $this->target->whereAdd('notice.is_local != ' . Notice::GATEWAY);
91
92             if (strtolower($q) != $q) {
93                 $this->target->whereAdd("( MATCH(content) AGAINST ('" . $this->target->escape($q) .
94                     "' IN BOOLEAN MODE)) OR ( MATCH(content) " .
95                     "AGAINST ('"  . $this->target->escape(strtolower($q)) .
96                     "' IN BOOLEAN MODE))");
97             } else {
98                 $this->target->whereAdd('MATCH(content) ' .
99                                          'AGAINST (\''.$this->target->escape($q).'\' IN BOOLEAN MODE)');
100             }
101
102         } else {
103             throw new ServerException('Unknown table: ' . $this->table);
104         }
105
106         return true;
107     }
108 }
109
110 class MySQLLikeSearch extends SearchEngine
111 {
112     function query($q)
113     {
114         if ('profile' === $this->table) {
115             $qry = sprintf('(%2$s.nickname LIKE "%%%1$s%%" OR '.
116                             ' %2$s.fullname LIKE "%%%1$s%%" OR '.
117                             ' %2$s.location LIKE "%%%1$s%%" OR '.
118                             ' %2$s.bio      LIKE "%%%1$s%%" OR '.
119                             ' %2$s.homepage LIKE "%%%1$s%%")',
120                            $this->target->escape($q, true),
121                            $this->table);
122         } else if ('notice' === $this->table) {
123             $qry = sprintf('content LIKE "%%%1$s%%"', $this->target->escape($q, true));
124         } else {
125             throw new ServerException('Unknown table: ' . $this->table);
126         }
127
128         $this->target->whereAdd($qry);
129
130         return true;
131     }
132 }
133
134 class PGSearch extends SearchEngine
135 {
136     function query($q)
137     {
138         if ('profile' === $this->table) {
139             return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.$this->target->escape($q).'\')');
140         } else if ('notice' === $this->table) {
141
142             // XXX: We need to filter out gateway notices (notice.is_local = -2) --Zach
143
144             return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.$this->target->escape($q).'\')');
145         } else {
146             throw new ServerException('Unknown table: ' . $this->table);
147         }
148     }
149 }
150