]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/rcfeed/RedisPubSubFeedEngine.php
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.git] / includes / rcfeed / RedisPubSubFeedEngine.php
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 notifications via Redis Pub/Sub
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  * @par Example:
31  * @code
32  * $wgRCFeeds['redis'] = array(
33  *      'formatter' => 'JSONRCFeedFormatter',
34  *      'uri'       => "redis://127.0.0.1:6379/rc.$wgDBname",
35  * );
36  * @endcode
37  *
38  * @since 1.22
39  */
40 class RedisPubSubFeedEngine extends RCFeedEngine {
41
42         /**
43          * @see FormattedRCFeed::send
44          * @param array $feed
45          * @param string $line
46          * @return bool
47          */
48         public function send( array $feed, $line ) {
49                 $parsed = wfParseUrl( $feed['uri'] );
50                 $server = $parsed['host'];
51                 $options = [ 'serializer' => 'none' ];
52                 $channel = 'rc';
53
54                 if ( isset( $parsed['port'] ) ) {
55                         $server .= ":{$parsed['port']}";
56                 }
57                 if ( isset( $parsed['query'] ) ) {
58                         parse_str( $parsed['query'], $options );
59                 }
60                 if ( isset( $parsed['pass'] ) ) {
61                         $options['password'] = $parsed['pass'];
62                 }
63                 if ( isset( $parsed['path'] ) ) {
64                         $channel = str_replace( '/', '.', ltrim( $parsed['path'], '/' ) );
65                 }
66                 $pool = RedisConnectionPool::singleton( $options );
67                 $conn = $pool->getConnection( $server );
68                 if ( $conn !== false ) {
69                         $conn->publish( $channel, $line );
70                         return true;
71                 } else {
72                         return false;
73                 }
74         }
75 }