]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/lib/search_engines.php
move opening brace of class declaration to next line
[quix0rs-gnu-social.git] / _darcs / pristine / lib / search_engines.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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('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             return $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
114                            'against (\''.addslashes($q).'\')');
115         if ('identica_notices' === $this->table)
116             return $this->target->whereAdd('MATCH(content) ' .
117                            'against (\''.addslashes($q).'\')');
118     }
119 }
120
121 class PGSearch extends SearchEngine
122 {
123     function query($q)
124     {
125         if ('identica_people' === $this->table)
126             return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
127         if ('identica_notices' === $this->table)
128             return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');
129     }
130 }
131