FormattedRCFeed.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. * Base class for RC feed engines that send messages in a freely configurable
  22. * format to a uri-addressed engine set in $wgRCEngines.
  23. * @since 1.29
  24. */
  25. abstract class FormattedRCFeed extends RCFeed {
  26. private $params;
  27. /**
  28. * @param array $params
  29. * - 'uri'
  30. * - 'formatter'
  31. * @see $wgRCFeeds
  32. */
  33. public function __construct( array $params = [] ) {
  34. $this->params = $params;
  35. }
  36. /**
  37. * Send some text to the specified feed.
  38. *
  39. * @param array $feed The feed, as configured in an associative array
  40. * @param string $line The text to send
  41. * @return bool Success
  42. */
  43. abstract public function send( array $feed, $line );
  44. /**
  45. * @param RecentChange $rc
  46. * @param string|null $actionComment
  47. * @return bool Success
  48. */
  49. public function notify( RecentChange $rc, $actionComment = null ) {
  50. $params = $this->params;
  51. /** @var RCFeedFormatter $formatter */
  52. // @phan-suppress-next-line PhanTypeExpectedObjectOrClassName
  53. $formatter = is_object( $params['formatter'] ) ? $params['formatter'] : new $params['formatter'];
  54. $line = $formatter->getLine( $params, $rc, $actionComment );
  55. if ( !$line ) {
  56. // @codeCoverageIgnoreStart
  57. // T109544 - If a feed formatter returns null, this will otherwise cause an
  58. // error in at least RedisPubSubFeedEngine. Not sure best to handle this.
  59. return;
  60. // @codeCoverageIgnoreEnd
  61. }
  62. return $this->send( $params, $line );
  63. }
  64. }