2012-11-27 01:53:35 +00:00
|
|
|
<?php
|
|
|
|
|
2019-04-23 00:16:21 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2018-04-05 22:19:36 +00:00
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
|
|
|
|
2012-11-27 01:53:35 +00:00
|
|
|
/**
|
2013-02-13 19:50:25 +00:00
|
|
|
* Handle user email batch ( daily/ weekly )
|
2012-11-27 01:53:35 +00:00
|
|
|
*/
|
2015-06-08 18:51:49 +00:00
|
|
|
class MWEchoEmailBatch {
|
2012-11-27 01:53:35 +00:00
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
|
|
|
* @var User the user to be notified
|
|
|
|
*/
|
2012-11-27 01:53:35 +00:00
|
|
|
protected $mUser;
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
|
|
|
* @var Language
|
|
|
|
*/
|
|
|
|
protected $language;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EchoEvent[] events included in this email
|
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $events = [];
|
2015-10-29 21:14:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EchoEvent the last notification event of this batch
|
|
|
|
*/
|
2012-11-27 01:53:35 +00:00
|
|
|
protected $lastEvent;
|
2015-10-29 21:14:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int the event count, this count is supported up to self::$displaySize + 1
|
|
|
|
*/
|
2012-11-27 01:53:35 +00:00
|
|
|
protected $count = 0;
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
|
|
|
* @var int number of bundle events to include in an email,
|
|
|
|
* we cannot include 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
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param User $user
|
2012-11-27 01:53:35 +00:00
|
|
|
*/
|
|
|
|
public function __construct( User $user ) {
|
|
|
|
$this->mUser = $user;
|
2018-06-05 08:15:37 +00:00
|
|
|
$this->language = Language::factory( $this->mUser->getOption( 'language' ) );
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param int $userId
|
|
|
|
* @param bool $enforceFrequency Whether or not email sending frequency should
|
2015-06-01 19:22:59 +00:00
|
|
|
* be enforced.
|
|
|
|
*
|
|
|
|
* When true, today's notifications won't be returned if they are
|
|
|
|
* configured to go out tonight or at the end of the week.
|
|
|
|
*
|
|
|
|
* When false, all pending notifications will be returned.
|
2018-04-04 12:33:06 +00:00
|
|
|
* @return MWEchoEmailBatch|false
|
2012-11-27 01:53:35 +00:00
|
|
|
*/
|
2015-05-27 09:48:39 +00:00
|
|
|
public static function newFromUserId( $userId, $enforceFrequency = true ) {
|
2019-03-01 22:34:38 +00:00
|
|
|
$user = User::newFromId( (int)$userId );
|
2012-11-27 01:53:35 +00:00
|
|
|
|
2019-03-01 22:34:38 +00:00
|
|
|
$userEmailSetting = (int)$user->getOption( 'echo-email-frequency' );
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
// clear all existing events if user decides not to receive emails
|
|
|
|
if ( $userEmailSetting == -1 ) {
|
2015-06-08 18:51:49 +00:00
|
|
|
$emailBatch = new self( $user );
|
2012-11-27 01:53:35 +00:00
|
|
|
$emailBatch->clearProcessedEvent();
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2012-11-27 01:53:35 +00:00
|
|
|
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;
|
2015-05-27 09:48:39 +00:00
|
|
|
if ( $enforceFrequency && wfTimestamp( TS_MW, $nextBatch ) > wfTimestampNow() ) {
|
2012-11-27 01:53:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 18:51:49 +00:00
|
|
|
return new self( $user );
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper function that calls other functions required to process email batch
|
|
|
|
*/
|
|
|
|
public function process() {
|
|
|
|
// if there is no event for this user, exist the process
|
|
|
|
if ( !$this->setLastEvent() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get valid events
|
|
|
|
$events = $this->getEvents();
|
|
|
|
|
|
|
|
if ( $events ) {
|
2015-10-01 13:48:52 +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 );
|
2016-05-13 23:39:17 +00:00
|
|
|
if ( !$event ) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-29 21:14:35 +00:00
|
|
|
$event->setBundleHash( $row->eeb_event_hash );
|
|
|
|
$this->events[] = $event;
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
$bundler = new Bundler();
|
|
|
|
$this->events = $bundler->bundle( $this->events );
|
|
|
|
|
2012-11-27 01:53:35 +00:00
|
|
|
$this->sendEmail();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->clearProcessedEvent();
|
|
|
|
$this->updateUserLastBatchTimestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2015-06-08 18:51:49 +00:00
|
|
|
protected function setLastEvent() {
|
2019-04-17 12:12:56 +00:00
|
|
|
$dbr = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_REPLICA );
|
2015-06-08 18:51:49 +00:00
|
|
|
$res = $dbr->selectField(
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'echo_email_batch' ],
|
2019-03-01 20:40:04 +00:00
|
|
|
'MAX( eeb_event_id )',
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'eeb_user_id' => $this->mUser->getId() ],
|
2015-06-08 18:51:49 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $res ) {
|
|
|
|
$this->lastEvent = $res;
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2015-06-08 18:51:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-01 22:34:38 +00:00
|
|
|
|
|
|
|
return false;
|
2015-06-08 18:51:49 +00:00
|
|
|
}
|
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
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return \stdClass[]
|
2012-11-27 01:53:35 +00:00
|
|
|
*/
|
2015-06-08 18:51:49 +00:00
|
|
|
protected function getEvents() {
|
|
|
|
global $wgEchoNotifications;
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$events = [];
|
2015-06-08 18:51:49 +00:00
|
|
|
|
|
|
|
$validEvents = array_keys( $wgEchoNotifications );
|
|
|
|
|
|
|
|
// Per the tech discussion in the design meeting (03/22/2013), since this is
|
|
|
|
// processed by a cron job, it's okay to use GROUP BY over more complex
|
|
|
|
// composite index, favor insert performance, storage space over read
|
|
|
|
// performance in this case
|
|
|
|
if ( $validEvents ) {
|
2019-04-17 12:12:56 +00:00
|
|
|
$dbr = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_REPLICA );
|
2015-06-08 18:51:49 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$conds = [
|
2015-06-08 18:51:49 +00:00
|
|
|
'eeb_user_id' => $this->mUser->getId(),
|
|
|
|
'event_id = eeb_event_id',
|
|
|
|
'event_type' => $validEvents
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2015-06-08 18:51:49 +00:00
|
|
|
|
2019-12-10 00:13:29 +00:00
|
|
|
$tables = [ 'echo_email_batch', 'echo_event' ];
|
|
|
|
|
|
|
|
if ( $this->mUser->getOption( 'echo-dont-email-read-notifications' ) ) {
|
|
|
|
$conds += [
|
|
|
|
'notification_event = event_id',
|
|
|
|
'notification_read_timestamp' => null
|
|
|
|
];
|
|
|
|
array_push( $tables, 'echo_notification' );
|
|
|
|
}
|
|
|
|
|
2015-06-08 18:51:49 +00:00
|
|
|
// See setLastEvent() for more detail for this variable
|
|
|
|
if ( $this->lastEvent ) {
|
2019-03-01 22:34:38 +00:00
|
|
|
$conds[] = 'eeb_event_id <= ' . (int)$this->lastEvent;
|
2015-06-08 18:51:49 +00:00
|
|
|
}
|
2019-03-02 20:25:33 +00:00
|
|
|
$fields = array_merge( EchoEvent::selectFields(), [
|
|
|
|
'eeb_id',
|
|
|
|
'eeb_user_id',
|
|
|
|
'eeb_event_priority',
|
|
|
|
'eeb_event_id',
|
|
|
|
'eeb_event_hash',
|
|
|
|
] );
|
2015-06-08 18:51:49 +00:00
|
|
|
|
|
|
|
$res = $dbr->select(
|
2019-12-10 00:13:29 +00:00
|
|
|
$tables,
|
2019-03-02 20:25:33 +00:00
|
|
|
$fields,
|
2015-06-08 18:51:49 +00:00
|
|
|
$conds,
|
|
|
|
__METHOD__,
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2015-06-08 18:51:49 +00:00
|
|
|
'ORDER BY' => 'eeb_event_priority',
|
|
|
|
'LIMIT' => self::$displaySize + 1,
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
2015-06-08 18:51:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
// records in the queue inserted before email bundling code
|
|
|
|
// have no hash, in this case, we just ignore them
|
|
|
|
if ( $row->eeb_event_hash ) {
|
|
|
|
$events[$row->eeb_id] = $row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $events;
|
|
|
|
}
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear "processed" events in the queue, processed could be: email sent, invalid, users do not want to receive emails
|
|
|
|
*/
|
2015-06-08 18:51:49 +00:00
|
|
|
public function clearProcessedEvent() {
|
2019-04-23 00:16:21 +00:00
|
|
|
global $wgUpdateRowsPerQuery;
|
|
|
|
$eventMapper = new EchoEventMapper();
|
|
|
|
$dbFactory = MWEchoDbFactory::newFromDefault();
|
|
|
|
$dbw = $dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
$dbr = $dbFactory->getEchoDb( DB_REPLICA );
|
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
|
|
|
$ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
|
2019-06-11 21:43:05 +00:00
|
|
|
$domainId = $dbw->getDomainID();
|
2019-04-23 00:16:21 +00:00
|
|
|
|
|
|
|
$iterator = new BatchRowIterator( $dbr, 'echo_email_batch', 'eeb_event_id', $wgUpdateRowsPerQuery );
|
|
|
|
$iterator->addConditions( [ 'eeb_user_id' => $this->mUser->getId() ] );
|
2015-06-08 18:51:49 +00:00
|
|
|
if ( $this->lastEvent ) {
|
2019-04-23 00:16:21 +00:00
|
|
|
// There is a processed cutoff point
|
|
|
|
$iterator->addConditions( [ 'eeb_event_id <= ' . (int)$this->lastEvent ] );
|
2015-06-08 18:51:49 +00:00
|
|
|
}
|
2019-04-23 00:16:21 +00:00
|
|
|
foreach ( $iterator as $batch ) {
|
|
|
|
$eventIds = [];
|
|
|
|
foreach ( $batch as $row ) {
|
|
|
|
$eventIds[] = $row->eeb_event_id;
|
|
|
|
}
|
|
|
|
$dbw->delete( 'echo_email_batch', [
|
|
|
|
'eeb_user_id' => $this->mUser->getId(),
|
|
|
|
'eeb_event_id' => $eventIds
|
|
|
|
] );
|
2015-06-08 18:51:49 +00:00
|
|
|
|
2019-04-23 00:16:21 +00:00
|
|
|
// Find out which events are now orphaned, i.e. no longer referenced in echo_email_batch
|
|
|
|
// (besides the rows we just deleted) or in echo_notification, and delete them
|
|
|
|
$eventMapper->deleteOrphanedEvents( $eventIds, $this->mUser->getId(), 'echo_email_batch' );
|
|
|
|
|
|
|
|
$lbFactory->commitAndWaitForReplication(
|
|
|
|
__METHOD__, $ticket, [ 'domain' => $domainId ] );
|
|
|
|
}
|
2015-06-08 18:51:49 +00:00
|
|
|
}
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the batch email
|
|
|
|
*/
|
|
|
|
public function sendEmail() {
|
2018-08-20 20:50:25 +00:00
|
|
|
global $wgPasswordSender, $wgNoReplyAddress;
|
2012-11-27 01:53:35 +00:00
|
|
|
|
2016-05-05 13:05:03 +00:00
|
|
|
if ( $this->mUser->getOption( 'echo-email-frequency' ) == EchoEmailFrequency::WEEKLY_DIGEST ) {
|
2012-11-27 01:53:35 +00:00
|
|
|
$frequency = 'weekly';
|
2013-05-07 23:27:46 +00:00
|
|
|
$emailDeliveryMode = 'weekly_digest';
|
2012-11-27 01:53:35 +00:00
|
|
|
} else {
|
|
|
|
$frequency = 'daily';
|
2013-05-07 23:27:46 +00:00
|
|
|
$emailDeliveryMode = 'daily_digest';
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
$textEmailDigestFormatter = new EchoPlainTextDigestEmailFormatter( $this->mUser, $this->language, $frequency );
|
|
|
|
$content = $textEmailDigestFormatter->format( $this->events, 'email' );
|
2013-07-09 00:41:08 +00:00
|
|
|
|
2016-06-28 13:19:01 +00:00
|
|
|
if ( !$content ) {
|
|
|
|
// no event could be formatted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-09 00:41:08 +00:00
|
|
|
$format = MWEchoNotifUser::newFromUser( $this->mUser )->getEmailFormat();
|
2016-05-05 13:05:03 +00:00
|
|
|
if ( $format == EchoEmailFormat::HTML ) {
|
|
|
|
$htmlEmailDigestFormatter = new EchoHtmlDigestEmailFormatter( $this->mUser, $this->language, $frequency );
|
|
|
|
$htmlContent = $htmlEmailDigestFormatter->format( $this->events, 'email' );
|
2015-10-29 21:14:35 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$content = [
|
|
|
|
'body' => [
|
2016-05-05 13:05:03 +00:00
|
|
|
'text' => $content['body'],
|
|
|
|
'html' => $htmlContent['body'],
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-05-05 13:05:03 +00:00
|
|
|
'subject' => $htmlContent['subject'],
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2013-07-09 00:41:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 01:25:14 +00:00
|
|
|
$toAddress = MailAddress::newFromUser( $this->mUser );
|
2018-08-20 20:44:33 +00:00
|
|
|
$fromAddress = new MailAddress( $wgPasswordSender, wfMessage( 'emailsender' )->inContentLanguage()->text() );
|
2018-08-20 20:50:25 +00:00
|
|
|
$replyTo = new MailAddress( $wgNoReplyAddress );
|
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?
|
2016-12-05 18:51:07 +00:00
|
|
|
UserMailer::send( $toAddress, $fromAddress, $content['subject'], $content['body'], [ 'replyTo' => $replyTo ] );
|
2013-05-07 23:27:46 +00:00
|
|
|
MWEchoEventLogging::logSchemaEchoMail( $this->mUser, $emailDeliveryMode );
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert notification event into email queue
|
2014-05-27 18:28:37 +00:00
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param int $userId
|
|
|
|
* @param int $eventId
|
|
|
|
* @param int $priority
|
|
|
|
* @param string $hash
|
2012-11-27 01:53:35 +00:00
|
|
|
*/
|
2013-03-06 00:04:48 +00:00
|
|
|
public static function addToQueue( $userId, $eventId, $priority, $hash ) {
|
2015-06-08 18:51:49 +00:00
|
|
|
if ( !$userId || !$eventId ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:12:56 +00:00
|
|
|
$dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_MASTER );
|
2015-06-08 18:51:49 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$row = [
|
2015-06-08 18:51:49 +00:00
|
|
|
'eeb_user_id' => $userId,
|
|
|
|
'eeb_event_id' => $eventId,
|
|
|
|
'eeb_event_priority' => $priority,
|
|
|
|
'eeb_event_hash' => $hash
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2015-06-08 18:51:49 +00:00
|
|
|
|
|
|
|
$dbw->insert(
|
|
|
|
'echo_email_batch',
|
|
|
|
$row,
|
|
|
|
__METHOD__,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'IGNORE' ]
|
2015-06-08 18:51:49 +00:00
|
|
|
);
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of users to be notified for the batch
|
2014-05-27 18:28:37 +00:00
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param int $startUserId
|
|
|
|
* @param int $batchSize
|
2014-05-27 18:28:37 +00:00
|
|
|
*
|
2018-04-05 22:19:36 +00:00
|
|
|
* @return IResultWrapper|bool
|
2013-01-15 23:21:39 +00:00
|
|
|
*/
|
|
|
|
public static function getUsersToNotify( $startUserId, $batchSize ) {
|
2019-04-17 12:12:56 +00:00
|
|
|
$dbr = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_REPLICA );
|
2015-06-08 18:51:49 +00:00
|
|
|
$res = $dbr->select(
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'echo_email_batch' ],
|
|
|
|
[ 'eeb_user_id' ],
|
2019-03-01 22:34:38 +00:00
|
|
|
[ 'eeb_user_id > ' . (int)$startUserId ],
|
2015-06-08 18:51:49 +00:00
|
|
|
__METHOD__,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'ORDER BY' => 'eeb_user_id', 'LIMIT' => $batchSize ]
|
2015-06-08 18:51:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $res;
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
}
|