]> 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 SphinxSearch extends SearchEngine
50 {
51     private $sphinx;
52     private $connected;
53
54     function __construct($target, $table)
55     {
56         $fp = @fsockopen(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
57         if (!$fp) {
58             $this->connected = false;
59             return;
60         }
61         fclose($fp);
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;
66     }
67
68     function is_connected()
69     {
70         return $this->connected;
71     }
72
73     function limit($offset, $count, $rss = false)
74     {
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;
78
79         if ($rss) {
80             $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
81         }
82         else {
83             // return at most 50 pages of results
84             $this->sphinx->setLimits($offset, $count, 50 * ($count - 1), $LARGEST_POSSIBLE);
85         }
86
87         return $this->target->limit(0, $count);
88     }
89
90     function query($q)
91     {
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)");
96         return true;
97      }
98
99     function set_sort_mode($mode)
100     {
101         if ('chron' === $mode) {
102             $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
103             return $this->target->orderBy('created desc');
104         }
105     }
106 }
107
108 class MySQLSearch extends SearchEngine
109 {
110     function query($q)
111     {
112         if ('identica_people' === $this->table) {
113             $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
114                                     'AGAINST (\''.addslashes($q).'\' IN BOOLEAN MODE)');
115             if (strtolower($q) != $q) {
116                 $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
117                                         'AGAINST (\''.addslashes(strtolower($q)).'\' IN BOOLEAN MODE)', 'OR');
118             }
119             return true;
120         } else if ('identica_notices' === $this->table) {
121
122             // Do not show imported notices
123             $this->target->whereAdd('notice.is_local != ' . Notice::GATEWAY);
124
125             if (strtolower($q) != $q) {
126                 $this->target->whereAdd("( MATCH(content) AGAINST ('" . addslashes($q) .
127                     "' IN BOOLEAN MODE)) OR ( MATCH(content) " .
128                     "AGAINST ('"  . addslashes(strtolower($q)) .
129                     "' IN BOOLEAN MODE))");
130             } else {
131                 $this->target->whereAdd('MATCH(content) ' .
132                                          'AGAINST (\''.addslashes($q).'\' IN BOOLEAN MODE)');
133             }
134
135             return true;
136         } else {
137             throw new ServerException('Unknown table: ' . $this->table);
138         }
139     }
140 }
141
142 class MySQLLikeSearch extends SearchEngine
143 {
144     function query($q)
145     {
146         if ('identica_people' === $this->table) {
147             $qry = sprintf('(nickname LIKE "%%%1$s%%" OR '.
148                            ' fullname LIKE "%%%1$s%%" OR '.
149                            ' location LIKE "%%%1$s%%" OR '.
150                            ' bio      LIKE "%%%1$s%%" OR '.
151                            ' homepage LIKE "%%%1$s%%")', addslashes($q));
152         } else if ('identica_notices' === $this->table) {
153             $qry = sprintf('content LIKE "%%%1$s%%"', addslashes($q));
154         } else {
155             throw new ServerException('Unknown table: ' . $this->table);
156         }
157
158         $this->target->whereAdd($qry);
159
160         return true;
161     }
162 }
163
164 class PGSearch extends SearchEngine
165 {
166     function query($q)
167     {
168         if ('identica_people' === $this->table) {
169             return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
170         } else if ('identica_notices' === $this->table) {
171
172             // XXX: We need to filter out gateway notices (notice.is_local = -2) --Zach
173
174             return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');
175         } else {
176             throw new ServerException('Unknown table: ' . $this->table);
177         }
178     }
179 }
180