SphinxSearchPlugin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /**
  54. * Automatically load any classes used
  55. *
  56. * @param string $cls the class
  57. * @return boolean hook return
  58. */
  59. function onAutoload($cls)
  60. {
  61. switch ($cls) {
  62. case 'SphinxSearch':
  63. include_once INSTALLDIR . '/plugins/SphinxSearch/' .
  64. strtolower($cls) . '.php';
  65. return false;
  66. }
  67. return parent::onAutoload($cls);
  68. }
  69. /**
  70. * Create sphinx search engine object for the given table type.
  71. *
  72. * @param Memcached_DataObject $target
  73. * @param string $table
  74. * @param out &$search_engine SearchEngine object on output if successful
  75. * @ return boolean hook return
  76. */
  77. function onGetSearchEngine(Memcached_DataObject $target, $table, &$search_engine)
  78. {
  79. if (common_config('sphinx', 'enabled')) {
  80. if (!class_exists('SphinxClient')) {
  81. // TRANS: Server exception.
  82. throw new ServerException(_m('Sphinx PHP extension must be installed.'));
  83. }
  84. $engine = new SphinxSearch($target, $table);
  85. if ($engine->is_connected()) {
  86. $search_engine = $engine;
  87. return false;
  88. }
  89. }
  90. // Sphinx disabled or disconnected
  91. return true;
  92. }
  93. /**
  94. * Provide plugin version information.
  95. *
  96. * This data is used when showing the version page.
  97. *
  98. * @param array &$versions array of version data arrays; see EVENTS.txt
  99. *
  100. * @return boolean hook value
  101. */
  102. function onPluginVersion(array &$versions)
  103. {
  104. $url = 'http://status.net/wiki/Plugin:SphinxSearch';
  105. $versions[] = array('name' => 'SphinxSearch',
  106. 'version' => GNUSOCIAL_VERSION,
  107. 'author' => 'Brion Vibber',
  108. 'homepage' => $url,
  109. 'rawdescription' =>
  110. // TRANS: Plugin description.
  111. _m('Plugin for Sphinx search backend.'));
  112. return true;
  113. }
  114. }