| 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 | namespace MediaWiki\RCFeed; |
| 22 | |
| 23 | use Wikimedia\ObjectCache\RedisConnectionPool; |
| 24 | |
| 25 | /** |
| 26 | * Send recent change to a Redis Pub/Sub channel. |
| 27 | * |
| 28 | * If the feed URI contains a path component, it will be used to generate a |
| 29 | * channel name by stripping the leading slash and replacing any remaining |
| 30 | * slashes with '.'. If no path component is present, the channel is set to |
| 31 | * 'rc'. If the URI contains a query string, its parameters will be parsed |
| 32 | * as RedisConnectionPool options. |
| 33 | * |
| 34 | * Parameters: |
| 35 | * - `formatter`: (Required) Which RCFeedFormatter class to use. |
| 36 | * - `uri`: (Required) Where to send the messages. |
| 37 | * |
| 38 | * @par Example: |
| 39 | * @code |
| 40 | * $wgRCFeeds['rc-to-redis'] = [ |
| 41 | * 'class' => 'RedisPubSubFeedEngine', |
| 42 | * 'formatter' => 'JSONRCFeedFormatter', |
| 43 | * 'uri' => "redis://127.0.0.1:6379/rc.$wgDBname", |
| 44 | * ]; |
| 45 | * @endcode |
| 46 | * |
| 47 | * @see $wgRCFeeds |
| 48 | * @since 1.22 |
| 49 | * @ingroup RecentChanges |
| 50 | */ |
| 51 | class RedisPubSubFeedEngine extends FormattedRCFeed { |
| 52 | |
| 53 | /** |
| 54 | * @see FormattedRCFeed::send |
| 55 | * @param array $feed |
| 56 | * @param string $line |
| 57 | * @return bool |
| 58 | */ |
| 59 | public function send( array $feed, $line ) { |
| 60 | $parsed = wfGetUrlUtils()->parse( $feed['uri'] ); |
| 61 | // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Valid URL |
| 62 | $server = $parsed['host']; |
| 63 | $options = [ 'serializer' => 'none' ]; |
| 64 | $channel = 'rc'; |
| 65 | |
| 66 | if ( isset( $parsed['port'] ) ) { |
| 67 | $server .= ":{$parsed['port']}"; |
| 68 | } |
| 69 | if ( isset( $parsed['query'] ) ) { |
| 70 | parse_str( $parsed['query'], $options ); |
| 71 | } |
| 72 | if ( isset( $parsed['pass'] ) ) { |
| 73 | $options['password'] = $parsed['pass']; |
| 74 | } |
| 75 | if ( isset( $parsed['path'] ) ) { |
| 76 | $channel = str_replace( '/', '.', ltrim( $parsed['path'], '/' ) ); |
| 77 | } |
| 78 | $pool = RedisConnectionPool::singleton( $options ); |
| 79 | $conn = $pool->getConnection( $server ); |
| 80 | if ( $conn !== false ) { |
| 81 | $conn->publish( $channel, $line ); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | /** @deprecated class alias since 1.43 */ |
| 89 | class_alias( RedisPubSubFeedEngine::class, 'RedisPubSubFeedEngine' ); |