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