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