From 7e9a35107b7f5eea3c7e460626391facfbd92d18 Mon Sep 17 00:00:00 2001 From: Kaldari Date: Tue, 27 Nov 2012 18:17:52 -0800 Subject: [PATCH] Allowing notifications to support multiple predefined components in the payload. Payload components can include edit summary, edit snippet, welcome message. This replaces the older 'content' system which constructed a payload from an arbitrary message + parameters (but only supported a single message). Change-Id: I9a5e1d69f0c8296bb2bb79cb3f26e67aa34592bb --- Echo.i18n.php | 5 +- Echo.php | 13 ++-- formatters/BasicFormatter.php | 90 +++++++++++++++++----------- formatters/EditFormatter.php | 2 +- formatters/NotificationFormatter.php | 38 +++++++++++- modules/base/ext.echo.base.css | 3 +- 6 files changed, 102 insertions(+), 49 deletions(-) diff --git a/Echo.i18n.php b/Echo.i18n.php index 80947cd64..fbcb2bc5b 100644 --- a/Echo.i18n.php +++ b/Echo.i18n.php @@ -31,7 +31,7 @@ $messages['en'] = array( // Notification 'notification-edit' => '$2 {{GENDER:$1|edited}} [[$3]] $4: "$5"', - 'notification-edit-talk-page' => '$2 {{GENDER:$1|edited}} [[User talk:$4|your talk page]] $3: "$5"', + 'notification-edit-talk-page' => '$2 {{GENDER:$1|edited}} [[User talk:$3|your talk page]]. $4', 'notification-add-comment' => '$2 {{GENDER:$1|commented}} on "[[$4|$3]]" on the "$5" talk page', 'notification-add-talkpage-topic' => '$2 {{GENDER:$1|posted}} a new topic "$3" on [[$4]]', 'notification-add-talkpage-topic-yours' => '$2 {{GENDER:$1|sent}} you a message: "[[$4#$3|$3]]"', @@ -140,8 +140,7 @@ $messages['qqq'] = array( * $1 is the username of the person who edited, plain text. Can be used for GENDER. * $2 is the username of the person who edited, HTML formatted as the link to the user\'s page. * $3 is a diff link, formatted as an HTML link with the text "(diff)". -* $4 is the current user\'s name, used in the link to their talk page. -* $5 is the edit summary.', +* $4 is the current user\'s name, used in the link to their talk page.', 'notification-add-comment' => 'Format for displaying notifications of a comment being added to an existing discussion. Parameters: * $1 is the username of the person who edited, plain text. Can be used for GENDER, * $2 is the username of the person who edited, diff --git a/Echo.php b/Echo.php index 6e6f7fe4b..1f93fd7d2 100644 --- a/Echo.php +++ b/Echo.php @@ -189,7 +189,8 @@ $wgEchoNotificationFormatters = array( 'edit-user-talk' => array( 'type' => 'edit', 'title-message' => 'notification-edit-talk-page', - 'title-params' => array( 'agent', 'difflink', 'user', 'summary' ), + 'title-params' => array( 'agent', 'user', 'difflink' ), + 'payload' => array( 'summary' ), 'email-subject-message' => 'notification-edit-talk-page-email-subject', 'email-subject-params' => array( 'agent' ), 'email-body-message' => 'notification-edit-talk-page-email-body', @@ -211,8 +212,7 @@ $wgEchoNotificationFormatters = array( 'title-message' => 'notification-add-comment', 'title-message-yours' => 'notification-add-comment-yours', 'title-params' => array( 'agent', 'subject', 'title', 'content-page' ), - 'content-message' => 'notification-talkpage-content', - 'content-params' => array( 'commentText' ), + 'payload' => array( 'snippet' ), 'icon' => 'chat', ), 'add-talkpage-topic' => array( @@ -220,22 +220,21 @@ $wgEchoNotificationFormatters = array( 'title-message' => 'notification-add-talkpage-topic', 'title-message-yours' => 'notification-add-talkpage-topic-yours', 'title-params' => array( 'agent', 'subject', 'title', 'content-page' ), - 'content-message' => 'notification-talkpage-content', - 'content-params' => array( 'commentText' ), + 'payload' => array( 'snippet' ), 'icon' => 'chat', ), 'welcome' => array( 'type' => 'welcome', 'title-message' => 'notification-new-user', 'title-params' => array( 'agent' ), - 'content-message' => 'notification-new-user-content', - 'content-params' => array( 'agent' ), + 'payload' => array( 'welcome' ), 'icon' => 'w', ), 'reverted' => array( 'type' => 'edit', 'title-message' => 'notification-reverted', 'title-params' => array( 'agent', 'title', 'difflink', 'summary', 'number' ), + 'payload' => array( 'summary' ), 'email-subject-message' => 'notification-reverted-email-subject', 'email-subject-params' => array( 'agent', 'title', 'summary' ), 'email-body-message' => 'notification-reverted-email-body', diff --git a/formatters/BasicFormatter.php b/formatters/BasicFormatter.php index ecdcfafc9..21488741c 100644 --- a/formatters/BasicFormatter.php +++ b/formatters/BasicFormatter.php @@ -7,6 +7,7 @@ class EchoBasicFormatter extends EchoNotificationFormatter { 'title-message', 'title-params', ); + protected $validPayloadComponents = array( 'summary', 'snippet', 'welcome' ); protected $title, $content, $email, $icon; @@ -16,16 +17,10 @@ class EchoBasicFormatter extends EchoNotificationFormatter { $this->title = array(); $this->title['message'] = $params['title-message']; $this->title['params'] = $params['title-params']; - - if ( isset( $params['content-message'] ) ) { - $this->content = array(); - $this->content['message'] = $params['content-message']; - - if ( isset( $params['content-params'] ) ) { - $this->content['params'] = $params['content-params']; - } else { - $this->content['params'] = array(); - } + $this->payload = array(); + + if ( isset( $params['payload'] ) ) { + $this->payload = $params['payload']; } $this->email = array(); @@ -94,14 +89,32 @@ class EchoBasicFormatter extends EchoNotificationFormatter { $output = Xml::tags( 'div', array( 'class' => 'mw-echo-title' ), $title ) . "\n"; // Build the notification content - if ( !is_null( $this->content ) ) { - $content = $this->formatContent( $event, $user ); - $content .= ' ' . $this->formatTimestamp( $event->getTimestamp(), $user ); - $output .= Xml::tags( 'div', array( 'class' => 'mw-echo-content' ), $content ) . "\n"; - } else { - $content = $this->formatTimestamp( $event->getTimestamp(), $user ); - $output .= Xml::tags( 'div', array( 'class' => 'mw-echo-content' ), $content ) . "\n"; + $content = ''; + foreach ( $this->payload as $payloadComponent ) { + if ( in_array( $payloadComponent, $this->validPayloadComponents ) ) { + switch ( $payloadComponent ) { + case 'summary': + $content .= $this->formatSummary( $event, $user ); + break; + case 'snippet': + // TODO: build this + break; + case 'welcome': + $details = array( + 'message' => 'notification-new-user-content', + 'params' => array( 'agent' ) + ); + $content .= $this->formatFragment( $details, $event, $user )->parse(); + break; + } + } else { + throw new MWException( "Unrecognised payload component $payloadComponent" ); + } } + $output .= Xml::tags( 'div', array( 'class' => 'mw-echo-content' ), $content ) . "\n"; + + // Add timestamp + $output .= $this->formatTimestamp( $event->getTimestamp(), $user ); // Add the notification icon if ( !is_null( $this->icon ) ) { @@ -118,23 +131,6 @@ class EchoBasicFormatter extends EchoNotificationFormatter { return $this->formatFragment( $this->title, $event, $user ); } - protected function formatContent( $event, $user ) { - if ( is_null( $this->content ) ) { - return ''; - } - - return $this->formatFragment( $this->content, $event, $user )->parse(); - } - - protected function formatFragment( $details, $event, $user ) { - $message = wfMessage( $details['message'] ) - ->inLanguage( $user->getOption( 'language' ) ); - - $this->processParams( $details['params'], $event, $message, $user ); - - return $message; - } - protected function formatEmail( $event, $user, $type ) { $subject = $this->formatFragment( $this->email['subject'], $event, $user )->text(); @@ -143,6 +139,30 @@ class EchoBasicFormatter extends EchoNotificationFormatter { return array( 'subject' => $subject, 'body' => $body ); } + /** + * Creates a notification fragment based on a message and parameters + * + * @param $details array An i18n message and parameters to pass to the message + * @param $event EchoEvent that the notification is for. + * @param $user User to format the notification for. + * @return string + */ + protected function formatFragment( $details, $event, $user ) { + $message = wfMessage( $details['message'] ); + + $this->processParams( $details['params'], $event, $message, $user ); + + return $message; + } + + /** + * Convert the parameters into real values and pass them into the message + * + * @param $params array + * @param $event EchoEvent + * @param $message Message + * @param $user User + */ protected function processParams( $params, $event, $message, $user ) { foreach ( $params as $param ) { $this->processParam( $event, $param, $message, $user ); @@ -150,6 +170,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter { } /** + * Helper function for processParams() + * * @param $event EchoEvent * @param $param * @param $message Message diff --git a/formatters/EditFormatter.php b/formatters/EditFormatter.php index e88715b40..b5730f68c 100644 --- a/formatters/EditFormatter.php +++ b/formatters/EditFormatter.php @@ -26,7 +26,7 @@ class EchoEditFormatter extends EchoBasicFormatter { if ( $this->outputFormat === 'html' ) { $link = Linker::link( $title, - wfMessage( 'parentheses', wfMessage( 'diff' )->text() )->escaped(), + wfMessage( 'parentheses', wfMessage( 'showdiff' )->text() )->escaped(), array( 'class' => 'mw-echo-diff', ), diff --git a/formatters/NotificationFormatter.php b/formatters/NotificationFormatter.php index 0fc6bc348..9a82ad7ce 100644 --- a/formatters/NotificationFormatter.php +++ b/formatters/NotificationFormatter.php @@ -1,5 +1,11 @@ 'EchoBasicFormatter', @@ -9,6 +15,7 @@ abstract class EchoNotificationFormatter { ); protected $validOutputFormats = array( 'text', 'html', 'email' ); protected $outputFormat = 'text'; + protected $parameters = array(); protected $requiredParameters = array(); /** @@ -124,6 +131,33 @@ abstract class EchoNotificationFormatter { $ts = $language->userTimeAndDate( $ts, $user ); } - return Xml::element( 'span', array( 'class' => 'mw-echo-timestamp' ), $ts ); + if ( $this->outputFormat === 'html' ) { + return Xml::element( 'div', array( 'class' => 'mw-echo-timestamp' ), $ts ); + } else { + return $ts; + } + } + + /** + * Formats an edit summary + * + * @param $event EchoEvent that the notification is for. + * @param $user User to format the notification for. + * @return string The edit summary (or empty string) + */ + protected function formatSummary( $event, $user ) { + $eventData = $event->getExtra(); + if ( !isset( $eventData['revid'] ) ) { + return ''; + } + $revision = Revision::newFromId( $eventData['revid'] ); + if ( $revision ) { + $summary = $revision->getComment( Revision::FOR_THIS_USER, $user ); + if ( $this->outputFormat === 'html' ) { + $summary = Xml::tags( 'div', array( 'class' => 'mw-echo-summary' ), htmlspecialchars( $summary ) ); + } + return $summary; + } + return ''; } } diff --git a/modules/base/ext.echo.base.css b/modules/base/ext.echo.base.css index b8353abbe..5ba388933 100644 --- a/modules/base/ext.echo.base.css +++ b/modules/base/ext.echo.base.css @@ -3,8 +3,7 @@ font-size: 9px; } -div.mw-echo-timestamp { - text-align: right; +.mw-echo-unread { } .mw-echo-title {