diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2014-12-27 15:41:37 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2014-12-31 11:43:28 +0100 |
commit | c1f9b1f7b1b77776192048005dcc66dcf3df2bfb (patch) | |
tree | 2b38796e738dd74cb42ecd9bfd151803108386bc /includes/rcfeed | |
parent | b88ab0086858470dd1f644e64cb4e4f62bb2be9b (diff) |
Update to MediaWiki 1.24.1
Diffstat (limited to 'includes/rcfeed')
-rw-r--r-- | includes/rcfeed/IRCColourfulRCFeedFormatter.php | 49 | ||||
-rw-r--r-- | includes/rcfeed/JSONRCFeedFormatter.php | 112 | ||||
-rw-r--r-- | includes/rcfeed/MachineReadableRCFeedFormatter.php | 130 | ||||
-rw-r--r-- | includes/rcfeed/RCFeedEngine.php | 33 | ||||
-rw-r--r-- | includes/rcfeed/RCFeedFormatter.php | 27 | ||||
-rw-r--r-- | includes/rcfeed/RedisPubSubFeedEngine.php | 62 | ||||
-rw-r--r-- | includes/rcfeed/UDPRCFeedEngine.php | 26 | ||||
-rw-r--r-- | includes/rcfeed/XMLRCFeedFormatter.php | 29 |
8 files changed, 353 insertions, 115 deletions
diff --git a/includes/rcfeed/IRCColourfulRCFeedFormatter.php b/includes/rcfeed/IRCColourfulRCFeedFormatter.php index 507369f3..02a8d7eb 100644 --- a/includes/rcfeed/IRCColourfulRCFeedFormatter.php +++ b/includes/rcfeed/IRCColourfulRCFeedFormatter.php @@ -1,11 +1,36 @@ <?php + +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Generates a colourful notification intended for humans on IRC. + * + * @since 1.22 + */ + class IRCColourfulRCFeedFormatter implements RCFeedFormatter { /** - * Generates a colourful notification intended for humans on IRC. * @see RCFeedFormatter::getLine */ public function getLine( array $feed, RecentChange $rc, $actionComment ) { - global $wgUseRCPatrol, $wgUseNPPatrol, $wgLocalInterwiki, + global $wgUseRCPatrol, $wgUseNPPatrol, $wgLocalInterwikis, $wgCanonicalServer, $wgScript; $attribs = $rc->getAttributes(); if ( $attribs['rc_type'] == RC_LOG ) { @@ -31,7 +56,7 @@ class IRCColourfulRCFeedFormatter implements RCFeedFormatter { $query .= '&rcid=' . $attribs['rc_id']; } // HACK: We need this hook for WMF's secure server setup - wfRunHooks( 'IRCLineURL', array( &$url, &$query ) ); + wfRunHooks( 'IRCLineURL', array( &$url, &$query, $rc ) ); $url .= $query; } @@ -52,19 +77,27 @@ class IRCColourfulRCFeedFormatter implements RCFeedFormatter { if ( $attribs['rc_type'] == RC_LOG ) { $targetText = $rc->getTitle()->getPrefixedText(); - $comment = self::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $actionComment ) ); + $comment = self::cleanupForIRC( str_replace( + "[[$targetText]]", + "[[\00302$targetText\00310]]", + $actionComment + ) ); $flag = $attribs['rc_log_action']; } else { $comment = self::cleanupForIRC( $attribs['rc_comment'] ); $flag = ''; - if ( !$attribs['rc_patrolled'] && ( $wgUseRCPatrol || $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) { + if ( !$attribs['rc_patrolled'] + && ( $wgUseRCPatrol || $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) + ) { $flag .= '!'; } - $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" ) . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" ); + $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" ) + . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" ); } - if ( $feed['add_interwiki_prefix'] === true && $wgLocalInterwiki !== false ) { - $prefix = $wgLocalInterwiki; + if ( $feed['add_interwiki_prefix'] === true && $wgLocalInterwikis ) { + // we use the first entry in $wgLocalInterwikis in recent changes feeds + $prefix = $wgLocalInterwikis[0]; } elseif ( $feed['add_interwiki_prefix'] ) { $prefix = $feed['add_interwiki_prefix']; } else { diff --git a/includes/rcfeed/JSONRCFeedFormatter.php b/includes/rcfeed/JSONRCFeedFormatter.php index f4cb9921..98d3f025 100644 --- a/includes/rcfeed/JSONRCFeedFormatter.php +++ b/includes/rcfeed/JSONRCFeedFormatter.php @@ -1,90 +1,32 @@ <?php -class JSONRCFeedFormatter implements RCFeedFormatter { - /** - * Generates a notification that can be easily interpreted by a machine. - * @see RCFeedFormatter::getLine - */ - public function getLine( array $feed, RecentChange $rc, $actionComment ) { - global $wgCanonicalServer, $wgScriptPath, $wgDBname; - $attrib = $rc->getAttributes(); - - $packet = array( - // Usually, RC ID is exposed only for patrolling purposes, - // but there is no real reason not to expose it in other cases, - // and I can see how this may be potentially useful for clients. - 'id' => $attrib['rc_id'], - 'type' => $attrib['rc_type'], - 'namespace' => $rc->getTitle()->getNamespace(), - 'title' => $rc->getTitle()->getPrefixedText(), - 'comment' => $attrib['rc_comment'], - 'timestamp' => (int)wfTimestamp( TS_UNIX, $attrib['rc_timestamp'] ), - 'user' => $attrib['rc_user_text'], - 'bot' => (bool)$attrib['rc_bot'], - ); - - if ( isset( $feed['channel'] ) ) { - $packet['channel'] = $feed['channel']; - } - - $type = $attrib['rc_type']; - if ( $type == RC_EDIT || $type == RC_NEW ) { - global $wgUseRCPatrol, $wgUseNPPatrol; - - $packet['minor'] = $attrib['rc_minor']; - if ( $wgUseRCPatrol || ( $type == RC_NEW && $wgUseNPPatrol ) ) { - $packet['patrolled'] = $attrib['rc_patrolled']; - } - } - - switch ( $type ) { - case RC_EDIT: - $packet['length'] = array( 'old' => $attrib['rc_old_len'], 'new' => $attrib['rc_new_len'] ); - $packet['revision'] = array( 'old' => $attrib['rc_last_oldid'], 'new' => $attrib['rc_this_oldid'] ); - break; - - case RC_NEW: - $packet['length'] = array( 'old' => NULL, 'new' => $attrib['rc_new_len'] ); - $packet['revision'] = array( 'old' => NULL, 'new' => $attrib['rc_this_oldid'] ); - break; - - case RC_LOG: - $packet['log_type'] = $attrib['rc_log_type']; - $packet['log_action'] = $attrib['rc_log_action']; - if ( $attrib['rc_params'] ) { - wfSuppressWarnings(); - $params = unserialize( $attrib['rc_params'] ); - wfRestoreWarnings(); - if ( - // If it's an actual serialised false... - $attrib['rc_params'] == serialize( false ) || - // Or if we did not get false back when trying to unserialise - $params !== false - ) { - // From ApiQueryLogEvents::addLogParams - $logParams = array(); - // Keys like "4::paramname" can't be used for output so we change them to "paramname" - foreach ( $params as $key => $value ) { - if ( strpos( $key, ':' ) === false ) { - $logParams[$key] = $value; - continue; - } - $logParam = explode( ':', $key, 3 ); - $logParams[$logParam[2]] = $value; - } - $packet['log_params'] = $logParams; - } else { - $packet['log_params'] = explode( "\n", $attrib['rc_params'] ); - } - } - $packet['log_action_comment'] = $actionComment; - break; - } - - $packet['server_url'] = $wgCanonicalServer; - $packet['server_script_path'] = $wgScriptPath ?: '/'; - $packet['wiki'] = $wgDBname; - +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Formats a notification into the JSON format (http://www.json.org) + * + * @since 1.22 + */ +class JSONRCFeedFormatter extends MachineReadableRCFeedFormatter { + + protected function formatArray( array $packet ) { return FormatJson::encode( $packet ); } } diff --git a/includes/rcfeed/MachineReadableRCFeedFormatter.php b/includes/rcfeed/MachineReadableRCFeedFormatter.php new file mode 100644 index 00000000..519606ca --- /dev/null +++ b/includes/rcfeed/MachineReadableRCFeedFormatter.php @@ -0,0 +1,130 @@ +<?php + +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Abstract class so there can be multiple formatters outputting the same data + * + * @since 1.23 + */ +abstract class MachineReadableRCFeedFormatter implements RCFeedFormatter { + + /** + * Take the packet and return the formatted string + * @param array $packet + * @return string + */ + abstract protected function formatArray( array $packet ); + + /** + * Generates a notification that can be easily interpreted by a machine. + * @see RCFeedFormatter::getLine + */ + public function getLine( array $feed, RecentChange $rc, $actionComment ) { + global $wgCanonicalServer, $wgServerName, $wgScriptPath; + + $packet = array( + // Usually, RC ID is exposed only for patrolling purposes, + // but there is no real reason not to expose it in other cases, + // and I can see how this may be potentially useful for clients. + 'id' => $rc->getAttribute( 'rc_id' ), + 'type' => RecentChange::parseFromRCType( $rc->getAttribute( 'rc_type' ) ), + 'namespace' => $rc->getTitle()->getNamespace(), + 'title' => $rc->getTitle()->getPrefixedText(), + 'comment' => $rc->getAttribute( 'rc_comment' ), + 'timestamp' => (int)wfTimestamp( TS_UNIX, $rc->getAttribute( 'rc_timestamp' ) ), + 'user' => $rc->getAttribute( 'rc_user_text' ), + 'bot' => (bool)$rc->getAttribute( 'rc_bot' ), + ); + + if ( isset( $feed['channel'] ) ) { + $packet['channel'] = $feed['channel']; + } + + $type = $rc->getAttribute( 'rc_type' ); + if ( $type == RC_EDIT || $type == RC_NEW ) { + global $wgUseRCPatrol, $wgUseNPPatrol; + + $packet['minor'] = (bool)$rc->getAttribute( 'rc_minor' ); + if ( $wgUseRCPatrol || ( $type == RC_NEW && $wgUseNPPatrol ) ) { + $packet['patrolled'] = (bool)$rc->getAttribute( 'rc_patrolled' ); + } + } + + switch ( $type ) { + case RC_EDIT: + $packet['length'] = array( + 'old' => $rc->getAttribute( 'rc_old_len' ), + 'new' => $rc->getAttribute( 'rc_new_len' ) + ); + $packet['revision'] = array( + 'old' => $rc->getAttribute( 'rc_last_oldid' ), + 'new' => $rc->getAttribute( 'rc_this_oldid' ) + ); + break; + + case RC_NEW: + $packet['length'] = array( 'old' => null, 'new' => $rc->getAttribute( 'rc_new_len' ) ); + $packet['revision'] = array( 'old' => null, 'new' => $rc->getAttribute( 'rc_this_oldid' ) ); + break; + + case RC_LOG: + $packet['log_id'] = $rc->getAttribute( 'rc_logid' ); + $packet['log_type'] = $rc->getAttribute( 'rc_log_type' ); + $packet['log_action'] = $rc->getAttribute( 'rc_log_action' ); + if ( $rc->getAttribute( 'rc_params' ) ) { + wfSuppressWarnings(); + $params = unserialize( $rc->getAttribute( 'rc_params' ) ); + wfRestoreWarnings(); + if ( + // If it's an actual serialised false... + $rc->getAttribute( 'rc_params' ) == serialize( false ) || + // Or if we did not get false back when trying to unserialise + $params !== false + ) { + // From ApiQueryLogEvents::addLogParams + $logParams = array(); + // Keys like "4::paramname" can't be used for output so we change them to "paramname" + foreach ( $params as $key => $value ) { + if ( strpos( $key, ':' ) === false ) { + $logParams[$key] = $value; + continue; + } + $logParam = explode( ':', $key, 3 ); + $logParams[$logParam[2]] = $value; + } + $packet['log_params'] = $logParams; + } else { + $packet['log_params'] = explode( "\n", $rc->getAttribute( 'rc_params' ) ); + } + } + $packet['log_action_comment'] = $actionComment; + break; + } + + $packet['server_url'] = $wgCanonicalServer; + $packet['server_name'] = $wgServerName; + + $packet['server_script_path'] = $wgScriptPath ?: '/'; + $packet['wiki'] = wfWikiID(); + + return $this->formatArray( $packet ); + } +} diff --git a/includes/rcfeed/RCFeedEngine.php b/includes/rcfeed/RCFeedEngine.php index f733bcb7..0b0cd869 100644 --- a/includes/rcfeed/RCFeedEngine.php +++ b/includes/rcfeed/RCFeedEngine.php @@ -1,12 +1,37 @@ <?php + +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Interface for RC feed engines, which send formatted notifications + * + * @since 1.22 + */ interface RCFeedEngine { /** * Sends some text to the specified live feed. * - * @see RecentChange::cleanupForIRC - * @param array $feed The feed, as configured in an associative array. - * @param string $line The text to send. - * @return boolean success + * @see IRCColourfulRCFeedFormatter::cleanupForIRC + * @param array $feed The feed, as configured in an associative array + * @param string $line The text to send + * @return bool Success */ public function send( array $feed, $line ); } diff --git a/includes/rcfeed/RCFeedFormatter.php b/includes/rcfeed/RCFeedFormatter.php index 6c9f8042..2f156598 100644 --- a/includes/rcfeed/RCFeedFormatter.php +++ b/includes/rcfeed/RCFeedFormatter.php @@ -1,7 +1,32 @@ <?php + +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Interface for RC feed formatters + * + * @since 1.22 + */ interface RCFeedFormatter { /** - * Formats the line for the live feed. + * Formats the line to be sent by an engine * * @param array $feed The feed, as configured in an associative array. * @param RecentChange $rc The RecentChange object showing what sort diff --git a/includes/rcfeed/RedisPubSubFeedEngine.php b/includes/rcfeed/RedisPubSubFeedEngine.php index 4bcc1337..b9023b6b 100644 --- a/includes/rcfeed/RedisPubSubFeedEngine.php +++ b/includes/rcfeed/RedisPubSubFeedEngine.php @@ -1,23 +1,48 @@ <?php + +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Emit a recent change notification via Redis Pub/Sub + * + * If the feed URI contains a path component, it will be used to generate a + * channel name by stripping the leading slash and replacing any remaining + * slashes with '.'. If no path component is present, the channel is set to + * 'rc'. If the URI contains a query string, its parameters will be parsed + * as RedisConnectionPool options. + * + * @example + * $wgRCFeeds['redis'] = array( + * 'formatter' => 'JSONRCFeedFormatter', + * 'uri' => "redis://127.0.0.1:6379/rc.$wgDBname", + * ); + * + * @since 1.22 + */ class RedisPubSubFeedEngine implements RCFeedEngine { + /** - * Emit a recent change notification via Redis Pub/Sub - * - * If the feed URI contains a path component, it will be used to generate a - * channel name by stripping the leading slash and replacing any remaining - * slashes with '.'. If no path component is present, the channel is set to - * 'rc'. If the URI contains a query string, its parameters will be parsed - * as RedisConnectionPool options. - * - * @example $wgRCFeeds['redis'] = array( - * 'formatter' => 'JSONRCFeedFormatter', - * 'uri' => "redis://127.0.0.1:6379/rc.$wgDBname", - * ); - * - * @since 1.22 + * @see RCFeedEngine::send */ public function send( array $feed, $line ) { - $parsed = parse_url( $feed['uri'] ); + $parsed = wfParseUrl( $feed['uri'] ); $server = $parsed['host']; $options = array( 'serializer' => 'none' ); $channel = 'rc'; @@ -36,6 +61,11 @@ class RedisPubSubFeedEngine implements RCFeedEngine { } $pool = RedisConnectionPool::singleton( $options ); $conn = $pool->getConnection( $server ); - $conn->publish( $channel, $line ); + if ( $conn !== false ) { + $conn->publish( $channel, $line ); + return true; + } else { + return false; + } } } diff --git a/includes/rcfeed/UDPRCFeedEngine.php b/includes/rcfeed/UDPRCFeedEngine.php index beeb73bd..8554670e 100644 --- a/includes/rcfeed/UDPRCFeedEngine.php +++ b/includes/rcfeed/UDPRCFeedEngine.php @@ -1,7 +1,31 @@ <?php + +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Sends the notification to the specified host in a UDP packet. + * @since 1.22 + */ + class UDPRCFeedEngine implements RCFeedEngine { /** - * Sends the notification to the specified host in a UDP packet. * @see RCFeedEngine::send */ public function send( array $feed, $line ) { diff --git a/includes/rcfeed/XMLRCFeedFormatter.php b/includes/rcfeed/XMLRCFeedFormatter.php new file mode 100644 index 00000000..d4df91a4 --- /dev/null +++ b/includes/rcfeed/XMLRCFeedFormatter.php @@ -0,0 +1,29 @@ +<?php +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * @since 1.23 + */ +class XMLRCFeedFormatter extends MachineReadableRCFeedFormatter { + + protected function formatArray( array $packet ) { + return ApiFormatXml::recXmlPrint( 'recentchange', $packet, 0 ); + } +} |