Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
RedisPubSubFeedEngine
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 send
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
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
21namespace MediaWiki\RCFeed;
22
23use 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 */
50class RedisPubSubFeedEngine extends FormattedRCFeed {
51
52    /**
53     * @see FormattedRCFeed::send
54     * @param array $feed
55     * @param string $line
56     * @return bool
57     */
58    public function send( array $feed, $line ) {
59        $parsed = wfParseUrl( $feed['uri'] );
60        $server = $parsed['host'];
61        $options = [ 'serializer' => 'none' ];
62        $channel = 'rc';
63
64        if ( isset( $parsed['port'] ) ) {
65            $server .= ":{$parsed['port']}";
66        }
67        if ( isset( $parsed['query'] ) ) {
68            parse_str( $parsed['query'], $options );
69        }
70        if ( isset( $parsed['pass'] ) ) {
71            $options['password'] = $parsed['pass'];
72        }
73        if ( isset( $parsed['path'] ) ) {
74            $channel = str_replace( '/', '.', ltrim( $parsed['path'], '/' ) );
75        }
76        $pool = RedisConnectionPool::singleton( $options );
77        $conn = $pool->getConnection( $server );
78        if ( $conn !== false ) {
79            $conn->publish( $channel, $line );
80            return true;
81        }
82
83        return false;
84    }
85}
86/** @deprecated class alias since 1.43 */
87class_alias( RedisPubSubFeedEngine::class, 'RedisPubSubFeedEngine' );