Debian Codegraph @

extensions/Echo/includes/Notifier.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications;
4
5 use MailAddress;
6 use MediaWiki\Extension\Notifications\Formatters\EchoHtmlEmailFormatter;
7 use MediaWiki\Extension\Notifications\Formatters\EchoPlainTextEmailFormatter;
8 use MediaWiki\Extension\Notifications\Hooks\HookRunner;
9 use MediaWiki\Extension\Notifications\Model\Event;
10 use MediaWiki\Extension\Notifications\Model\Notification;
11 use MediaWiki\MediaWikiServices;
12 use MediaWiki\User\User;
13 use UserMailer;
14
15 class Notifier {
16 /**
17 * Record a Notification for an Event
18 * Currently used for web-based notifications.
19 *
20 * @param User $user User to notify.
21 * @param Event $event Event to notify about.
22 */
23 public static function notifyWithNotification( $user, $event ) {
24 // Only create the notification if the user wants to receive that type
25 // of notification, and they are eligible to receive it. See bug 47664.
26 $attributeManager = Services::getInstance()->getAttributeManager();
27 $userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' );
28 if ( !in_array( $event->getType(), $userWebNotifications ) ) {
29 return;
30 }
31
32 Notification::create( [ 'user' => $user, 'event' => $event ] );
33 }
34
35 /**
36 * Send a Notification to a user by email
37 *
38 * @param User $user User to notify.
39 * @param Event $event Event to notify about.
40 * @return bool
41 */
42 public static function notifyWithEmail( $user, $event ) {
43 global $wgEnableEmail, $wgBlockDisablesLogin, $wgEchoWatchlistEmailOncePerPage, $wgEnotifMinorEdits;
44 $services = MediaWikiServices::getInstance();
45 $userOptionsLookup = $services->getUserOptionsLookup();
46
47 if (
48 // Email is globally disabled
49 !$wgEnableEmail ||
50 // User does not have a valid and confirmed email address
51 !$user->isEmailConfirmed() ||
52 // User has disabled Echo emails
53 $userOptionsLookup->getOption( $user, 'echo-email-frequency' ) < 0 ||
54 // User is blocked and cannot log in (T199993)
55 ( $wgBlockDisablesLogin && $user->getBlock() )
56 ) {
57 return false;
58 }
59
60 $type = $event->getType();
61 if ( $type === 'edit-user-talk' ) {
62 $extra = $event->getExtra();
63 if ( !empty( $extra['minoredit'] ) ) {
64 if ( !$wgEnotifMinorEdits || !$userOptionsLookup->getOption( $user, 'enotifminoredits' ) ) {
65 // Do not send talk page notification email
66 return false;
67 }
68 }
69 // Mimic core code of only sending watchlist notification emails once per page
70 } elseif ( $type === "watchlist-change" || $type === "minor-watchlist-change" ) {
71 // Don't care about rate limiting
72 if ( $wgEchoWatchlistEmailOncePerPage ) {
73 $store = $services->getWatchedItemStore();
74 $ts = $store->getWatchedItem( $user, $event->getTitle() )->getNotificationTimestamp();
75 // if (ts != null) is not sufficient because, if $wgEchoUseJobQueue is set,
76 // wl_notificationtimestamp will have already been set for the new edit
77 // by the time this code runs.
78 if ( $ts !== null && $ts !== $event->getExtraParam( "timestamp" ) ) {
79 // User has already seen an email for this page before
80 return false;
81 }
82 }
83 } elseif ( $event->getExtraParam( 'noemail' ) ) {
84 // Could be set for API triggered notifications were email is not
85 // requested in API request params
86 return false;
87 }
88
89 $hookRunner = new HookRunner( $services->getHookContainer() );
90 // Final check on whether to send email for this user & event
91 if ( !$hookRunner->onEchoAbortEmailNotification( $user, $event ) ) {
92 return false;
93 }
94
95 $attributeManager = Services::getInstance()->getAttributeManager();
96 $userEmailNotifications = $attributeManager->getUserEnabledEvents( $user, 'email' );
97 // See if the user wants to receive emails for this category or the user is eligible to receive this email
98 if ( in_array( $event->getType(), $userEmailNotifications ) ) {
99 global $wgEchoEnableEmailBatch, $wgEchoNotifications, $wgPasswordSender, $wgNoReplyAddress;
100
101 $priority = $attributeManager->getNotificationPriority( $event->getType() );
102
103 $bundleString = $bundleHash = '';
104
105 // We should have bundling for email digest as long as either web or email bundling is on,
106 // for example, talk page email bundling is off, but if a user decides to receive email
107 // digest, we should bundle those messages
108 if ( !empty( $wgEchoNotifications[$event->getType()]['bundle']['web'] ) ||
109 !empty( $wgEchoNotifications[$event->getType()]['bundle']['email'] )
110 ) {
111 self::getBundleRules( $event, $bundleString );
112 }
113 if ( $bundleString ) {
114 $bundleHash = md5( $bundleString );
115 }
116
117 // email digest notification ( weekly or daily )
118 if ( $wgEchoEnableEmailBatch && $userOptionsLookup->getOption( $user, 'echo-email-frequency' ) > 0 ) {
119 // always create a unique event hash for those events don't support bundling
120 // this is mainly for group by
121 if ( !$bundleHash ) {
122 $bundleHash = md5( $event->getType() . '-' . $event->getId() );
123 }
124 EmailBatch::addToQueue( $user->getId(), $event->getId(), $priority, $bundleHash );
125
126 return true;
127 }
128
129 // instant email notification
130 $toAddress = MailAddress::newFromUser( $user );
131 $fromAddress = new MailAddress(
132 $wgPasswordSender,
133 wfMessage( 'emailsender' )->inContentLanguage()->text()
134 );
135 $replyAddress = new MailAddress( $wgNoReplyAddress );
136 // Since we are sending a single email, should set the bundle hash to null
137 // if it is set with a value from somewhere else
138 $event->setBundleHash( null );
139 $email = self::generateEmail( $event, $user );
140 if ( !$email ) {
141 return false;
142 }
143 $subject = $email['subject'];
144 $body = $email['body'];
145 $options = [ 'replyTo' => $replyAddress ];
146
147 UserMailer::send( $toAddress, $fromAddress, $subject, $body, $options );
148 }
149
150 return true;
151 }
152
153 /**
154 * Handler to get bundle rules, handles echo's own events and calls the EchoGetBundleRule hook,
155 * which defines the bundle rule for the extensions notification.
156 *
157 * @param Event $event
158 * @param string &$bundleString Determines how the notification should be bundled, for example,
159 * talk page notification is bundled based on namespace and title, the bundle string would be
160 * 'edit-user-talk-' + namespace + title, email digest/email bundling would use this hash as
161 * a key to identify bundle-able event. For web bundling, we bundle further based on user's
162 * visit to the overlay, we would generate a display hash based on the hash of $bundleString
163 */
164 public static function getBundleRules( $event, &$bundleString ) {
165 switch ( $event->getType() ) {
166 case 'edit-user-page':
167 case 'edit-user-talk':
168 case 'page-linked':
169 $bundleString = $event->getType();
170 if ( $event->getTitle() ) {
171 $bundleString .= '-' . $event->getTitle()->getNamespace()
172 . '-' . $event->getTitle()->getDBkey();
173 }
174 break;
175 case 'mention-success':
176 case 'mention-failure':
177 $bundleString = 'mention-status-' . $event->getExtraParam( 'revid' );
178 break;
179 case 'watchlist-change':
180 case 'minor-watchlist-change':
181 $bundleString = 'watchlist-change';
182 if ( $event->getTitle() ) {
183 $bundleString .= '-' . $event->getTitle()->getNamespace()
184 . '-' . $event->getTitle()->getDBkey();
185 }
186 break;
187 default:
188 $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
189 $hookRunner->onEchoGetBundleRules( $event, $bundleString );
190 }
191 }
192
193 /**
194 * @param Event $event
195 * @param User $user
196 * @return array|false An array of 'subject' and 'body', or false if things went wrong
197 */
198 private static function generateEmail( Event $event, User $user ) {
199 $emailFormat = NotifUser::newFromUser( $user )->getEmailFormat();
200 $services = MediaWikiServices::getInstance();
201 $userOptionsLookup = $services->getUserOptionsLookup();
202 $lang = $services->getLanguageFactory()
203 ->getLanguage( $userOptionsLookup->getOption( $user, 'language' ) );
204 $formatter = new EchoPlainTextEmailFormatter( $user, $lang );
205 $content = $formatter->format( $event, 'email' );
206 if ( !$content ) {
207 return false;
208 }
209
210 if ( $emailFormat === EmailFormat::HTML ) {
211 $htmlEmailFormatter = new EchoHtmlEmailFormatter( $user, $lang );
212 $htmlContent = $htmlEmailFormatter->format( $event, 'email' );
213 $multipartBody = [
214 'text' => $content['body'],
215 'html' => $htmlContent['body']
216 ];
217 $content['body'] = $multipartBody;
218 }
219
220 return $content;
221 }
222 }
223
224 class_alias( Notifier::class, 'EchoNotifier' );