]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SphinxSearch/sphinxsearch.php
Merge branch 'master' into 1.0.x
[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 (!isset($result['matches'])) return false;
69         $id_set = join(', ', array_keys($result['matches']));
70         $this->target->whereAdd("id in ($id_set)");
71         return true;
72      }
73
74     function set_sort_mode($mode)
75     {
76         switch ($mode) {
77         case 'chron':
78             $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
79             return $this->target->orderBy('id desc');
80             break;
81         case 'reverse_chron':
82             $this->sphinx->SetSortMode(SPH_SORT_ATTR_ASC, 'created_ts');
83             return $this->target->orderBy('id asc');
84             break;
85         case 'nickname_desc':
86             if ($this->table != 'profile') {
87                 throw new Exception(
88                     'nickname_desc sort mode can only be use when searching profile.'
89                 );
90             } else {
91                 $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'nickname');
92                 return $this->target->orderBy('id desc');
93             }
94             break;
95         case 'nickname_asc':
96             if ($this->table != 'profile') {
97                 throw new Exception(
98                     'nickname_desc sort mode can only be use when searching profile.'
99                 );
100             } else {
101                 $this->sphinx->SetSortMode(SPH_SORT_ATTR_ASC, 'nickname');
102                 return $this->target->orderBy('id asc');
103             }
104             break;
105         default:
106             $this->sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'created_ts');
107             return $this->target->orderBy('id desc');
108             break;
109         }
110     }
111
112     function remote_table()
113     {
114         return $this->dbname() . '_' . $this->table;
115     }
116
117     function dbname()
118     {
119         // @fixme there should be a less dreadful way to do this.
120         // DB objects won't give database back until they connect, it's confusing
121         if (preg_match('!^.*?://.*?:.*?@.*?/(.*?)$!', common_config('db', 'database'), $matches)) {
122             return $matches[1];
123         }
124
125         // TRANS: Server exception thrown when a database name cannot be identified.
126         throw new ServerException(_m("Sphinx search could not identify database name."));
127     }
128 }