1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
<?php
// @todo Fill in
class EchoNotifier {
/**
* Record an EchoNotification for an EchoEvent
* Currently used for web-based notifications.
*
* @param User $user User to notify.
* @param EchoEvent $event EchoEvent to notify about.
*/
public static function notifyWithNotification( $user, $event ) {
// Only create the notification if the user wants to receive that type
// of notification and they are eligible to receive it. See bug 47664.
$attributeManager = EchoAttributeManager::newFromGlobalVars();
$userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' );
if ( !in_array( $event->getType(), $userWebNotifications ) ) {
return;
}
EchoNotification::create( [ 'user' => $user, 'event' => $event ] );
MWEchoEventLogging::logSchemaEcho( $user, $event, 'web' );
}
/**
* Send a Notification to a user by email
*
* @param User $user User to notify.
* @param EchoEvent $event EchoEvent to notify about.
* @return bool
*/
public static function notifyWithEmail( $user, $event ) {
global $wgEnableEmail, $wgBlockDisablesLogin;
if (
// Email is globally disabled
!$wgEnableEmail ||
// User does not have a valid and confirmed email address
!$user->isEmailConfirmed() ||
// User has disabled Echo emails
$user->getOption( 'echo-email-frequency' ) < 0 ||
// User is blocked and cannot log in (T199993)
( $wgBlockDisablesLogin && $user->isBlocked() )
) {
return false;
}
// Final check on whether to send email for this user & event
if ( !Hooks::run( 'EchoAbortEmailNotification', [ $user, $event ] ) ) {
return false;
}
$attributeManager = EchoAttributeManager::newFromGlobalVars();
$userEmailNotifications = $attributeManager->getUserEnabledEvents( $user, 'email' );
// See if the user wants to receive emails for this category or the user is eligible to receive this email
if ( in_array( $event->getType(), $userEmailNotifications ) ) {
global $wgEchoEnableEmailBatch, $wgEchoNotifications, $wgPasswordSender, $wgNoReplyAddress;
$priority = $attributeManager->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'] )
) {
Hooks::run( 'EchoGetBundleRules', [ $event, &$bundleString ] );
}
if ( $bundleString ) {
$bundleHash = md5( $bundleString );
}
MWEchoEventLogging::logSchemaEcho( $user, $event, 'email' );
// email digest notification ( weekly or daily )
if ( $wgEchoEnableEmailBatch && $user->getOption( 'echo-email-frequency' ) > 0 ) {
// 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 );
return true;
}
// instant email notification
$toAddress = MailAddress::newFromUser( $user );
$fromAddress = new MailAddress( $wgPasswordSender, wfMessage( 'emailsender' )->inContentLanguage()->text() );
$replyAddress = new MailAddress( $wgNoReplyAddress );
// 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;
}
$subject = $email['subject'];
$body = $email['body'];
$options = [ 'replyTo' => $replyAddress ];
UserMailer::send( $toAddress, $fromAddress, $subject, $body, $options );
MWEchoEventLogging::logSchemaEchoMail( $user, 'single' );
}
return true;
}
/**
* @param EchoEvent $event
* @param User $user
* @return array|false An array of 'subject' and 'body', or false if things went wrong
*/
private static function generateEmail( EchoEvent $event, User $user ) {
$emailFormat = MWEchoNotifUser::newFromUser( $user )->getEmailFormat();
$lang = Language::factory( $user->getOption( 'language' ) );
$formatter = new EchoPlainTextEmailFormatter( $user, $lang );
$content = $formatter->format( $event );
if ( !$content ) {
return false;
}
if ( $emailFormat === EchoEmailFormat::HTML ) {
$htmlEmailFormatter = new EchoHtmlEmailFormatter( $user, $lang );
$htmlContent = $htmlEmailFormatter->format( $event );
$multipartBody = [
'text' => $content['body'],
'html' => $htmlContent['body']
];
$content['body'] = $multipartBody;
}
return $content;
}
}
|