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-02-16 02:20:34 +00:00
|
|
|
global $wgEchoConfig, $wgEchoNotifications;
|
2013-03-01 00:26:59 +00:00
|
|
|
|
2013-02-16 02:20:34 +00:00
|
|
|
EchoNotification::create( array( 'user' => $user, 'event' => $event ) );
|
2013-03-01 00:26:59 +00:00
|
|
|
|
|
|
|
// Attempt event logging if Echo schema is enabled
|
|
|
|
if ( $wgEchoConfig['eventlogging']['Echo']['enabled'] ) {
|
2013-02-16 02:20:34 +00:00
|
|
|
$agent = $event->getAgent();
|
|
|
|
// Typically an event should always have an agent, but agent could be
|
|
|
|
// null if the data is corrupted
|
|
|
|
if ( $agent ) {
|
|
|
|
$sender = $agent->isAnon() ? $agent->getName() : $agent->getId();
|
|
|
|
} else {
|
|
|
|
$sender = 0;
|
|
|
|
}
|
2013-03-01 00:26:59 +00:00
|
|
|
|
2013-02-16 02:20:34 +00:00
|
|
|
if ( isset( $wgEchoNotifications[$event->getType()]['group'] ) ) {
|
|
|
|
$group = $wgEchoNotifications[$event->getType()]['group'];
|
2013-03-01 00:26:59 +00:00
|
|
|
} else {
|
|
|
|
$group = 'neutral';
|
|
|
|
}
|
|
|
|
$data = array (
|
|
|
|
'version' => $wgEchoConfig['version'],
|
|
|
|
'eventId' => $event->getId(),
|
|
|
|
'notificationType' => $event->getType(),
|
|
|
|
'notificationGroup' => $group,
|
2013-02-16 02:20:34 +00:00
|
|
|
'sender' => (string)$sender,
|
2013-03-01 00:26:59 +00:00
|
|
|
'recipientUserId' => $user->getId(),
|
|
|
|
'recipientEditCount' => (int)$user->getEditCount()
|
|
|
|
);
|
2013-03-20 20:55:21 +00:00
|
|
|
// Add the source if it exists. (This is mostly for the Thanks extension.)
|
|
|
|
$extra = $event->getExtra();
|
|
|
|
if ( isset( $extra['source'] ) ) {
|
|
|
|
$data['eventSource'] = (string)$extra['source'];
|
|
|
|
}
|
2013-03-01 00:26:59 +00:00
|
|
|
EchoHooks::logEvent( 'Echo', $data );
|
|
|
|
}
|
|
|
|
|
2012-12-19 02:16:52 +00:00
|
|
|
EchoNotificationController::resetNotificationCount( $user, DB_MASTER );
|
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 ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$user->isEmailConfirmed() ) {
|
2012-05-17 15:36:18 +00:00
|
|
|
// No valid email address
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-06 00:04:48 +00:00
|
|
|
|
|
|
|
// See if the user wants to receive emails for this category
|
2013-03-09 00:32:28 +00:00
|
|
|
if ( $user->getOption( 'echo-subscriptions-email-' . $event->getCategory() ) ) {
|
2013-03-06 00:04:48 +00:00
|
|
|
global $wgEchoEnableEmailBatch, $wgEchoNotifications, $wgPasswordSender, $wgPasswordSenderName, $wgEchoBundleEmailInterval;
|
|
|
|
|
|
|
|
$priority = EchoNotificationController::getNotificationPriority( $event->getType() );
|
|
|
|
|
|
|
|
$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'] ) ) {
|
|
|
|
wfRunHooks( 'EchoGetBundleRules', array( $event, &$bundleString ) );
|
|
|
|
}
|
|
|
|
if ( $bundleString ) {
|
|
|
|
$bundleHash = md5( $bundleString );
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2012-11-16 21:03:57 +00:00
|
|
|
// no email notification
|
|
|
|
if ( $user->getOption( 'echo-email-frequency' ) < 0 ) {
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
$adminAddress = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
|
|
|
|
$address = new MailAddress( $user );
|
|
|
|
// 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'];
|
|
|
|
|
|
|
|
UserMailer::send( $address, $adminAddress, $subject, $body );
|
|
|
|
}
|
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
|
|
|
}
|