2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
|
|
|
use MailAddress;
|
2022-11-01 22:01:23 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Formatters\EchoHtmlEmailFormatter;
|
|
|
|
use MediaWiki\Extension\Notifications\Formatters\EchoPlainTextEmailFormatter;
|
2023-06-06 21:02:02 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Hooks\HookRunner;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
|
|
|
use MediaWiki\Extension\Notifications\Model\Notification;
|
2021-12-01 18:29:00 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-11-12 07:19:00 +00:00
|
|
|
use UserMailer;
|
2021-12-01 18:29:00 +00:00
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
class Notifier {
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* Record a Notification for an Event
|
2013-01-14 23:52:46 +00:00
|
|
|
* Currently used for web-based notifications.
|
2012-04-27 15:14:24 +00:00
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param User $user User to notify.
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event Event to notify about.
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
public static function notifyWithNotification( $user, $event ) {
|
2016-12-28 11:29:44 +00:00
|
|
|
// Only create the notification if the user wants to receive that type
|
2022-11-12 07:19:00 +00:00
|
|
|
// of notification, and they are eligible to receive it. See bug 47664.
|
2022-11-13 07:53:42 +00:00
|
|
|
$attributeManager = Services::getInstance()->getAttributeManager();
|
2014-07-22 21:33:22 +00:00
|
|
|
$userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' );
|
2013-04-28 18:07:28 +00:00
|
|
|
if ( !in_array( $event->getType(), $userWebNotifications ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
Notification::create( [ 'user' => $user, 'event' => $event ] );
|
2013-04-24 20:45:05 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* Send a Notification to a user by email
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param User $user User to notify.
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event Event to notify about.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
public static function notifyWithEmail( $user, $event ) {
|
2023-08-15 20:21:38 +00:00
|
|
|
global $wgEnableEmail, $wgBlockDisablesLogin, $wgEchoWatchlistEmailOncePerPage, $wgEnotifMinorEdits;
|
2023-04-27 21:12:22 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$userOptionsLookup = $services->getUserOptionsLookup();
|
2015-04-18 13:43:57 +00:00
|
|
|
|
2018-08-22 22:09:30 +00:00
|
|
|
if (
|
|
|
|
// Email is globally disabled
|
|
|
|
!$wgEnableEmail ||
|
|
|
|
// User does not have a valid and confirmed email address
|
|
|
|
!$user->isEmailConfirmed() ||
|
|
|
|
// User has disabled Echo emails
|
2021-12-01 18:29:00 +00:00
|
|
|
$userOptionsLookup->getOption( $user, 'echo-email-frequency' ) < 0 ||
|
2018-08-22 22:09:30 +00:00
|
|
|
// User is blocked and cannot log in (T199993)
|
2021-03-03 19:39:10 +00:00
|
|
|
( $wgBlockDisablesLogin && $user->getBlock() )
|
2018-08-22 22:09:30 +00:00
|
|
|
) {
|
2012-05-17 15:36:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-06 00:04:48 +00:00
|
|
|
|
2023-08-15 20:21:38 +00:00
|
|
|
$type = $event->getType();
|
|
|
|
if ( $type === 'edit-user-talk' ) {
|
|
|
|
$extra = $event->getExtra();
|
|
|
|
if ( !empty( $extra['minoredit'] ) ) {
|
|
|
|
if ( !$wgEnotifMinorEdits || !$userOptionsLookup->getOption( $user, 'enotifminoredits' ) ) {
|
|
|
|
// Do not send talk page notification email
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Mimic core code of only sending watchlist notification emails once per page
|
|
|
|
} elseif ( $type === "watchlist-change" || $type === "minor-watchlist-change" ) {
|
|
|
|
// Don't care about rate limiting
|
|
|
|
if ( $wgEchoWatchlistEmailOncePerPage ) {
|
|
|
|
$store = $services->getWatchedItemStore();
|
|
|
|
$ts = $store->getWatchedItem( $user, $event->getTitle() )->getNotificationTimestamp();
|
|
|
|
// if (ts != null) is not sufficient because, if $wgEchoUseJobQueue is set,
|
|
|
|
// wl_notificationtimestamp will have already been set for the new edit
|
|
|
|
// by the time this code runs.
|
|
|
|
if ( $ts !== null && $ts !== $event->getExtraParam( "timestamp" ) ) {
|
|
|
|
// User has already seen an email for this page before
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 20:18:51 +00:00
|
|
|
} elseif ( $event->getExtraParam( 'noemail' ) ) {
|
2024-09-22 09:42:51 +00:00
|
|
|
// Could be set for API triggered notifications where email is not
|
2023-06-07 20:18:51 +00:00
|
|
|
// requested in API request params
|
|
|
|
return false;
|
2023-08-15 20:21:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 21:02:02 +00:00
|
|
|
$hookRunner = new HookRunner( $services->getHookContainer() );
|
2013-05-01 19:27:32 +00:00
|
|
|
// Final check on whether to send email for this user & event
|
2023-06-06 21:02:02 +00:00
|
|
|
if ( !$hookRunner->onEchoAbortEmailNotification( $user, $event ) ) {
|
2013-05-01 19:27:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-13 07:53:42 +00:00
|
|
|
$attributeManager = Services::getInstance()->getAttributeManager();
|
2014-07-22 21:33:22 +00:00
|
|
|
$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 ) ) {
|
2018-08-20 20:50:25 +00:00
|
|
|
global $wgEchoEnableEmailBatch, $wgEchoNotifications, $wgPasswordSender, $wgNoReplyAddress;
|
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 = '';
|
|
|
|
|
2018-08-25 10:51:14 +00:00
|
|
|
// 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'] )
|
|
|
|
) {
|
2023-08-15 20:21:38 +00:00
|
|
|
self::getBundleRules( $event, $bundleString );
|
2013-03-06 00:04:48 +00:00
|
|
|
}
|
|
|
|
if ( $bundleString ) {
|
|
|
|
$bundleHash = md5( $bundleString );
|
|
|
|
}
|
|
|
|
|
2024-09-22 09:42:51 +00:00
|
|
|
// email digest notification (weekly or daily)
|
2021-12-01 18:29:00 +00:00
|
|
|
if ( $wgEchoEnableEmailBatch && $userOptionsLookup->getOption( $user, '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
|
2024-09-22 09:42:51 +00:00
|
|
|
$eventId = $event->acquireId();
|
2013-03-06 00:04:48 +00:00
|
|
|
if ( !$bundleHash ) {
|
2024-09-22 09:42:51 +00:00
|
|
|
$bundleHash = md5( $event->getType() . '-' . $eventId );
|
2013-03-06 00:04:48 +00:00
|
|
|
}
|
2024-09-22 09:42:51 +00:00
|
|
|
EmailBatch::addToQueue( $user->getId(), $eventId, $priority, $bundleHash );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
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
|
|
|
|
2016-05-17 19:58:06 +00:00
|
|
|
// instant email notification
|
|
|
|
$toAddress = MailAddress::newFromUser( $user );
|
2020-06-27 10:05:03 +00:00
|
|
|
$fromAddress = new MailAddress(
|
|
|
|
$wgPasswordSender,
|
|
|
|
wfMessage( 'emailsender' )->inContentLanguage()->text()
|
|
|
|
);
|
2018-08-20 20:50:25 +00:00
|
|
|
$replyAddress = new MailAddress( $wgNoReplyAddress );
|
2016-05-17 19:58:06 +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 = self::generateEmail( $event, $user );
|
|
|
|
if ( !$email ) {
|
|
|
|
return false;
|
2013-03-06 00:04:48 +00:00
|
|
|
}
|
2016-05-17 19:58:06 +00:00
|
|
|
$subject = $email['subject'];
|
|
|
|
$body = $email['body'];
|
2016-12-05 18:51:07 +00:00
|
|
|
$options = [ 'replyTo' => $replyAddress ];
|
2013-03-06 00:04:48 +00:00
|
|
|
|
2016-05-17 19:58:06 +00:00
|
|
|
UserMailer::send( $toAddress, $fromAddress, $subject, $body, $options );
|
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
|
|
|
}
|
2015-10-29 21:14:35 +00:00
|
|
|
|
2023-08-15 20:21:38 +00:00
|
|
|
/**
|
|
|
|
* Handler to get bundle rules, handles echo's own events and calls the EchoGetBundleRule hook,
|
|
|
|
* which defines the bundle rule for the extensions notification.
|
|
|
|
*
|
|
|
|
* @param Event $event
|
|
|
|
* @param string &$bundleString Determines how the notification should be bundled, for example,
|
|
|
|
* talk page notification is bundled based on namespace and title, the bundle string would be
|
|
|
|
* 'edit-user-talk-' + namespace + title, email digest/email bundling would use this hash as
|
|
|
|
* a key to identify bundle-able event. For web bundling, we bundle further based on user's
|
|
|
|
* visit to the overlay, we would generate a display hash based on the hash of $bundleString
|
|
|
|
*/
|
|
|
|
public static function getBundleRules( $event, &$bundleString ) {
|
|
|
|
switch ( $event->getType() ) {
|
|
|
|
case 'edit-user-page':
|
|
|
|
case 'edit-user-talk':
|
|
|
|
case 'page-linked':
|
|
|
|
$bundleString = $event->getType();
|
|
|
|
if ( $event->getTitle() ) {
|
|
|
|
$bundleString .= '-' . $event->getTitle()->getNamespace()
|
|
|
|
. '-' . $event->getTitle()->getDBkey();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'mention-success':
|
|
|
|
case 'mention-failure':
|
|
|
|
$bundleString = 'mention-status-' . $event->getExtraParam( 'revid' );
|
|
|
|
break;
|
|
|
|
case 'watchlist-change':
|
|
|
|
case 'minor-watchlist-change':
|
|
|
|
$bundleString = 'watchlist-change';
|
|
|
|
if ( $event->getTitle() ) {
|
|
|
|
$bundleString .= '-' . $event->getTitle()->getNamespace()
|
|
|
|
. '-' . $event->getTitle()->getDBkey();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
|
|
|
|
$hookRunner->onEchoGetBundleRules( $event, $bundleString );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2015-10-29 21:14:35 +00:00
|
|
|
* @param User $user
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return array|false An array of 'subject' and 'body', or false if things went wrong
|
2015-10-29 21:14:35 +00:00
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
private static function generateEmail( Event $event, User $user ) {
|
2022-11-13 07:48:43 +00:00
|
|
|
$emailFormat = NotifUser::newFromUser( $user )->getEmailFormat();
|
2022-12-28 19:19:01 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$userOptionsLookup = $services->getUserOptionsLookup();
|
|
|
|
$lang = $services->getLanguageFactory()
|
|
|
|
->getLanguage( $userOptionsLookup->getOption( $user, 'language' ) );
|
2016-05-05 13:05:03 +00:00
|
|
|
$formatter = new EchoPlainTextEmailFormatter( $user, $lang );
|
2020-06-01 02:17:13 +00:00
|
|
|
$content = $formatter->format( $event, 'email' );
|
2016-06-28 13:19:01 +00:00
|
|
|
if ( !$content ) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-05 13:05:03 +00:00
|
|
|
|
2022-11-13 06:43:40 +00:00
|
|
|
if ( $emailFormat === EmailFormat::HTML ) {
|
2016-05-05 13:05:03 +00:00
|
|
|
$htmlEmailFormatter = new EchoHtmlEmailFormatter( $user, $lang );
|
2020-06-01 02:17:13 +00:00
|
|
|
$htmlContent = $htmlEmailFormatter->format( $event, 'email' );
|
2016-12-05 18:51:07 +00:00
|
|
|
$multipartBody = [
|
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
|
|
|
$content['body'] = $multipartBody;
|
2015-10-29 21:14:35 +00:00
|
|
|
}
|
2016-05-05 13:05:03 +00:00
|
|
|
|
|
|
|
return $content;
|
2015-10-29 21:14:35 +00:00
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|
2022-11-12 07:19:00 +00:00
|
|
|
|
|
|
|
class_alias( Notifier::class, 'EchoNotifier' );
|