2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// @todo Fill in
|
|
|
|
class EchoNotifier {
|
|
|
|
/**
|
2013-01-14 23:52:46 +00:00
|
|
|
* Record an EchoNotification for an EchoEvent
|
|
|
|
* Currently used for web-based notifications.
|
2012-04-27 15:14:24 +00:00
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $user User to notify.
|
|
|
|
* @param $event EchoEvent to notify about.
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
public static function notifyWithNotification( $user, $event ) {
|
2013-04-28 18:07:28 +00:00
|
|
|
// Only create the notification if the user wants to recieve that type
|
|
|
|
// of notification and they are eligible to recieve it. See bug 47664.
|
2014-07-22 21:33:22 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' );
|
2013-04-28 18:07:28 +00:00
|
|
|
if ( !in_array( $event->getType(), $userWebNotifications ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-16 02:20:34 +00:00
|
|
|
EchoNotification::create( array( 'user' => $user, 'event' => $event ) );
|
2013-03-01 00:26:59 +00:00
|
|
|
|
2013-05-07 23:27:46 +00:00
|
|
|
MWEchoEventLogging::logSchemaEcho( $user, $event, 'web' );
|
2013-04-24 20:45:05 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* Send a Notification to a user by email
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $user User to notify.
|
|
|
|
* @param $event EchoEvent to notify about.
|
|
|
|
* @return bool
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
public static function notifyWithEmail( $user, $event ) {
|
2015-04-18 13:43:57 +00:00
|
|
|
global $wgEnableEmail;
|
|
|
|
|
|
|
|
if ( !$wgEnableEmail ) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-26 20:13:06 +00:00
|
|
|
// No valid email address or email notification
|
|
|
|
if ( !$user->isEmailConfirmed() || $user->getOption( 'echo-email-frequency' ) < 0 ) {
|
2012-05-17 15:36:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-06 00:04:48 +00:00
|
|
|
|
2013-05-01 19:27:32 +00:00
|
|
|
// Final check on whether to send email for this user & event
|
2015-06-01 18:36:35 +00:00
|
|
|
if ( !Hooks::run( 'EchoAbortEmailNotification', array( $user, $event ) ) ) {
|
2013-05-01 19:27:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$userEmailNotifications = $attributeManager->getUserEnabledEvents( $user, 'email' );
|
2013-04-30 20:22:15 +00:00
|
|
|
// See if the user wants to receive emails for this category or the user is eligible to receive this email
|
2014-07-22 21:33:22 +00:00
|
|
|
if ( in_array( $event->getType(), $userEmailNotifications ) ) {
|
2014-08-08 20:35:58 +00:00
|
|
|
global $wgEchoEnableEmailBatch, $wgEchoNotifications, $wgNotificationSender, $wgNotificationReplyName, $wgEchoBundleEmailInterval;
|
2013-03-06 00:04:48 +00:00
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
$priority = $attributeManager->getNotificationPriority( $event->getType() );
|
2013-03-06 00:04:48 +00:00
|
|
|
|
|
|
|
$bundleString = $bundleHash = '';
|
|
|
|
|
|
|
|
// We should have bundling for email digest as long as either web or email bundling is on, for example, talk page
|
|
|
|
// email bundling is off, but if a user decides to receive email digest, we should bundle those messages
|
|
|
|
if ( !empty( $wgEchoNotifications[$event->getType()]['bundle']['web'] ) || !empty( $wgEchoNotifications[$event->getType()]['bundle']['email'] ) ) {
|
2015-06-01 18:36:35 +00:00
|
|
|
Hooks::run( 'EchoGetBundleRules', array( $event, &$bundleString ) );
|
2013-03-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
if ( $bundleString ) {
|
|
|
|
$bundleHash = md5( $bundleString );
|
|
|
|
}
|
|
|
|
|
2013-05-07 23:27:46 +00:00
|
|
|
MWEchoEventLogging::logSchemaEcho( $user, $event, 'email' );
|
2013-04-26 20:13:06 +00:00
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
// email digest notification ( weekly or daily )
|
2012-11-16 21:03:57 +00:00
|
|
|
if ( $wgEchoEnableEmailBatch && $user->getOption( 'echo-email-frequency' ) > 0 ) {
|
2013-03-06 00:04:48 +00:00
|
|
|
// always create a unique event hash for those events don't support bundling
|
|
|
|
// this is mainly for group by
|
|
|
|
if ( !$bundleHash ) {
|
|
|
|
$bundleHash = md5( $event->getType() . '-' . $event->getId() );
|
|
|
|
}
|
|
|
|
MWEchoEmailBatch::addToQueue( $user->getId(), $event->getId(), $priority, $bundleHash );
|
2012-11-16 21:03:57 +00:00
|
|
|
return true;
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
2013-03-06 00:04:48 +00:00
|
|
|
|
|
|
|
$addedToQueue = false;
|
|
|
|
|
|
|
|
// only send bundle email if email bundling is on
|
|
|
|
if ( $wgEchoBundleEmailInterval && $bundleHash && !empty( $wgEchoNotifications[$event->getType()]['bundle']['email'] ) ) {
|
|
|
|
$bundler = MWEchoEmailBundler::newFromUserHash( $user, $bundleHash );
|
|
|
|
if ( $bundler ) {
|
|
|
|
$addedToQueue = $bundler->addToEmailBatch( $event->getId(), $priority );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send single notification if the email wasn't added to queue for bundling
|
|
|
|
if ( !$addedToQueue ) {
|
|
|
|
// instant email notification
|
2014-09-22 01:25:14 +00:00
|
|
|
$toAddress = MailAddress::newFromUser( $user );
|
2014-08-08 20:35:58 +00:00
|
|
|
$fromAddress = new MailAddress( $wgNotificationSender, EchoHooks::getNotificationSenderName() );
|
2013-04-17 16:26:05 +00:00
|
|
|
$replyAddress = new MailAddress( $wgNotificationSender, $wgNotificationReplyName );
|
2013-03-06 00:04:48 +00:00
|
|
|
// Since we are sending a single email, should set the bundle hash to null
|
|
|
|
// if it is set with a value from somewhere else
|
|
|
|
$event->setBundleHash( null );
|
|
|
|
$email = EchoNotificationController::formatNotification( $event, $user, 'email', 'email' );
|
|
|
|
$subject = $email['subject'];
|
|
|
|
$body = $email['body'];
|
|
|
|
|
2013-04-17 16:26:05 +00:00
|
|
|
UserMailer::send( $toAddress, $fromAddress, $subject, $body, $replyAddress );
|
2013-05-07 23:27:46 +00:00
|
|
|
MWEchoEventLogging::logSchemaEchoMail( $user, 'single' );
|
2013-03-06 00:04:48 +00:00
|
|
|
}
|
2012-11-27 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 09:30:38 +00:00
|
|
|
return true;
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|