mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-11-27 17:20:40 +00:00
Initial check-in for email batch
patch set 3: * add gender support to various messages * tweak variables a little bit, e.g. move class variables to function local variables patch set 4: * update various email to e-mail in i18n file * add support to process only valid echo events * add global email footer * add the new table schema to core schema file patch set 5: * remove trailing white space * add missing semicolon to return statement in Notifier::notifyWithEmail() patch set 8: * some change based on newest feature requirement Change-Id: I3298617dab4c04c4d6d486469120fc2d0c986b66
This commit is contained in:
parent
f519bca628
commit
6b8571403a
|
@ -43,46 +43,46 @@ Please remember to sign any comments on talk pages with 4 tildes (~~~~).',
|
|||
'notification-reverted' => '$2 {{GENDER:$1|reverted}} your {{PLURAL:$5|1=edit|edits}} on [[$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.
|
||||
This is a notification to let you know that $2 {{GENDER:$2|has edited}} the {{SITENAME}} page $3.
|
||||
|
||||
$2 summarized their changes with this comment: $6
|
||||
$2 {{GENDER:$2|summarized}} {{GENDER:$2|his|her|their}} change with this comment: $6
|
||||
|
||||
You can see the changes that $2 made by following this link:
|
||||
You can see the change that $2 {{GENDER:$2|made}} by following this link:
|
||||
<$4>
|
||||
|
||||
You are receiving this message because you have subscribed to email updates for changes to this page.
|
||||
You are receiving this message because you have subscribed to e-mail updates for changes to this page.
|
||||
|
||||
$7',
|
||||
'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}}.
|
||||
This is a notification to let you know that $2 {{GENDER:$2|has edited}} your talk page on {{SITENAME}}.
|
||||
|
||||
On {{SITENAME}}, your talk page is where other users can leave you messages.
|
||||
|
||||
$2 used the following summary to describe their change: $5
|
||||
$2 {{GENDER:$2|used}} the following summary to describe {{GENDER:$2|his|her|their}} change: $5
|
||||
|
||||
You can see the changes that $2 made at this link:
|
||||
You can see the change that $2 {{GENDER:$2|made}} at this link:
|
||||
<$3>
|
||||
|
||||
$6',
|
||||
|
||||
'notification-reverted-email-subject' => '{{SITENAME}} notification: $2 has reverted your edit on $3: $4',
|
||||
'notification-edit-talk-page-email-batch-body' => '$2 {{GENDER:$2|posted}} on your talk page',
|
||||
'notification-reverted-email-subject' => '{{SITENAME}} notification: $2 {{GENDER:$2|has reverted}} your edit on $3: $4',
|
||||
'notification-reverted-email-body' => 'Hello $5,
|
||||
This is a notification to let you know that $2 has reverted your edits on the {{SITENAME}} page $3.
|
||||
This is a notification to let you know that $2 {{GENDER:$2|has reverted}} your edits on the {{SITENAME}} page $3.
|
||||
|
||||
$2 used the following summary to describe their change: $6
|
||||
$2 {{GENDER:$2|used}} the following summary to describe {{GENDER:$2|his|her|their}} change: $6
|
||||
|
||||
You can see the change that $2 made to revert your edits by following this link:
|
||||
You can see the change that $2 {{GENDER:$2|made}} to revert your edits by following this link:
|
||||
<$4>
|
||||
|
||||
You are receiving this message because you have subscribed to email alerts
|
||||
You are receiving this message because you have subscribed to e-mail alerts
|
||||
when your changes are reverted.
|
||||
|
||||
$7',
|
||||
'echo-notification-count' => '$1+',
|
||||
|
||||
// Email notification
|
||||
'notification-reverted-email-batch-body' => 'Your edit to $3 was reverted by $2',
|
||||
// E-mail notification
|
||||
'echo-email-subject-default' => 'New notification at {{SITENAME}}',
|
||||
'echo-email-body-default' => 'You have a new notification at {{SITENAME}}:
|
||||
|
||||
|
@ -107,6 +107,31 @@ $1',
|
|||
'echo-date-yesterday' => 'Yesterday',
|
||||
'echo-date-header' => '$1 $2',
|
||||
'echo-load-more-error' => 'An error occurred while fetching more results.',
|
||||
|
||||
// E-mail batch
|
||||
'echo-email-batch-separator' => '________________________________________________', # only translate this message to other languages if you have to change it
|
||||
'echo-email-batch-bullet' => '•', # only translate this message to other languages if you have to change it
|
||||
'echo-email-batch-subject-daily' => 'You have $1 {{PLURAL:$2|notification|notifications}} today',
|
||||
'echo-email-batch-subject-weekly' => 'You have $1 {{PLURAL:$2|notification|notifications}} this week',
|
||||
'echo-email-batch-body-daily' => '$1,
|
||||
|
||||
You have $2 {{PLURAL:$3|notification|notifications}} on {{SITENAME}} today. View them here:
|
||||
{{canonicalurl:{{#special:Notifications}}}}
|
||||
|
||||
$4
|
||||
|
||||
$5',
|
||||
'echo-email-batch-body-weekly' => '$1,
|
||||
|
||||
You have $2 {{PLURAL:$3|notification|notifications}} on {{SITENAME}} this week. View them here:
|
||||
{{canonicalurl:{{#special:Notifications}}}}
|
||||
|
||||
$4
|
||||
|
||||
$5',
|
||||
'echo-email-batch-category-header-edit-user-talk' => '$1 Talk page {{PLURAL:$1|message|messages}}',
|
||||
'echo-email-batch-category-header-edit-revert' => '$1 Edit {{PLURAL:$1|revert|reverts}}',
|
||||
'echo-email-batch-category-header-other' => '$1 {{PLURAL:$1|Other|Others}}',
|
||||
);
|
||||
|
||||
/** Message documentation (Message documentation)
|
||||
|
@ -189,15 +214,17 @@ $messages['qqq'] = array(
|
|||
* $4 is a link to a change
|
||||
* $5 is the e-mail recipient's username.
|
||||
* $6 is the edit summary.
|
||||
* $7 is the email footer, {{msg|echo-email-footer-default}}", # Fuzzy
|
||||
* $7 is the e-mail footer, {{msg|echo-email-footer-default}}", # Fuzzy
|
||||
'notification-edit-talk-page-email-subject' => 'E-mail subject. Parameters:
|
||||
* $2 is a username.',
|
||||
'notification-edit-talk-page-email-body' => "E-mail notification. Parameters:
|
||||
* $2 is a username
|
||||
* $3 link to a change
|
||||
* $3 is a link to a change
|
||||
* $4 is the e-mail recipient's username.
|
||||
* $5 is the edit summary.
|
||||
* $6 is the email footer, {{msg|echo-email-footer-default}}", # Fuzzy
|
||||
* $6 is the e-mail footer, {{msg|echo-email-footer-default}}", # Fuzzy
|
||||
'notification-edit-talk-page-email-batch-body' => 'E-mail notification for talk page edit
|
||||
* $2 is a username',
|
||||
'notification-reverted-email-subject' => 'E-mail subject. Parameters:
|
||||
*$2 is a username
|
||||
*$3 is a page title
|
||||
|
@ -212,10 +239,13 @@ $messages['qqq'] = array(
|
|||
'echo-notification-count' => '{{optional}}
|
||||
The new notification count next to notification link, for example: 99+
|
||||
* $1 is the count',
|
||||
'echo-email-subject-default' => 'Default subject for Echo email notifications',
|
||||
'echo-email-body-default' => 'Default message content for Echo email notifications.
|
||||
'notification-reverted-email-batch-body' => 'E-mail notification for page revert. Parameters:
|
||||
* $2 is a username
|
||||
* $3 is a page title',
|
||||
'echo-email-subject-default' => 'Default subject for Echo e-mail notifications',
|
||||
'echo-email-body-default' => 'Default message content for Echo e-mail notifications.
|
||||
* $1 is a plain text description of the notification.',
|
||||
'echo-email-footer-default' => 'Default footer content for Echo email notifications. Parameters:
|
||||
'echo-email-footer-default' => 'Default footer content for Echo e-mail notifications. Parameters:
|
||||
* $1 is the address of the organization that sent the e-mail',
|
||||
'echo-link-new' => 'Shown in "personal links" when a user has unread notifications.
|
||||
* $1 is number of unread notifications',
|
||||
|
@ -228,6 +258,31 @@ The new notification count next to notification link, for example: 99+
|
|||
* $1 is the month, it could be {{january-gen}}, {{february-gen}}, {{march-gen}}, {{april-gen}}, {{may-gen}}, {{june-gen}}, {{july-gen}}, {{august-gen}}, {{september-gen}}, {{october-gen}}, {{november-gen}}, {{december-gen}}
|
||||
* $2 is the date of a month, eg 21',
|
||||
'echo-load-more-error' => 'Error message for errors in loading more notifications',
|
||||
'echo-email-batch-separator' => 'Email batch content separator',
|
||||
'echo-email-batch-subject-daily' => 'Daily e-mail batch subject.
|
||||
* $1 could be a numeric count or {{echo-notification-count}}
|
||||
* $2 is a numeric count, this is used for plural support',
|
||||
'echo-email-batch-subject-weekly' => 'Weekly e-mail batch subject.
|
||||
* $1 could be a numeric count or {{echo-notification-count}}
|
||||
* $2 is a numeric count, this is used for plural support',
|
||||
'echo-email-batch-body-daily' => 'Daily e-mail batch body. Parameters:
|
||||
* $1 is a username
|
||||
* $2 could be a numeric count or {{echo-notification-count}}
|
||||
* $3 is a numeric count, this is used for plural support
|
||||
* $4 is the e-mail batch content separated by {{echo-email-batch-separator}}
|
||||
* $5 is the e-mail footer, {{msg|echo-email-footer-default}}',
|
||||
'echo-email-batch-body-weekly' => 'Weekly e-mail batch body. Parameters:
|
||||
* $1 is a username
|
||||
* $2 could be a numeric count or {{echo-notification-count}}
|
||||
* $3 is a numeric count, this is used for plural support
|
||||
* $4 is the e-mail batch content separated by {{echo-email-batch-separator}}
|
||||
* $5 is the e-mail footer, {{msg|echo-email-footer-default}}',
|
||||
'echo-email-batch-category-header-edit-user-talk' => 'E-mail batch section title for edit-user-talk category
|
||||
* $1 is the numeric count',
|
||||
'echo-email-batch-category-header-edit-revert' => 'E-mail batch section title for edit-revert category
|
||||
* $1 is the numeric count',
|
||||
'echo-email-batch-category-header-other' => 'E-mail batch section title for events with category not specified
|
||||
* $1 is the numeric count',
|
||||
);
|
||||
|
||||
/** Afrikaans (Afrikaans)
|
||||
|
|
28
Echo.php
28
Echo.php
|
@ -50,6 +50,7 @@ $wgAutoloadClasses['EchoHooks'] = "$dir/Hooks.php";
|
|||
$wgAutoloadClasses['EchoSubscription'] = "$dir/model/Subscription.php";
|
||||
$wgAutoloadClasses['EchoEvent'] = "$dir/model/Event.php";
|
||||
$wgAutoloadClasses['EchoNotification'] = "$dir/model/Notification.php";
|
||||
$wgAutoloadClasses['MWEchoEmailBatch'] = "$dir/includes/EmailBatch.php";
|
||||
|
||||
// Formatters
|
||||
$wgAutoloadClasses['EchoNotificationFormatter'] = "$dir/formatters/NotificationFormatter.php";
|
||||
|
@ -159,6 +160,8 @@ $wgHooks['ArticleEditUpdateNewTalk'][] = 'EchoHooks::abortNewTalkNotification';
|
|||
$wgDefaultUserOptions['echo-notify-link'] = 'true';
|
||||
|
||||
$wgEchoDisableStandardEmail = true;
|
||||
// Whether to turn on email batch function
|
||||
$wgEchoEnableEmailBatch = true;
|
||||
|
||||
// The organization address, the value should be defined in LocalSettings.php
|
||||
$wgEchoEmailFooterAddress = '';
|
||||
|
@ -187,12 +190,27 @@ $wgEchoNotifiers = array(
|
|||
// extensions can add to this list through the BeforeCreateEchoEvent hook.
|
||||
$wgEchoEnabledEvents = array(
|
||||
'edit-user-talk',
|
||||
'add-comment',
|
||||
'add-talkpage-topic',
|
||||
//'add-comment',
|
||||
//'add-talkpage-topic',
|
||||
'welcome',
|
||||
'reverted',
|
||||
);
|
||||
|
||||
// This array stores the category and priority for enabled events
|
||||
// if an event is not specified, it means the event belongs to the
|
||||
// 'other' category with priority 10, which is the lowest,
|
||||
// priority is ranging from 1 to 10
|
||||
$wgEchoEventDetails = array(
|
||||
'edit-user-talk' => array(
|
||||
'category' => 'edit-user-talk',
|
||||
'priority' => 1
|
||||
),
|
||||
'reverted' => array(
|
||||
'category' => 'edit-revert',
|
||||
'priority' => 9
|
||||
),
|
||||
);
|
||||
|
||||
// Definitions of the notifications built into Echo
|
||||
$wgEchoNotificationFormatters = array(
|
||||
'edit-user-talk' => array(
|
||||
|
@ -204,6 +222,8 @@ $wgEchoNotificationFormatters = array(
|
|||
'email-subject-params' => array( 'agent' ),
|
||||
'email-body-message' => 'notification-edit-talk-page-email-body',
|
||||
'email-body-params' => array( 'agent', 'difflink', 'user', 'summary', 'email-footer' ),
|
||||
'email-body-batch-message' => 'notification-edit-talk-page-email-batch-body',
|
||||
'email-body-batch-params' => array( 'agent', 'difflink', 'summary' ),
|
||||
'icon' => 'chat',
|
||||
),
|
||||
'edit' => array(
|
||||
|
@ -214,6 +234,8 @@ $wgEchoNotificationFormatters = array(
|
|||
'email-subject-params' => array( 'agent', 'title' ),
|
||||
'email-body-message' => 'notification-edit-email-body',
|
||||
'email-body-params' => array( 'agent', 'title', 'difflink', 'user', 'summary', 'email-footer' ),
|
||||
'email-body-batch-message' => 'notification-edit-email-batch-body',
|
||||
'email-body-batch-params' => array( 'agent', 'title', 'difflink', 'summary' ),
|
||||
'icon' => 'w',
|
||||
),
|
||||
'add-comment' => array(
|
||||
|
@ -248,6 +270,8 @@ $wgEchoNotificationFormatters = array(
|
|||
'email-subject-params' => array( 'agent', 'title', 'summary' ),
|
||||
'email-body-message' => 'notification-reverted-email-body',
|
||||
'email-body-params' => array( 'agent', 'title', 'difflink', 'user', 'summary', 'email-footer' ),
|
||||
'email-body-batch-message' => 'notification-reverted-email-batch-body',
|
||||
'email-body-batch-params' => array( 'agent', 'title', 'difflink', 'summary' ),
|
||||
'icon' => 'revert',
|
||||
)
|
||||
);
|
||||
|
|
|
@ -10,6 +10,7 @@ class EchoHooks {
|
|||
$dir = __DIR__;
|
||||
$baseSQLFile = "$dir/echo.sql";
|
||||
$updater->addExtensionTable( 'echo_subscription', $baseSQLFile );
|
||||
$updater->addExtensionTable( 'echo_email_batch', "$dir/db_patches/echo_email_batch.sql" );
|
||||
|
||||
$updater->modifyField( 'echo_event', 'event_agent',
|
||||
"$dir/db_patches/patch-event_agent-split.sql", true );
|
||||
|
|
18
Notifier.php
18
Notifier.php
|
@ -26,8 +26,24 @@ class EchoNotifier {
|
|||
return false;
|
||||
}
|
||||
|
||||
global $wgPasswordSender, $wgPasswordSenderName;
|
||||
global $wgEchoEnableEmailBatch, $wgEchoEventDetails, $wgPasswordSender, $wgPasswordSenderName;
|
||||
|
||||
// batched email notification
|
||||
if ( $wgEchoEnableEmailBatch && $user->getOption( 'echo-email-frequency' ) > 0 ) {
|
||||
// default priority is 10
|
||||
$priority = 10;
|
||||
if ( isset( $wgEchoEventDetails[$event->getType()]['priority'] ) ) {
|
||||
$priority = $wgEchoEventDetails[$event->getType()]['priority'];
|
||||
}
|
||||
MWEchoEmailBatch::addToQueue( $user->getId(), $event->getId(), $priority );
|
||||
return true;
|
||||
}
|
||||
// no email notification
|
||||
if ( $user->getOption( 'echo-email-frequency' ) < 0 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// instant email notification
|
||||
$adminAddress = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
|
||||
$address = new MailAddress( $user );
|
||||
$email = EchoNotificationController::formatNotification( $event, $user, 'email' );
|
||||
|
|
9
db_patches/echo_email_batch.sql
Normal file
9
db_patches/echo_email_batch.sql
Normal file
|
@ -0,0 +1,9 @@
|
|||
CREATE TABLE /*_*/echo_email_batch (
|
||||
eeb_id int unsigned not null primary key auto_increment,
|
||||
eeb_user_id int unsigned not null,
|
||||
eeb_event_priority tinyint unsigned not null default 10, -- event priority
|
||||
eeb_event_id int unsigned not null
|
||||
) /*$wgDBTableOptions*/;
|
||||
|
||||
CREATE UNIQUE INDEX /*i*/echo_email_batch_user_event ON /*_*/echo_email_batch (eeb_user_id,eeb_event_id);
|
||||
CREATE UNIQUE INDEX /*i*/echo_email_batch_user_priority_event ON /*_*/echo_email_batch (eeb_user_id,eeb_event_priority,eeb_event_id);
|
10
echo.sql
10
echo.sql
|
@ -35,3 +35,13 @@ CREATE TABLE /*_*/echo_notification (
|
|||
|
||||
CREATE INDEX /*i*/user_timestamp ON /*_*/echo_notification (notification_user,notification_timestamp);
|
||||
CREATE UNIQUE INDEX /*i*/user_event ON /*_*/echo_notification (notification_user,notification_event);
|
||||
|
||||
CREATE TABLE /*_*/echo_email_batch (
|
||||
eeb_id int unsigned not null primary key auto_increment,
|
||||
eeb_user_id int unsigned not null,
|
||||
eeb_event_priority tinyint unsigned not null default 10, -- event priority
|
||||
eeb_event_id int unsigned not null
|
||||
) /*$wgDBTableOptions*/;
|
||||
|
||||
CREATE UNIQUE INDEX /*i*/echo_email_batch_user_event ON /*_*/echo_email_batch (eeb_user_id,eeb_event_id);
|
||||
CREATE UNIQUE INDEX /*i*/echo_email_batch_user_priority_event ON /*_*/echo_email_batch (eeb_user_id,eeb_event_priority,eeb_event_id);
|
||||
|
|
|
@ -57,6 +57,25 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
|
|||
} else {
|
||||
$this->email['body']['params'] = array( 'text-notification' );
|
||||
}
|
||||
} else {
|
||||
$this->email['body'] = array(
|
||||
'message' => 'echo-email-body-default',
|
||||
'params' => array(
|
||||
'text-notification',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
$this->email['batch-body'] = array();
|
||||
if ( isset( $params['email-body-batch-message'] ) ) {
|
||||
$this->email['batch-body']['message'] = $params['email-body-batch-message'];
|
||||
if ( isset( $params['email-body-batch-params'] ) ) {
|
||||
$this->email['batch-body']['params'] = $params['email-body-batch-params'];
|
||||
} else {
|
||||
$this->email['batch-body']['params'] = array();
|
||||
}
|
||||
} else {
|
||||
$this->email['batch-body'] = $this->email['body'];
|
||||
}
|
||||
|
||||
if ( isset( $params['icon'] ) ) {
|
||||
|
@ -143,7 +162,9 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
|
|||
|
||||
$body = $this->formatFragment( $this->email['body'], $event, $user )->text();
|
||||
|
||||
return array( 'subject' => $subject, 'body' => $body );
|
||||
$batchBody = $this->formatFragment( $this->email['batch-body'], $event, $user )->text();
|
||||
|
||||
return array( 'subject' => $subject, 'body' => $body, 'batch-body' => $batchBody );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
314
includes/EmailBatch.php
Normal file
314
includes/EmailBatch.php
Normal file
|
@ -0,0 +1,314 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Handles email batch for a user
|
||||
*/
|
||||
class MWEchoEmailBatch {
|
||||
|
||||
// the user to be notified
|
||||
protected $mUser;
|
||||
|
||||
// list of email content
|
||||
protected $content = array();
|
||||
// the last notification event of this batch
|
||||
protected $lastEvent;
|
||||
// the event count, this count is supported up to self::$displaySize + 1
|
||||
protected $count = 0;
|
||||
|
||||
// number of events to include in an email, we couldn't include
|
||||
// all events in a batch email
|
||||
protected static $displaySize = 10;
|
||||
|
||||
/**
|
||||
* @param $user User
|
||||
*/
|
||||
public function __construct( User $user ) {
|
||||
$this->mUser = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to determine whether to create a batch instance for this
|
||||
* user based on the user setting, this assumes the following value for
|
||||
* member setting for echo-email-frequency
|
||||
* -1 - no email
|
||||
* 0 - instant
|
||||
* 1 - once everyday
|
||||
* 7 - once every 7 days
|
||||
* @param $userId int
|
||||
* @return MWEchoEmailBatch/false
|
||||
*/
|
||||
public static function newFromUserId( $userId ) {
|
||||
$user = User::newFromId( intval( $userId ) );
|
||||
|
||||
$userEmailSetting = intval( $user->getOption( 'echo-email-frequency' ) );
|
||||
|
||||
// clear all existing events if user decides not to receive emails
|
||||
if ( $userEmailSetting == -1 ) {
|
||||
$emailBatch = new MWEchoEmailBatch( $user );
|
||||
$emailBatch->clearProcessedEvent();
|
||||
return false;
|
||||
}
|
||||
|
||||
$userLastBatch = $user->getOption( 'echo-email-last-batch' );
|
||||
|
||||
// send email batch, if
|
||||
// 1. it has been long enough since last email batch based on frequency
|
||||
// 2. there is no last batch timestamp recorded for the user
|
||||
// 3. user has switched from batch to instant email, send events left in the queue
|
||||
if ( $userLastBatch ) {
|
||||
// use 20 as hours per day to get estimate
|
||||
$nextBatch = wfTimestamp( TS_UNIX, $userLastBatch ) + $userEmailSetting * 20 * 60 * 60;
|
||||
if ( wfTimestamp( TS_MW, $nextBatch ) > wfTimestampNow() ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return new MWEchoEmailBatch( $user );
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper function that calls other functions required to process email batch
|
||||
*/
|
||||
public function process() {
|
||||
wfProfileIn( __METHOD__ );
|
||||
|
||||
// if there is no event for this user, exist the process
|
||||
if ( !$this->setLastEvent() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get valid events
|
||||
$events = $this->getEvents();
|
||||
|
||||
if ( $events ) {
|
||||
foreach( $events as $batchId => $row ) {
|
||||
$this->count++;
|
||||
if ( $this->count > self::$displaySize ) {
|
||||
break;
|
||||
}
|
||||
$event = EchoEvent::newFromRow( $row );
|
||||
$this->appendContent( $event );
|
||||
}
|
||||
|
||||
$this->sendEmail();
|
||||
}
|
||||
|
||||
$this->clearProcessedEvent();
|
||||
$this->updateUserLastBatchTimestamp();
|
||||
|
||||
wfProfileOut( __METHOD__ );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the last event of this batch, this is a cutoff point for clearing
|
||||
* processed/invalid events
|
||||
*
|
||||
* @return bool true if event exists false otherwise
|
||||
*/
|
||||
protected function setLastEvent() {
|
||||
$dbr = wfGetDB( DB_SLAVE );
|
||||
$res = $dbr->selectField(
|
||||
array( 'echo_email_batch' ),
|
||||
array( 'MAX( eeb_event_id )' ),
|
||||
array( 'eeb_user_id' => $this->mUser->getId() ),
|
||||
__METHOD__
|
||||
);
|
||||
|
||||
if ( $res ) {
|
||||
$this->lastEvent = $res;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the user's last batch timestamp after a successful batch
|
||||
*/
|
||||
protected function updateUserLastBatchTimestamp() {
|
||||
$this->mUser->setOption( 'echo-email-last-batch', wfTimestampNow() );
|
||||
$this->mUser->saveSettings();
|
||||
$this->mUser->invalidateCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the events queued for the current user
|
||||
* @return array
|
||||
*/
|
||||
protected function getEvents() {
|
||||
$events = array();
|
||||
|
||||
$validEvents = EchoEvent::gatherValidEchoEvents();
|
||||
|
||||
if ( $validEvents ) {
|
||||
$dbr = wfGetDB( DB_SLAVE );
|
||||
|
||||
$conds = array(
|
||||
'eeb_user_id' => $this->mUser->getId(),
|
||||
'event_id = eeb_event_id',
|
||||
'event_type' => $validEvents
|
||||
);
|
||||
|
||||
if ( $this->lastEvent ) {
|
||||
$conds[] = 'eeb_event_id <= ' . intval( $this->lastEvent );
|
||||
}
|
||||
|
||||
$res = $dbr->select(
|
||||
array( 'echo_email_batch', 'echo_event' ),
|
||||
array( '*' ),
|
||||
$conds,
|
||||
__METHOD__,
|
||||
array( 'ORDER BY' => 'eeb_event_priority, eeb_event_id', 'LIMIT' => self::$displaySize + 1 )
|
||||
);
|
||||
|
||||
foreach( $res as $row ) {
|
||||
$events[$row->eeb_id] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add individual event template to the big email content
|
||||
*/
|
||||
protected function appendContent( $event ) {
|
||||
global $wgEchoEventDetails;
|
||||
|
||||
// get the category for this event
|
||||
$category = 'other';
|
||||
if ( isset( $wgEchoEventDetails[$event->getType()]['category'] ) ) {
|
||||
$category = $wgEchoEventDetails[$event->getType()]['category'];
|
||||
}
|
||||
|
||||
$email = EchoNotificationController::formatNotification( $event, $this->mUser, 'email' );
|
||||
if ( !isset( $this->content[$category] ) ) {
|
||||
$this->content[$category] = array();
|
||||
}
|
||||
$this->content[$category][] = $email['batch-body'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate the list of contents with 'echo-email-batch-separator'
|
||||
* grouped by category
|
||||
* @return string
|
||||
*/
|
||||
protected function listToText() {
|
||||
if ( !$this->content ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$result = array();
|
||||
// build the text section for each category
|
||||
foreach( $this->content as $category => $notifs ) {
|
||||
$output = wfMessage( 'echo-email-batch-category-header-' . $category )->params( count( $notifs ) )->text() . "\n";
|
||||
foreach( $notifs as $notif ) {
|
||||
$output .= "\n " . wfMessage( 'echo-email-batch-bullet' )->text() . ' ' . $notif;
|
||||
}
|
||||
$result[] = $output;
|
||||
}
|
||||
|
||||
// for prepending and appending 'echo-email-batch-separator'
|
||||
$result = array_merge( array( '' ), $result, array( '' ) );
|
||||
|
||||
return trim(
|
||||
implode(
|
||||
"\n\n" . wfMessage( 'echo-email-batch-separator' )->text() . "\n\n",
|
||||
$result
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear "processed" events in the queue, processed could be: email sent, invalid, users do not want to receive emails
|
||||
*/
|
||||
public function clearProcessedEvent() {
|
||||
$conds = array( 'eeb_user_id' => $this->mUser->getId() );
|
||||
|
||||
// there is a processed cutoff point
|
||||
if ( $this->lastEvent ) {
|
||||
$conds[] = 'eeb_event_id <= ' . intval( $this->lastEvent );
|
||||
}
|
||||
|
||||
$dbw = wfGetDB( DB_MASTER );
|
||||
$dbw->delete(
|
||||
'echo_email_batch',
|
||||
$conds,
|
||||
__METHOD__,
|
||||
array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the batch email
|
||||
*/
|
||||
public function sendEmail() {
|
||||
global $wgPasswordSender, $wgPasswordSenderName, $wgEchoEmailFooterAddress;
|
||||
|
||||
// global email footer
|
||||
$footer = wfMessage( 'echo-email-footer-default' )
|
||||
->inLanguage( $this->mUser->getOption( 'language' ) )
|
||||
->params( $wgEchoEmailFooterAddress )
|
||||
->text();
|
||||
|
||||
// @Todo - replace them with the CONSTANT in 33810 once it is merged
|
||||
if ( $this->mUser->getOption( 'echo-email-frequency' ) == 7 ) {
|
||||
$frequency = 'weekly';
|
||||
} else {
|
||||
$frequency = 'daily';
|
||||
}
|
||||
|
||||
// email subject
|
||||
if ( $this->count > self::$displaySize ) {
|
||||
$count = wfMessage( 'echo-notification-count' )->params( self::$displaySize )->text();
|
||||
} else {
|
||||
$count = $this->count;
|
||||
}
|
||||
$subject = wfMessage( 'echo-email-batch-subject-' . $frequency )->params( $count, $this->count )->text();
|
||||
$body = wfMessage( 'echo-email-batch-body-' . $frequency )->params(
|
||||
$this->mUser->getName(),
|
||||
$count,
|
||||
$this->count,
|
||||
$this->listToText(),
|
||||
$footer
|
||||
)->text();
|
||||
|
||||
$adminAddress = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
|
||||
$address = new MailAddress( $this->mUser );
|
||||
|
||||
$params = array(
|
||||
'to' => $address,
|
||||
'from' => $adminAddress,
|
||||
'subj' => $subject,
|
||||
'body' => $body,
|
||||
// no replyto
|
||||
'replyto' => ''
|
||||
);
|
||||
$job = new EmaillingJob( null, $params );
|
||||
JobQueueGroup::singleton()->push( $job );
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert notification event into email queue
|
||||
* @param $userId int
|
||||
* @param $eventId int
|
||||
* @param $priority int
|
||||
*/
|
||||
public static function addToQueue( $userId, $eventId, $priority ) {
|
||||
if ( !$userId || !$eventId ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$dbw = wfGetDB( DB_MASTER );
|
||||
$dbw->insert(
|
||||
'echo_email_batch',
|
||||
array(
|
||||
'eeb_user_id' => $userId,
|
||||
'eeb_event_id' => $eventId,
|
||||
'eeb_event_priority' => $priority
|
||||
),
|
||||
__METHOD__,
|
||||
array( 'IGNORE' )
|
||||
);
|
||||
}
|
||||
}
|
|
@ -101,13 +101,20 @@ class EchoEvent {
|
|||
* Get a list of enabled Echo events, allow extensions to create their own events
|
||||
*/
|
||||
public static function gatherValidEchoEvents() {
|
||||
global $wgEchoEnabledEvents;
|
||||
global $wgEchoEnabledEvents, $wgEchoEventDetails;
|
||||
|
||||
static $runHook = true;
|
||||
// this hook should only be executed once to gather valid echo events
|
||||
if ( $runHook ) {
|
||||
// allow extensions to define their own event
|
||||
wfRunHooks( 'BeforeCreateEchoEvent', array( &$wgEchoEnabledEvents ) );
|
||||
wfRunHooks( 'BeforeCreateEchoEvent', array( &$wgEchoEnabledEvents, &$wgEchoEventDetails ) );
|
||||
|
||||
foreach ( $wgEchoEventDetails as $event => $detail ) {
|
||||
if ( isset( $detail['priority'] ) && $detail['priority'] < 1 && $detail['priority'] > 10 ) {
|
||||
throw new MWException( "$event: Valid event priority is ranging from 1 to 10" );
|
||||
}
|
||||
}
|
||||
|
||||
$runHook = false;
|
||||
}
|
||||
|
||||
|
|
78
processEchoEmailBatch.php
Normal file
78
processEchoEmailBatch.php
Normal file
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
$IP = getenv( 'MW_INSTALL_PATH' );
|
||||
if ( $IP === false ) {
|
||||
$IP = dirname( __FILE__ ) . '/../..';
|
||||
}
|
||||
require_once( "$IP/maintenance/Maintenance.php" );
|
||||
|
||||
/**
|
||||
* A maintenance script that processes email digest
|
||||
*/
|
||||
class processEchoEmailBatch extends Maintenance {
|
||||
|
||||
/**
|
||||
* Max number of records to process at a time
|
||||
* @var int
|
||||
*/
|
||||
protected $batchSize = 300;
|
||||
|
||||
/**
|
||||
* @var DatabaseBase
|
||||
*/
|
||||
protected $dbr;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->mDescription = "Process email digest";
|
||||
}
|
||||
|
||||
protected function init() {
|
||||
$this->dbr = wfGetDB( DB_SLAVE );
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
$this->init();
|
||||
$this->output( "Started processing... \n" );
|
||||
|
||||
$startUserId = 0;
|
||||
$count = $this->batchSize;
|
||||
|
||||
while ( $count === $this->batchSize ) {
|
||||
$count = 0;
|
||||
$res = $this->dbr->select(
|
||||
array( 'echo_email_batch' ),
|
||||
array( 'eeb_user_id' ),
|
||||
array( 'eeb_user_id > ' . $startUserId ),
|
||||
__METHOD__,
|
||||
array( 'ORDER BY' => 'eeb_user_id', 'LIMIT' => $this->batchSize )
|
||||
);
|
||||
|
||||
$updated = false;
|
||||
foreach ( $res as $row ) {
|
||||
$userId = intval( $row->eeb_user_id );
|
||||
if ( $userId && $userId > $startUserId ) {
|
||||
$emailBatch = MWEchoEmailBatch::newFromUserId( $userId );
|
||||
if ( $emailBatch ) {
|
||||
$this->output( "processing user_Id " . $userId . " \n" );
|
||||
$emailBatch->process();
|
||||
}
|
||||
$startUserId = $userId;
|
||||
$updated = true;
|
||||
}
|
||||
$count++;
|
||||
}
|
||||
wfWaitForSlaves();
|
||||
|
||||
// double check to make sure that the id is updated
|
||||
if ( !$updated ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->output( "Completed \n" );
|
||||
}
|
||||
}
|
||||
|
||||
$maintClass = "processEchoEmailBatch";
|
||||
require_once( DO_MAINTENANCE );
|
Loading…
Reference in a new issue