2012-11-27 01:53:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2013-02-13 19:50:25 +00:00
|
|
|
* Handle user email batch ( daily/ weekly )
|
2012-11-27 01:53:35 +00:00
|
|
|
*/
|
2013-01-15 23:21:39 +00:00
|
|
|
abstract class MWEchoEmailBatch {
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
// number of bundle events to include in an email, we couldn't include
|
2012-11-27 01:53:35 +00:00
|
|
|
// all events in a batch email
|
2013-03-06 00:04:48 +00:00
|
|
|
protected static $displaySize = 20;
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 ) {
|
2013-03-06 22:43:26 +00:00
|
|
|
$batchClassName = self::getEmailBatchClass();
|
2013-01-15 23:21:39 +00:00
|
|
|
|
2012-11-27 01:53:35 +00:00
|
|
|
$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 ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
$emailBatch = new $batchClassName( $user );
|
2012-11-27 01:53:35 +00:00
|
|
|
$emailBatch->clearProcessedEvent();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
// @Todo - There may be some items idling in the queue, eg, a bundle job is lost
|
|
|
|
// and there is not never another message with the same hash or a user switches from
|
|
|
|
// digest to instant. We should check the first item in the queue, if it doesn't
|
|
|
|
// have either web or email bundling or created long ago, then clear it, this will
|
|
|
|
// prevent idling item queuing up.
|
|
|
|
|
|
|
|
// user has instant email delivery
|
|
|
|
if ( $userEmailSetting == 0 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-27 01:53:35 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
return new $batchClassName( $user );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the email batch class
|
|
|
|
* @return string
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
2013-03-06 22:43:26 +00:00
|
|
|
private static function getEmailBatchClass() {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoBackendName;
|
|
|
|
|
|
|
|
$className = 'MW' . $wgEchoBackendName . 'EchoEmailBatch';
|
|
|
|
|
|
|
|
if ( !class_exists( $className ) ) {
|
|
|
|
throw new MWException( "$wgEchoBackendName email batch is not supported!" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $className;
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2013-02-28 23:33:46 +00:00
|
|
|
foreach( $events as $row ) {
|
2012-11-27 01:53:35 +00:00
|
|
|
$this->count++;
|
|
|
|
if ( $this->count > self::$displaySize ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$event = EchoEvent::newFromRow( $row );
|
2013-03-06 00:04:48 +00:00
|
|
|
$this->appendContent( $event, $row->eeb_event_hash );
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
*/
|
2013-01-15 23:21:39 +00:00
|
|
|
abstract protected function setLastEvent();
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-01-15 23:21:39 +00:00
|
|
|
abstract protected function getEvents();
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add individual event template to the big email content
|
|
|
|
*/
|
2013-03-06 00:04:48 +00:00
|
|
|
protected function appendContent( $event, $hash ) {
|
2012-11-27 01:53:35 +00:00
|
|
|
// get the category for this event
|
2013-03-09 00:32:28 +00:00
|
|
|
$category = $event->getCategory();
|
2013-03-06 00:04:48 +00:00
|
|
|
$event->setBundleHash( $hash );
|
|
|
|
$email = EchoNotificationController::formatNotification( $event, $this->mUser, 'email', 'email' );
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
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 ) {
|
2013-04-16 22:40:45 +00:00
|
|
|
// Messages that can be used here:
|
|
|
|
// * echo-category-title-system
|
|
|
|
// * echo-category-title-other
|
|
|
|
// * echo-category-title-edit-user-talk
|
|
|
|
// * echo-category-title-reverted
|
|
|
|
// * echo-category-title-article-linked
|
|
|
|
// * echo-category-title-mention
|
|
|
|
$output = wfMessage( 'echo-category-title-' . $category )->numParams( count( $notifs ) )->text() . "\n";
|
|
|
|
|
2012-11-27 01:53:35 +00:00
|
|
|
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
|
|
|
|
*/
|
2013-01-15 23:21:39 +00:00
|
|
|
abstract public function clearProcessedEvent();
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the batch email
|
|
|
|
*/
|
|
|
|
public function sendEmail() {
|
2013-04-17 16:26:05 +00:00
|
|
|
global $wgNotificationSender, $wgNotificationSenderName, $wgNotificationReplyName, $wgEchoEmailFooterAddress;
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
// global email footer
|
|
|
|
$footer = wfMessage( 'echo-email-footer-default' )
|
|
|
|
->inLanguage( $this->mUser->getOption( 'language' ) )
|
2012-12-20 01:38:35 +00:00
|
|
|
->params( $wgEchoEmailFooterAddress, '' )
|
2012-11-27 01:53:35 +00:00
|
|
|
->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();
|
|
|
|
|
2013-04-17 16:26:05 +00:00
|
|
|
$toAddress = new MailAddress( $this->mUser );
|
|
|
|
$fromAddress = new MailAddress( $wgNotificationSender, $wgNotificationSenderName );
|
|
|
|
$replyAddress = new MailAddress( $wgNotificationSender, $wgNotificationReplyName );
|
2012-11-27 01:53:35 +00:00
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
// @Todo Push the email to job queue or just send it out directly?
|
2013-04-17 16:26:05 +00:00
|
|
|
UserMailer::send( $toAddress, $fromAddress, $subject, $body, $replyAddress );
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert notification event into email queue
|
|
|
|
* @param $userId int
|
|
|
|
* @param $eventId int
|
|
|
|
* @param $priority int
|
2013-03-06 00:04:48 +00:00
|
|
|
* @param $hash string
|
2012-11-27 01:53:35 +00:00
|
|
|
*/
|
2013-03-06 00:04:48 +00:00
|
|
|
public static function addToQueue( $userId, $eventId, $priority, $hash ) {
|
2013-03-06 22:43:26 +00:00
|
|
|
$batchClassName = self::getEmailBatchClass();
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
if ( !method_exists( $batchClassName, 'actuallyAddToQueue' ) ) {
|
|
|
|
throw new MWException( "$batchClassName must implement method actuallyAddToQueue()" );
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
$batchClassName::actuallyAddToQueue( $userId, $eventId, $priority, $hash );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of users to be notified for the batch
|
|
|
|
* @param $startUserId int
|
|
|
|
* @param $batchSize int
|
|
|
|
*/
|
|
|
|
public static function getUsersToNotify( $startUserId, $batchSize ) {
|
2013-03-06 22:43:26 +00:00
|
|
|
$batchClassName = self::getEmailBatchClass();
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
if ( !method_exists( $batchClassName, 'actuallyGetUsersToNotify' ) ) {
|
|
|
|
throw new MWException( "$batchClassName must implement method actuallyGetUsersToNotify()" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $batchClassName::actuallyGetUsersToNotify( $startUserId, $batchSize );
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
}
|