]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/search_engines.php
Added type-hint for StartShowEntryForms hook
[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')) {
21     exit(1);
22 }
23
24 class SearchEngine
25 {
26     protected $target;
27     protected $table;
28
29     function __construct($target, $table)
30     {
31         $this->target = $target;
32         $this->table = $table;
33     }
34
35     function query($q)
36     {
37     }
38
39     function limit($offset, $count, $rss = false)
40     {
41         return $this->target->limit($offset, $count);
42     }
43
44     function set_sort_mode($mode)
45     {
46         switch ($mode) {
47             case 'chron':
48                 return $this->target->orderBy('created DESC');
49                 break;
50             case 'reverse_chron':
51                 return $this->target->orderBy('created ASC');
52                 break;
53             case 'nickname_desc':
54                 if ($this->table != 'profile') {
55                     throw new Exception(
56                         'nickname_desc sort mode can only be use when searching profile.'
57                     );
58                 } else {
59                     return $this->target->orderBy(sprintf('%1$s.nickname DESC', $this->table));
60                 }
61                 break;
62             case 'nickname_asc':
63                 if ($this->table != 'profile') {
64                     throw new Exception(
65                         'nickname_desc sort mode can only be use when searching profile.'
66                     );
67                 } else {
68                     return $this->target->orderBy(sprintf('%1$s.nickname ASC', $this->table));
69                 }
70                 break;
71             default:
72                 return $this->target->orderBy('created DESC');
73                 break;
74         }
75     }
76 }
77
78 class MySQLSearch extends SearchEngine
79 {
80     function query($q)
81     {
82         if ('profile' === $this->table) {
83             $this->target->whereAdd(
84                 sprintf('MATCH (%2$s.nickname, %2$s.fullname, %2$s.location, %2$s.bio, %2$s.homepage) ' .
85                     'AGAINST ("%1$s" IN BOOLEAN MODE)',
86                     $this->target->escape($q, true),
87                     $this->table)
88             );
89             if (strtolower($q) != $q) {
90                 $this->target->whereAdd(
91                     sprintf('MATCH (%2$s.nickname, %2$s.fullname, %2$s.location, %2$s.bio, %2$s.homepage) ' .
92                         'AGAINST ("%1$s" IN BOOLEAN MODE)',
93                         $this->target->escape(strtolower($q), true),
94                         $this->table),
95                     'OR'
96                 );
97             }
98         } else if ('notice' === $this->table) {
99
100             // Don't show imported notices
101             $this->target->whereAdd('notice.is_local != ' . Notice::GATEWAY);
102
103             $this->target->whereAdd(
104                 sprintf('MATCH (%2$s.content) AGAINST ("%1$s" IN BOOLEAN MODE)',
105                     $this->target->escape($q, true),
106                     $this->table)
107             );
108             if (strtolower($q) != $q) {
109                 $this->target->whereAdd(
110                     sprintf('MATCH (%2$s.content) AGAINST ("%1$s" IN BOOLEAN MODE)',
111                         $this->target->escape(strtolower($q), true),
112                         $this->table),
113                     'OR'
114                 );
115             }
116
117         } else {
118             throw new ServerException('Unknown table: ' . $this->table);
119         }
120
121         return true;
122     }
123 }
124
125 class MySQLLikeSearch extends SearchEngine
126 {
127     function query($q)
128     {
129         if ('profile' === $this->table) {
130             $qry = sprintf('(%2$s.nickname LIKE "%%%1$s%%" OR ' .
131                            ' %2$s.fullname LIKE "%%%1$s%%" OR ' .
132                            ' %2$s.location LIKE "%%%1$s%%" OR ' .
133                            ' %2$s.bio      LIKE "%%%1$s%%" OR ' .
134                            ' %2$s.homepage LIKE "%%%1$s%%")',
135                            $this->target->escape($q, true),
136                            $this->table);
137         } else if ('notice' === $this->table) {
138             $qry = sprintf('content LIKE "%%%1$s%%"', $this->target->escape($q, true));
139         } else {
140             throw new ServerException('Unknown table: ' . $this->table);
141         }
142
143         $this->target->whereAdd($qry);
144
145         return true;
146     }
147 }
148
149 class PGSearch extends SearchEngine
150 {
151     function query($q)
152     {
153         if ('profile' === $this->table) {
154             return $this->target->whereAdd('textsearch @@ plainto_tsquery(\'' . $this->target->escape($q) . '\')');
155         } else if ('notice' === $this->table) {
156
157             // XXX: We need to filter out gateway notices (notice.is_local = -2) --Zach
158
159             return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\'' . $this->target->escape($q) . '\')');
160         } else {
161             throw new ServerException('Unknown table: ' . $this->table);
162         }
163     }
164 }
165