]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/search_engines.php
change function headers to K&R style
[quix0rs-gnu-social.git] / 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     protected $target;
24     protected $table;
25
26     function __construct($target, $table)
27     {
28         $this->target = $target;
29         $this->table = $table;
30     }
31
32     function query($q)
33     {
34     }
35
36     function limit($offset, $count, $rss = false)
37     {
38         return $this->target->limit($offset, $count);
39     }
40
41     function set_sort_mode($mode)
42     {
43         if ('chron' === $mode)
44             return $this->target->orderBy('created desc');
45     }
46 }
47
48 class SphinxSearch extends SearchEngine {
49     private $sphinx;
50     private $connected;
51
52     function __construct($target, $table)
53     {
54         $fp = @fsockopen(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
55         if (!$fp) {
56             $this->connected = false;
57             return;
58         }
59         fclose($fp);
60         parent::__construct($target, $table);
61         $this->sphinx = new SphinxClient;
62         $this->sphinx->setServer(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
63         $this->connected = true;
64     }
65
66     function is_connected()
67     {
68         return $this->connected;
69     }
70
71     function limit($offset, $count, $rss = false)
72     {
73         //FIXME without LARGEST_POSSIBLE, the most recent results aren't returned
74         //      this probably has a large impact on performance
75         $LARGEST_POSSIBLE = 1e6; 
76
77         if ($rss) {
78             $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
79         }
80         else {
81             // return at most 50 pages of results
82             $this->sphinx->setLimits($offset, $count, 50 * ($count - 1), $LARGEST_POSSIBLE);
83         }
84
85         return $this->target->limit(0, $count);
86     }
87
88     function query($q)
89     {
90         $result = $this->sphinx->query($q, $this->table);
91         if (!isset($result['matches'])) return false;
92         $id_set = join(', ', array_keys($result['matches']));
93         $this->target->whereAdd("id in ($id_set)");
94         return true;
95      }
96
97     function set_sort_mode($mode)
98     {
99         if ('chron' === $mode) {
100             $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
101             return $this->target->orderBy('created desc');
102         }
103     }
104 }
105
106 class MySQLSearch extends SearchEngine {
107     function query($q)
108     {
109         if ('identica_people' === $this->table)
110             return $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
111                            'against (\''.addslashes($q).'\')');
112         if ('identica_notices' === $this->table)
113             return $this->target->whereAdd('MATCH(content) ' .
114                            'against (\''.addslashes($q).'\')');
115     }
116 }
117
118 class PGSearch extends SearchEngine {
119     function query($q)
120     {
121         if ('identica_people' === $this->table)
122             return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
123         if ('identica_notices' === $this->table)
124             return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');
125     }
126 }
127