]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SphinxSearch/SphinxSearchPlugin.php
Merge branch '0.9-release'
[quix0rs-gnu-social.git] / plugins / SphinxSearch / SphinxSearchPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * PHP version 5
6  *
7  * LICENCE: This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @category  Plugin
21  * @package   StatusNet
22  * @author    Brion Vibber <brion@status.net>
23  * @copyright 2009 Control Yourself, Inc.
24  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
25  * @link      http://laconi.ca/
26  */
27
28 if (!defined('STATUSNET')) {
29     exit(1);
30 }
31
32 // Set defaults if not already set in the config array...
33 global $config;
34 $sphinxDefaults =
35     array('enabled' => true,
36           'server' => 'localhost',
37           'port' => 3312);
38 foreach($sphinxDefaults as $key => $val) {
39     if (!isset($config['sphinx'][$key])) {
40         $config['sphinx'][$key] = $val;
41     }
42 }
43
44
45
46 /**
47  * Plugin for Sphinx search backend.
48  *
49  * @category Plugin
50  * @package  StatusNet
51  * @author   Brion Vibber <brion@status.net>
52  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
53  * @link     http://laconi.ca/
54  * @link     http://twitter.com/
55  */
56
57 class SphinxSearchPlugin extends Plugin
58 {
59     /**
60      * Automatically load any classes used
61      *
62      * @param string $cls the class
63      * @return boolean hook return
64      */
65     function onAutoload($cls)
66     {
67         switch ($cls) {
68         case 'SphinxSearch':
69             include_once INSTALLDIR . '/plugins/SphinxSearch/' .
70               strtolower($cls) . '.php';
71             return false;
72         default:
73             return true;
74         }
75     }
76
77     /**
78      * Create sphinx search engine object for the given table type.
79      *
80      * @param Memcached_DataObject $target
81      * @param string $table
82      * @param out &$search_engine SearchEngine object on output if successful
83      * @ return boolean hook return
84      */
85     function onGetSearchEngine(Memcached_DataObject $target, $table, &$search_engine)
86     {
87         if (common_config('sphinx', 'enabled')) {
88             if (!class_exists('SphinxClient')) {
89                 throw new ServerException('Sphinx PHP extension must be installed.');
90             }
91             $engine = new SphinxSearch($target, $table);
92             if ($engine->is_connected()) {
93                 $search_engine = $engine;
94                 return false;
95             }
96         }
97         // Sphinx disabled or disconnected
98         return true;
99     }
100 }