SphinxSearchPlugin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. if (!defined('STATUSNET')) {
  28. exit(1);
  29. }
  30. // Set defaults if not already set in the config array...
  31. global $config;
  32. $sphinxDefaults =
  33. array('enabled' => true,
  34. 'server' => 'localhost',
  35. 'port' => 3312);
  36. foreach($sphinxDefaults as $key => $val) {
  37. if (!isset($config['sphinx'][$key])) {
  38. $config['sphinx'][$key] = $val;
  39. }
  40. }
  41. /**
  42. * Plugin for Sphinx search backend.
  43. *
  44. * @category Plugin
  45. * @package StatusNet
  46. * @author Brion Vibber <brion@status.net>
  47. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  48. * @link http://laconi.ca/
  49. * @link http://twitter.com/
  50. */
  51. class SphinxSearchPlugin extends Plugin
  52. {
  53. const PLUGIN_VERSION = '2.0.0';
  54. /**
  55. * Automatically load any classes used
  56. *
  57. * @param string $cls the class
  58. * @return boolean hook return
  59. */
  60. function onAutoload($cls)
  61. {
  62. switch ($cls) {
  63. case 'SphinxSearch':
  64. include_once INSTALLDIR . '/plugins/SphinxSearch/' .
  65. strtolower($cls) . '.php';
  66. return false;
  67. }
  68. return parent::onAutoload($cls);
  69. }
  70. /**
  71. * Create sphinx search engine object for the given table type.
  72. *
  73. * @param Memcached_DataObject $target
  74. * @param string $table
  75. * @param out &$search_engine SearchEngine object on output if successful
  76. * @ return boolean hook return
  77. */
  78. function onGetSearchEngine(Memcached_DataObject $target, $table, &$search_engine)
  79. {
  80. if (common_config('sphinx', 'enabled')) {
  81. if (!class_exists('SphinxClient')) {
  82. // TRANS: Server exception.
  83. throw new ServerException(_m('Sphinx PHP extension must be installed.'));
  84. }
  85. $engine = new SphinxSearch($target, $table);
  86. if ($engine->is_connected()) {
  87. $search_engine = $engine;
  88. return false;
  89. }
  90. }
  91. // Sphinx disabled or disconnected
  92. return true;
  93. }
  94. /**
  95. * Provide plugin version information.
  96. *
  97. * This data is used when showing the version page.
  98. *
  99. * @param array &$versions array of version data arrays; see EVENTS.txt
  100. *
  101. * @return boolean hook value
  102. */
  103. function onPluginVersion(array &$versions)
  104. {
  105. $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/SphinxSearch';
  106. $versions[] = array('name' => 'SphinxSearch',
  107. 'version' => self::PLUGIN_VERSION,
  108. 'author' => 'Brion Vibber',
  109. 'homepage' => $url,
  110. 'rawdescription' =>
  111. // TRANS: Plugin description.
  112. _m('Plugin for Sphinx search backend.'));
  113. return true;
  114. }
  115. }