MediaWiki REL1_33
EmailNotification.php
Go to the documentation of this file.
1<?php
28
49
53 const USER_TALK = 'user_talk';
57 const WATCHLIST = 'watchlist';
61 const ALL_CHANGES = 'all_changes';
62
65 protected $mailTargets = [];
66
70 protected $title;
71
75 protected $editor;
76
87 public function getPageStatus() {
88 return $this->pageStatus;
89 }
90
104 public function notifyOnPageChange( $editor, $title, $timestamp, $summary,
105 $minorEdit, $oldid = false, $pageStatus = 'changed'
106 ) {
108
109 if ( $title->getNamespace() < 0 ) {
110 return;
111 }
112
113 // update wl_notificationtimestamp for watchers
114 $config = RequestContext::getMain()->getConfig();
115 $watchers = [];
116 if ( $config->get( 'EnotifWatchlist' ) || $config->get( 'ShowUpdatedMarker' ) ) {
117 $watchers = MediaWikiServices::getInstance()->getWatchedItemStore()->updateNotificationTimestamp(
118 $editor,
119 $title,
121 );
122 }
123
124 $sendEmail = true;
125 // $watchers deals with $wgEnotifWatchlist.
126 // If nobody is watching the page, and there are no users notified on all changes
127 // don't bother creating a job/trying to send emails, unless it's a
128 // talk page with an applicable notification.
129 if ( $watchers === [] && !count( $wgUsersNotifiedOnAllChanges ) ) {
130 $sendEmail = false;
131 // Only send notification for non minor edits, unless $wgEnotifMinorEdits
132 if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
133 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK );
135 && $isUserTalkPage
136 && $this->canSendUserTalkEmail( $editor, $title, $minorEdit )
137 ) {
138 $sendEmail = true;
139 }
140 }
141 }
142
143 if ( $sendEmail ) {
144 JobQueueGroup::singleton()->lazyPush( new EnotifNotifyJob(
145 $title,
146 [
147 'editor' => $editor->getName(),
148 'editorID' => $editor->getId(),
149 'timestamp' => $timestamp,
150 'summary' => $summary,
151 'minorEdit' => $minorEdit,
152 'oldid' => $oldid,
153 'watchers' => $watchers,
154 'pageStatus' => $pageStatus
155 ]
156 ) );
157 }
158 }
159
177 $oldid, $watchers, $pageStatus = 'changed' ) {
178 # we use $wgPasswordSender as sender's address
182
183 # The following code is only run, if several conditions are met:
184 # 1. EmailNotification for pages (other than user_talk pages) must be enabled
185 # 2. minor edits (changes) are only regarded if the global flag indicates so
186
187 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK );
188
189 $this->title = $title;
190 $this->timestamp = $timestamp;
191 $this->summary = $summary;
192 $this->minorEdit = $minorEdit;
193 $this->oldid = $oldid;
194 $this->editor = $editor;
195 $this->composed_common = false;
196 $this->pageStatus = $pageStatus;
197
198 $formattedPageStatus = [ 'deleted', 'created', 'moved', 'restored', 'changed' ];
199
200 Hooks::run( 'UpdateUserMailerFormattedPageStatus', [ &$formattedPageStatus ] );
201 if ( !in_array( $this->pageStatus, $formattedPageStatus ) ) {
202 throw new MWException( 'Not a valid page status!' );
203 }
204
205 $userTalkId = false;
206
207 if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
209 && $isUserTalkPage
210 && $this->canSendUserTalkEmail( $editor, $title, $minorEdit )
211 ) {
212 $targetUser = User::newFromName( $title->getText() );
213 $this->compose( $targetUser, self::USER_TALK );
214 $userTalkId = $targetUser->getId();
215 }
216
217 if ( $wgEnotifWatchlist ) {
218 // Send updates to watchers other than the current editor
219 // and don't send to watchers who are blocked and cannot login
220 $userArray = UserArray::newFromIDs( $watchers );
221 foreach ( $userArray as $watchingUser ) {
222 if ( $watchingUser->getOption( 'enotifwatchlistpages' )
223 && ( !$minorEdit || $watchingUser->getOption( 'enotifminoredits' ) )
224 && $watchingUser->isEmailConfirmed()
225 && $watchingUser->getId() != $userTalkId
226 && !in_array( $watchingUser->getName(), $wgUsersNotifiedOnAllChanges )
227 && !( $wgBlockDisablesLogin && $watchingUser->isBlocked() )
228 && Hooks::run( 'SendWatchlistEmailNotification', [ $watchingUser, $title, $this ] )
229 ) {
230 $this->compose( $watchingUser, self::WATCHLIST );
231 }
232 }
233 }
234 }
235
236 foreach ( $wgUsersNotifiedOnAllChanges as $name ) {
237 if ( $editor->getName() == $name ) {
238 // No point notifying the user that actually made the change!
239 continue;
240 }
241 $user = User::newFromName( $name );
242 $this->compose( $user, self::ALL_CHANGES );
243 }
244
245 $this->sendMails();
246 }
247
254 private function canSendUserTalkEmail( $editor, $title, $minorEdit ) {
256 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK );
257
258 if ( $wgEnotifUserTalk && $isUserTalkPage ) {
259 $targetUser = User::newFromName( $title->getText() );
260
261 if ( !$targetUser || $targetUser->isAnon() ) {
262 wfDebug( __METHOD__ . ": user talk page edited, but user does not exist\n" );
263 } elseif ( $targetUser->getId() == $editor->getId() ) {
264 wfDebug( __METHOD__ . ": user edited their own talk page, no notification sent\n" );
265 } elseif ( $wgBlockDisablesLogin && $targetUser->isBlocked() ) {
266 wfDebug( __METHOD__ . ": talk page owner is blocked and cannot login, no notification sent\n" );
267 } elseif ( $targetUser->getOption( 'enotifusertalkpages' )
268 && ( !$minorEdit || $targetUser->getOption( 'enotifminoredits' ) )
269 ) {
270 if ( !$targetUser->isEmailConfirmed() ) {
271 wfDebug( __METHOD__ . ": talk page owner doesn't have validated email\n" );
272 } elseif ( !Hooks::run( 'AbortTalkPageEmailNotification', [ $targetUser, $title ] ) ) {
273 wfDebug( __METHOD__ . ": talk page update notification is aborted for this user\n" );
274 } else {
275 wfDebug( __METHOD__ . ": sending talk page update notification\n" );
276 return true;
277 }
278 } else {
279 wfDebug( __METHOD__ . ": talk page owner doesn't want notifications\n" );
280 }
281 }
282 return false;
283 }
284
288 private function composeCommonMailtext() {
292
293 $this->composed_common = true;
294
295 # You as the WikiAdmin and Sysops can make use of plenty of
296 # named variables when composing your notification emails while
297 # simply editing the Meta pages
298
299 $keys = [];
300 $postTransformKeys = [];
301 $pageTitleUrl = $this->title->getCanonicalURL();
302 $pageTitle = $this->title->getPrefixedText();
303
304 if ( $this->oldid ) {
305 // Always show a link to the diff which triggered the mail. See T34210.
306 $keys['$NEWPAGE'] = "\n\n" . wfMessage( 'enotif_lastdiff',
307 $this->title->getCanonicalURL( [ 'diff' => 'next', 'oldid' => $this->oldid ] ) )
308 ->inContentLanguage()->text();
309
310 if ( !$wgEnotifImpersonal ) {
311 // For personal mail, also show a link to the diff of all changes
312 // since last visited.
313 $keys['$NEWPAGE'] .= "\n\n" . wfMessage( 'enotif_lastvisited',
314 $this->title->getCanonicalURL( [ 'diff' => '0', 'oldid' => $this->oldid ] ) )
315 ->inContentLanguage()->text();
316 }
317 $keys['$OLDID'] = $this->oldid;
318 // Deprecated since MediaWiki 1.21, not used by default. Kept for backwards-compatibility.
319 $keys['$CHANGEDORCREATED'] = wfMessage( 'changed' )->inContentLanguage()->text();
320 } else {
321 # clear $OLDID placeholder in the message template
322 $keys['$OLDID'] = '';
323 $keys['$NEWPAGE'] = '';
324 // Deprecated since MediaWiki 1.21, not used by default. Kept for backwards-compatibility.
325 $keys['$CHANGEDORCREATED'] = wfMessage( 'created' )->inContentLanguage()->text();
326 }
327
328 $keys['$PAGETITLE'] = $this->title->getPrefixedText();
329 $keys['$PAGETITLE_URL'] = $this->title->getCanonicalURL();
330 $keys['$PAGEMINOREDIT'] = $this->minorEdit ?
331 "\n\n" . wfMessage( 'enotif_minoredit' )->inContentLanguage()->text() :
332 '';
333 $keys['$UNWATCHURL'] = $this->title->getCanonicalURL( 'action=unwatch' );
334
335 if ( $this->editor->isAnon() ) {
336 # real anon (user:xxx.xxx.xxx.xxx)
337 $keys['$PAGEEDITOR'] = wfMessage( 'enotif_anon_editor', $this->editor->getName() )
338 ->inContentLanguage()->text();
339 $keys['$PAGEEDITOR_EMAIL'] = wfMessage( 'noemailtitle' )->inContentLanguage()->text();
340
341 } else {
342 $keys['$PAGEEDITOR'] = $wgEnotifUseRealName && $this->editor->getRealName() !== ''
343 ? $this->editor->getRealName() : $this->editor->getName();
344 $emailPage = SpecialPage::getSafeTitleFor( 'Emailuser', $this->editor->getName() );
345 $keys['$PAGEEDITOR_EMAIL'] = $emailPage->getCanonicalURL();
346 }
347
348 $keys['$PAGEEDITOR_WIKI'] = $this->editor->getUserPage()->getCanonicalURL();
349 $keys['$HELPPAGE'] = wfExpandUrl(
350 Skin::makeInternalOrExternalUrl( wfMessage( 'helppage' )->inContentLanguage()->text() )
351 );
352
353 # Replace this after transforming the message, T37019
354 $postTransformKeys['$PAGESUMMARY'] = $this->summary == '' ? ' - ' : $this->summary;
355
356 // Now build message's subject and body
357
358 // Messages:
359 // enotif_subject_deleted, enotif_subject_created, enotif_subject_moved,
360 // enotif_subject_restored, enotif_subject_changed
361 $this->subject = wfMessage( 'enotif_subject_' . $this->pageStatus )->inContentLanguage()
362 ->params( $pageTitle, $keys['$PAGEEDITOR'] )->text();
363
364 // Messages:
365 // enotif_body_intro_deleted, enotif_body_intro_created, enotif_body_intro_moved,
366 // enotif_body_intro_restored, enotif_body_intro_changed
367 $keys['$PAGEINTRO'] = wfMessage( 'enotif_body_intro_' . $this->pageStatus )
368 ->inContentLanguage()->params( $pageTitle, $keys['$PAGEEDITOR'], $pageTitleUrl )
369 ->text();
370
371 $body = wfMessage( 'enotif_body' )->inContentLanguage()->plain();
372 $body = strtr( $body, $keys );
373 $body = MessageCache::singleton()->transform( $body, false, null, $this->title );
374 $this->body = wordwrap( strtr( $body, $postTransformKeys ), 72 );
375
376 # Reveal the page editor's address as REPLY-TO address only if
377 # the user has not opted-out and the option is enabled at the
378 # global configuration level.
379 $adminAddress = new MailAddress( $wgPasswordSender,
380 wfMessage( 'emailsender' )->inContentLanguage()->text() );
382 && ( $this->editor->getEmail() != '' )
383 && $this->editor->getOption( 'enotifrevealaddr' )
384 ) {
385 $editorAddress = MailAddress::newFromUser( $this->editor );
386 if ( $wgEnotifFromEditor ) {
387 $this->from = $editorAddress;
388 } else {
389 $this->from = $adminAddress;
390 $this->replyto = $editorAddress;
391 }
392 } else {
393 $this->from = $adminAddress;
394 $this->replyto = new MailAddress( $wgNoReplyAddress );
395 }
396 }
397
406 function compose( $user, $source ) {
407 global $wgEnotifImpersonal;
408
409 if ( !$this->composed_common ) {
410 $this->composeCommonMailtext();
411 }
412
413 if ( $wgEnotifImpersonal ) {
414 $this->mailTargets[] = MailAddress::newFromUser( $user );
415 } else {
416 $this->sendPersonalised( $user, $source );
417 }
418 }
419
423 function sendMails() {
424 global $wgEnotifImpersonal;
425 if ( $wgEnotifImpersonal ) {
426 $this->sendImpersonal( $this->mailTargets );
427 }
428 }
429
441 function sendPersonalised( $watchingUser, $source ) {
443 // From the PHP manual:
444 // Note: The to parameter cannot be an address in the form of
445 // "Something <someone@example.com>". The mail command will not parse
446 // this properly while talking with the MTA.
447 $to = MailAddress::newFromUser( $watchingUser );
448
449 # $PAGEEDITDATE is the time and date of the page change
450 # expressed in terms of individual local time of the notification
451 # recipient, i.e. watching user
452 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
454 [ '$WATCHINGUSERNAME',
455 '$PAGEEDITDATE',
456 '$PAGEEDITTIME' ],
457 [ $wgEnotifUseRealName && $watchingUser->getRealName() !== ''
458 ? $watchingUser->getRealName() : $watchingUser->getName(),
459 $contLang->userDate( $this->timestamp, $watchingUser ),
460 $contLang->userTime( $this->timestamp, $watchingUser ) ],
461 $this->body );
462
463 $headers = [];
464 if ( $source === self::WATCHLIST ) {
465 $headers['List-Help'] = 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Watchlist';
466 }
467
468 return UserMailer::send( $to, $this->from, $this->subject, $body, [
469 'replyTo' => $this->replyto,
470 'headers' => $headers,
471 ] );
472 }
473
480 function sendImpersonal( $addresses ) {
481 if ( empty( $addresses ) ) {
482 return null;
483 }
484
485 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
487 [ '$WATCHINGUSERNAME',
488 '$PAGEEDITDATE',
489 '$PAGEEDITTIME' ],
490 [ wfMessage( 'enotif_impersonal_salutation' )->inContentLanguage()->text(),
491 $contLang->date( $this->timestamp, false, false ),
492 $contLang->time( $this->timestamp, false, false ) ],
493 $this->body );
494
495 return UserMailer::send( $addresses, $this->from, $this->subject, $body, [
496 'replyTo' => $this->replyto,
497 ] );
498 }
499
500}
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
bool $wgEnotifRevealEditorAddress
Allow sending of e-mail notifications with the editor's address in "Reply-To".
$wgEnotifWatchlist
Allow users to enable email notification ("enotif") on watchlist changes.
bool $wgEnotifFromEditor
Allow sending of e-mail notifications with the editor's address as sender.
$wgEnotifUserTalk
Allow users to enable email notification ("enotif") when someone edits their user talk page.
$wgNoReplyAddress
Reply-To address for e-mail notifications.
$wgEnotifImpersonal
Send a generic mail instead of a personalised mail for each user.
$wgUsersNotifiedOnAllChanges
Array of usernames who will be sent a notification email for every change which occurs on a wiki.
$wgBlockDisablesLogin
If true, blocked users will not be allowed to login.
$wgEnotifMinorEdits
Potentially send notification mails on minor edits to pages.
$wgPasswordSender
Sender email address for e-mail notifications.
$wgEnotifUseRealName
Use real name instead of username in e-mail "from" field.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
This module processes the email notifications when the current page is changed.
sendImpersonal( $addresses)
Same as sendPersonalised but does impersonal mail suitable for bulk mailing.
canSendUserTalkEmail( $editor, $title, $minorEdit)
const ALL_CHANGES
Notification because user is notified for all changes.
sendMails()
Send any queued mails.
notifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid=false, $pageStatus='changed')
Send emails corresponding to the user $editor editing the page $title.
sendPersonalised( $watchingUser, $source)
Does the per-user customizations to a notification e-mail (name, timestamp in proper timezone,...
compose( $user, $source)
Compose a mail to a given user and either queue it for sending, or send it now, depending on settings...
const WATCHLIST
Notification is due to a watchlisted page being edited.
actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid, $watchers, $pageStatus='changed')
Immediate version of notifyOnPageChange().
const USER_TALK
Notification is due to user's user talk being edited.
getPageStatus()
Extensions that have hooks for UpdateUserMailerFormattedPageStatus (to provide additional pageStatus ...
composeCommonMailtext()
Generate the generic "this page has been changed" e-mail text.
Job for email notification mails.
MediaWiki exception.
Stores a single person's name and email address.
static newFromUser(User $user)
Create a new MailAddress object for the given user.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:40
static newFromIDs( $ids)
Definition UserArray.php:45
static send( $to, $from, $subject, $body, $options=[])
This function will perform a direct (authenticated) login to a SMTP Server to use for mail relaying i...
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2452
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
isAllowed( $action='')
Internal mechanics of testing a permission.
Definition User.php:3856
getId()
Get the user's ID.
Definition User.php:2425
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
const NS_USER_TALK
Definition Defines.php:76
$source
title