RedisPubSubFeedEngine.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * Send recent change notifications via Redis Pub/Sub
  22. *
  23. * If the feed URI contains a path component, it will be used to generate a
  24. * channel name by stripping the leading slash and replacing any remaining
  25. * slashes with '.'. If no path component is present, the channel is set to
  26. * 'rc'. If the URI contains a query string, its parameters will be parsed
  27. * as RedisConnectionPool options.
  28. *
  29. * @par Example:
  30. * @code
  31. * $wgRCFeeds['redis'] = [
  32. * 'formatter' => 'JSONRCFeedFormatter',
  33. * 'uri' => "redis://127.0.0.1:6379/rc.$wgDBname",
  34. * ];
  35. * @endcode
  36. *
  37. * @since 1.22
  38. */
  39. class RedisPubSubFeedEngine extends FormattedRCFeed {
  40. /**
  41. * @see FormattedRCFeed::send
  42. * @param array $feed
  43. * @param string $line
  44. * @return bool
  45. */
  46. public function send( array $feed, $line ) {
  47. $parsed = wfParseUrl( $feed['uri'] );
  48. $server = $parsed['host'];
  49. $options = [ 'serializer' => 'none' ];
  50. $channel = 'rc';
  51. if ( isset( $parsed['port'] ) ) {
  52. $server .= ":{$parsed['port']}";
  53. }
  54. if ( isset( $parsed['query'] ) ) {
  55. parse_str( $parsed['query'], $options );
  56. }
  57. if ( isset( $parsed['pass'] ) ) {
  58. $options['password'] = $parsed['pass'];
  59. }
  60. if ( isset( $parsed['path'] ) ) {
  61. $channel = str_replace( '/', '.', ltrim( $parsed['path'], '/' ) );
  62. }
  63. $pool = RedisConnectionPool::singleton( $options );
  64. $conn = $pool->getConnection( $server );
  65. if ( $conn !== false ) {
  66. $conn->publish( $channel, $line );
  67. return true;
  68. }
  69. return false;
  70. }
  71. }