Implement email notification in Echo. No way to turn it off yet, but at least it is there :).

Change-Id: Ie7c3d776d3698264d18ccaec90cc39aae83761dd
This commit is contained in:
Andrew Garrett 2012-05-18 01:36:18 +10:00
parent 75301a8ed9
commit 8839f98735
6 changed files with 125 additions and 18 deletions

View file

@ -26,6 +26,35 @@ $messages['en'] = array(
// Notification
'notification-edit-talk-page' => '$2 edited your talk page $3',
'notification-edit' => '$2 edited $3 $4',
'notification-edit-email-subject' => '{{SITENAME}} notification: $3 has been edited by $2',
'notification-edit-email-body' => 'Hello $5,
This is a notification to let you know that $2 has edited the {{SITENAME}} page $3.
You can see the changes that $2 made by following this link:
<$4>
You are receiving this message because you have subscribed to email updates for changes to this page.
Thanks for using {{SITENAME}}
The {{SITENAME}} notification system',
'notification-edit-talk-page-email-subject' => 'Your {{SITENAME}} talk page has been edited by $2',
'notification-edit-talk-page-email-body' => 'Hello $4,
This is a notification to let you know that $2 has edited your talk page on {{SITENAME}}.
On {{SITENAME}}, your talk page is where other users can leave you messages.
You can see the changes that $2 made at this link:
<$3>
Thanks for using {{SITENAME}}
The {{SITENAME}} notification system',
// Email notification
'echo-email-subject-default' => 'New notification at {{SITENAME}}',
'echo-email-body-default' => 'You have a new notification at {{SITENAME}}:
$1',
);
/**
@ -50,6 +79,18 @@ $messages['qqq'] = array(
'echo-anon' => 'Error message shown to users who try to visit Special:Notifications as an anon.',
// Notification
'notification-edit-talk-page' => 'Format for displaying notifications of a user talk page being edited',
'notification-edit' => 'Format for displaying notifications of a page being edited (generally from a watchlist)',
'notification-edit-talk-page' => 'Format for displaying notifications of a user talk page being edited
* $1 is the username of the person who edited, plain text.
* $2 is the username of the person who edited, formatted.
* $3 is a diff link, formatted.',
'notification-edit' => 'Format for displaying notifications of a page being edited (generally from a watchlist)
* $1 is the username of the person who edited, plain text.
* $2 is the username of the person who edited, formatted.
* $3 is the page that was edited, formatted.
* $4 is a diff link, possibly formatted.',
// Email notification
'echo-email-subject-default' => 'Default subject for Echo email notifications',
'echo-email-body-default' => 'Default message content for Echo email notifications.
* $1 is a plain text description of the notification.',
);

View file

@ -101,6 +101,7 @@ $wgHooks['ArticleSaveComplete'][] = 'EchoHooks::onArticleSaved';
$wgEchoDefaultNotificationTypes = array(
'all' => array(
'notify' => true,
'email' => true,
),
);
@ -113,11 +114,15 @@ $wgEchoNotificationFormatters = array(
'edit-user-talk' => array(
'type' => 'edit',
'message-key' => 'notification-edit-talk-page',
'message-params' => array('agent', 'difflink'),
'message-params' => array('agent', 'difflink', 'user'),
'email-subject-message' => 'notification-edit-talk-page-email-subject',
'email-body-message' => 'notification-edit-talk-page-email-body',
),
'edit' => array(
'type' => 'edit',
'message-key' => 'notification-edit',
'message-params' => array('agent', 'title', 'difflink'),
'message-params' => array('agent', 'title', 'difflink', 'user'),
'email-subject-message' => 'notification-edit-email-subject',
'email-body-message' => 'notification-edit-email-body',
),
);

View file

@ -5,7 +5,6 @@ class EchoNotifier {
/**
* Record an EchoNotification for an EchoEvent.
*
* @todo Implement.
* @param $user The User to notify.
* @param $event The EchoEvent to notify about.
*/
@ -16,11 +15,23 @@ class EchoNotifier {
/**
* Send a Notification to a user by email
*
* @todo Implement.
* @param $user The User to notify.
* @param $event The EchoEvent to notify about.
*/
public static function notifyWithEmail( $user, $event ) {
// throw new MWException( "Not implemented" );
if ( ! $user->isEmailConfirmed() ) {
// No valid email address
return false;
}
global $wgPasswordSender, $wgPasswordSenderName;
$adminAddress = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
$address = new MailAddress( $user );
$email = EchoNotificationController::formatNotification( $event, $user, 'email' );
$subject = $email['subject'];
$body = $email['body'];
UserMailer::send($address, $adminAddress, $subject, $body );
}
}

View file

@ -5,6 +5,7 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
protected $messageParams = false;
public function __construct( $params ) {
parent::__construct($params);
if ( !isset($params['message-key']) || !isset($params['message-params']) ) {
throw new MWException("message-key and message-params parameters are required for an EchoBasicFormatter");
}
@ -16,19 +17,62 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
public function format( $event, $user, $type ) {
$messageParams = array();
$message = wfMessage( $this->messageKey );
foreach( $this->messageParams as $param ) {
$this->processParam( $event, $param, $message );
if ( $this->outputFormat === 'email' ) {
return $this->formatEmail( $event, $user, $type );
}
if ( $this->outputFormat == 'html' ) {
$message = wfMessage( $this->messageKey );
$this->processParams( $event, $message, $user );
if ( $this->outputFormat === 'html' ) {
return $message->parse();
} else {
return $message->text();
}
}
protected function processParam( $event, $param, $message ) {
protected function formatEmail( $event, $user, $type ) {
$params = $this->parameters;
$language = $user->getOption('language');
if ( isset($params['email-subject-message']) ) {
$subjectMsg = wfMessage( $params['email-subject-message'] )
->inLanguage($language);
$this->processParams( $event, $subjectMsg, $user );
$subject = $subjectMsg
->text();
} else {
$subject = wfMessage('echo-email-subject-default')
->inLanguage($language)
->text();
}
if ( isset( $params['email-body-message']) ) {
$bodyMsg = wfMessage( $params['email-body-message'] )
->inLanguage($language);
$this->processParams( $event, $bodyMsg, $user );
$body = $bodyMsg
->text();
} else {
$this->setOutputFormat('text');
$textNotification = $this->format( $event, $user, $type );
$this->setOutputFormat('email');
$body = wfMessage('echo-email-body-default')
->inLanguage($language)
->params($textNotification)
->text();
}
return array( 'subject' => $subject, 'body' => $body );
}
protected function processParams( $event, $message, $user ) {
foreach( $this->messageParams as $param ) {
$this->processParam( $event, $param, $message, $user );
}
}
protected function processParam( $event, $param, $message, $user ) {
if ( $param === 'agent' ) {
// Actually converts to two parameters for gender support
if ( ! $event->getAgent() ) {
@ -38,12 +82,16 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
$message->params( $agent->getName() );
$message->rawParams( $this->formatUser($agent) );
}
} elseif ( $param === 'user' ) {
$message->params( $user->getName() );
} elseif ( $param === 'title' ) {
if ( ! $event->getTitle() ) {
$message->params( wfMsg('echo-no-title') );
} else {
$message->rawParams( $this->formatTitle( $event->getTitle() ) );
}
} else {
throw new MWException( "Unrecognised parameter $param" );
}
}
}

View file

@ -1,9 +1,7 @@
<?php
class EchoEditFormatter extends EchoBasicFormatter {
protected function processParam( $event, $param, $message ) {
parent::processParam( $event, $param, $message );
protected function processParam( $event, $param, $message, $user ) {
if ( $param === 'difflink' ) {
$eventData = $event->getExtra();
if ( !isset($eventData['revid']) ) {
@ -32,11 +30,13 @@ class EchoEditFormatter extends EchoBasicFormatter {
);
$message->rawParams($link);
} else {
$link = Linker::linkUrl( $title,
$link = $title->getFullURL(
array( 'oldid' => $revid, 'diff' => 'prev' ) );
$message->params($link);
}
} else {
parent::processParam( $event, $param, $message, $user );
}
}
}

View file

@ -5,7 +5,7 @@ abstract class EchoNotificationFormatter {
'basic' => 'EchoBasicFormatter',
'edit' => 'EchoEditFormatter',
);
protected $validOutputFormats = array('text', 'html');
protected $validOutputFormats = array('text', 'html', 'email');
protected $outputFormat = 'text';
/**
@ -13,7 +13,9 @@ abstract class EchoNotificationFormatter {
*
* @param $parameters Associative array of parameters
*/
public abstract function __construct( $parameters );
public function __construct( $parameters ) {
$this->parameters = $parameters;
}
/**
* Shows a notification in human-readable format.