]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SphinxSearch/sphinxsearch.php
Removed plugin Google-Analytics as this is free/libre and decentralized
[quix0rs-gnu-social.git] / plugins / SphinxSearch / sphinxsearch.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')) {
21     exit(1);
22 }
23
24 class SphinxSearch extends SearchEngine
25 {
26     private $sphinx;
27     private $connected;
28
29     function __construct($target, $table)
30     {
31         $fp = @fsockopen(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
32         if (!$fp) {
33             $this->connected = false;
34             return;
35         }
36         fclose($fp);
37         parent::__construct($target, $table);
38         $this->sphinx = new SphinxClient;
39         $this->sphinx->setServer(common_config('sphinx', 'server'), common_config('sphinx', 'port'));
40         $this->connected = true;
41     }
42
43     function is_connected()
44     {
45         return $this->connected;
46     }
47
48     function limit($offset, $count, $rss = false)
49     {
50         //FIXME without LARGEST_POSSIBLE, the most recent results aren't returned
51         //      this probably has a large impact on performance
52         $LARGEST_POSSIBLE = 1e6;
53
54         if ($rss) {
55             $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
56         }
57         else {
58             // return at most 50 pages of results
59             $this->sphinx->setLimits($offset, $count, 50 * ($count - 1), $LARGEST_POSSIBLE);
60         }
61
62         return $this->target->limit(0, $count);
63     }
64
65     function query($q)
66     {
67         $result = $this->sphinx->query($q, $this->remote_table());
68         if ($result === false) {
69             throw new ServerException($this->sphinx->getLastError());
70         }
71         if (!isset($result['matches'])) return false;
72         $id_set = join(', ', array_keys($result['matches']));
73         $this->target->whereAdd("id in ($id_set)");
74         return true;
75      }
76
77     function set_sort_mode($mode)
78     {
79         switch ($mode) {
80         case 'chron':
81             $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
82             return $this->target->orderBy('id desc');
83             break;
84         case 'reverse_chron':
85             $this->sphinx->SetSortMode(SPH_SORT_ATTR_ASC, 'created_ts');
86             return $this->target->orderBy('id asc');
87             break;
88         case 'nickname_desc':
89             if ($this->table != 'profile') {
90                 throw new Exception(
91                     'nickname_desc sort mode can only be use when searching profile.'
92                 );
93             } else {
94                 $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'nickname');
95                 return $this->target->orderBy('id desc');
96             }
97             break;
98         case 'nickname_asc':
99             if ($this->table != 'profile') {
100                 throw new Exception(
101                     'nickname_desc sort mode can only be use when searching profile.'
102                 );
103             } else {
104                 $this->sphinx->SetSortMode(SPH_SORT_ATTR_ASC, 'nickname');
105                 return $this->target->orderBy('id asc');
106             }
107             break;
108         default:
109             $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
110             return $this->target->orderBy('id desc');
111             break;
112         }
113     }
114
115     function remote_table()
116     {
117         return $this->dbname() . '_' . $this->table;
118     }
119
120     function dbname()
121     {
122         // @fixme there should be a less dreadful way to do this.
123         // DB objects won't give database back until they connect, it's confusing
124         if (preg_match('!^.*?://.*?:.*?@.*?/(.*?)$!', common_config('db', 'database'), $matches)) {
125             return $matches[1];
126         }
127
128         // TRANS: Server exception thrown when a database name cannot be identified.
129         throw new ServerException(_m("Sphinx search could not identify database name."));
130     }
131 }