64 private const USER_TALK =
'user_talk';
68 private const WATCHLIST =
'watchlist';
72 private const ALL_CHANGES =
'all_changes';
74 protected string $subject =
'';
76 protected string $body =
'';
84 protected string $summary =
'';
91 protected bool $composed_common =
false;
93 protected string $pageStatus =
'';
96 protected array $mailTargets = [];
113 return $this->pageStatus;
138 $pageStatus =
'changed'
140 if ( $title->getNamespace() < 0 ) {
144 $mwServices = MediaWikiServices::getInstance();
145 $config = $mwServices->getMainConfig();
149 if ( $config->get( MainConfigNames::EnotifWatchlist ) || $config->get( MainConfigNames::ShowUpdatedMarker ) ) {
150 $watchers = $mwServices->getWatchedItemStore()->updateNotificationTimestamp(
158 if ( $mwServices->getUserFactory()->newFromAuthority( $editor )->isBot() ) {
167 if ( $watchers === [] &&
168 !count( $config->get( MainConfigNames::UsersNotifiedOnAllChanges ) )
173 ( $config->get( MainConfigNames::EnotifMinorEdits ) &&
174 !$editor->
isAllowed(
'nominornewtalk' ) )
176 if ( $config->get( MainConfigNames::EnotifUserTalk )
178 && $this->canSendUserTalkEmail( $editor->
getUser(), $title, $minorEdit )
189 'editor' => $editor->
getUser()->getName(),
190 'editorID' => $editor->
getUser()->getId(),
191 'timestamp' => $timestamp,
192 'summary' => $summary,
193 'minorEdit' => $minorEdit,
195 'watchers' => $watchers,
196 'pageStatus' => $pageStatus
227 $pageStatus =
'changed'
229 # we use $wgPasswordSender as sender's address
231 $mwServices = MediaWikiServices::getInstance();
232 $messageCache = $mwServices->getMessageCache();
233 $config = $mwServices->getMainConfig();
235 # The following code is only run, if several conditions are met:
236 # 1. EmailNotification for pages (other than user_talk pages) must be enabled
237 # 2. minor edits (changes) are only regarded if the global flag indicates so
239 $this->title = $title;
240 $this->timestamp = $timestamp;
241 $this->summary = $summary;
242 $this->minorEdit = $minorEdit;
243 $this->oldid = $oldid;
244 $this->editor = MediaWikiServices::getInstance()->getUserFactory()->newFromAuthority( $editor );
245 $this->composed_common =
false;
246 $this->pageStatus = $pageStatus;
248 $formattedPageStatus = [
'deleted',
'created',
'moved',
'restored',
'changed' ];
250 $hookRunner =
new HookRunner( $mwServices->getHookContainer() );
251 $hookRunner->onUpdateUserMailerFormattedPageStatus( $formattedPageStatus );
252 if ( !in_array( $this->pageStatus, $formattedPageStatus ) ) {
253 throw new UnexpectedValueException(
'Not a valid page status!' );
259 ( $config->get( MainConfigNames::EnotifMinorEdits ) &&
260 !$editor->
isAllowed(
'nominornewtalk' ) )
262 if ( $config->get( MainConfigNames::EnotifUserTalk )
264 && $this->canSendUserTalkEmail( $editor->
getUser(), $title, $minorEdit )
266 $targetUser = User::newFromName( $title->
getText() );
267 $this->compose( $targetUser, self::USER_TALK, $messageCache );
268 $userTalkId = $targetUser->getId();
271 if ( $config->get( MainConfigNames::EnotifWatchlist ) ) {
272 $userOptionsLookup = $mwServices->getUserOptionsLookup();
275 $userArray = UserArray::newFromIDs( $watchers );
276 foreach ( $userArray as $watchingUser ) {
277 if ( $userOptionsLookup->getOption( $watchingUser,
'enotifwatchlistpages' )
278 && ( !$minorEdit || $userOptionsLookup->getOption( $watchingUser,
'enotifminoredits' ) )
279 && $watchingUser->isEmailConfirmed()
280 && $watchingUser->getId() != $userTalkId
281 && !in_array( $watchingUser->getName(),
282 $config->get( MainConfigNames::UsersNotifiedOnAllChanges ) )
285 && !( $config->get( MainConfigNames::BlockDisablesLogin ) &&
286 $watchingUser->getBlock() )
287 && $hookRunner->onSendWatchlistEmailNotification( $watchingUser, $title, $this )
289 $this->compose( $watchingUser, self::WATCHLIST, $messageCache );
295 foreach ( $config->get( MainConfigNames::UsersNotifiedOnAllChanges ) as $name ) {
296 if ( $editor->
getUser()->getName() == $name ) {
300 $user = User::newFromName( $name );
301 if ( $user instanceof
User ) {
302 $this->compose( $user, self::ALL_CHANGES, $messageCache );
314 private function canSendUserTalkEmail(
UserIdentity $editor, $title, $minorEdit ) {
315 $services = MediaWikiServices::getInstance();
316 $config = $services->getMainConfig();
322 $userOptionsLookup = $services->getUserOptionsLookup();
323 $targetUser = User::newFromName( $title->
getText() );
325 if ( !$targetUser || $targetUser->isAnon() ) {
326 wfDebug( __METHOD__ .
": user talk page edited, but user does not exist" );
327 } elseif ( $targetUser->getId() == $editor->
getId() ) {
328 wfDebug( __METHOD__ .
": user edited their own talk page, no notification sent" );
329 } elseif ( $targetUser->isTemp() ) {
330 wfDebug( __METHOD__ .
": talk page owner is a temporary user so doesn't have email" );
331 } elseif ( $config->get( MainConfigNames::BlockDisablesLogin ) &&
332 $targetUser->getBlock()
336 wfDebug( __METHOD__ .
": talk page owner is blocked and cannot login, no notification sent" );
337 } elseif ( $userOptionsLookup->getOption( $targetUser,
'enotifusertalkpages' )
338 && ( !$minorEdit || $userOptionsLookup->getOption( $targetUser,
'enotifminoredits' ) )
340 if ( !$targetUser->isEmailConfirmed() ) {
341 wfDebug( __METHOD__ .
": talk page owner doesn't have validated email" );
342 } elseif ( !(
new HookRunner( $services->getHookContainer() ) )
343 ->onAbortTalkPageEmailNotification( $targetUser, $title )
345 wfDebug( __METHOD__ .
": talk page update notification is aborted for this user" );
347 wfDebug( __METHOD__ .
": sending talk page update notification" );
351 wfDebug( __METHOD__ .
": talk page owner doesn't want notifications" );
360 private function composeCommonMailtext(
MessageCache $messageCache ) {
361 $services = MediaWikiServices::getInstance();
362 $config = $services->getMainConfig();
363 $userOptionsLookup = $services->getUserOptionsLookup();
365 $this->composed_common =
true;
367 # You as the WikiAdmin and Sysops can make use of plenty of
368 # named variables when composing your notification emails while
369 # simply editing the Meta pages
372 $postTransformKeys = [];
373 $pageTitleUrl = $this->title->getCanonicalURL();
374 $pageTitle = $this->title->getPrefixedText();
376 if ( $this->oldid ) {
380 $this->title->getCanonicalURL( [
'diff' =>
'next',
'oldid' => $this->oldid ] )
381 )->inContentLanguage()->text();
383 if ( !$config->get( MainConfigNames::EnotifImpersonal ) ) {
387 'enotif_lastvisited',
388 $this->title->getCanonicalURL( [
'diff' =>
'0',
'oldid' => $this->oldid ] )
389 )->inContentLanguage()->text();
391 $keys[
'$OLDID'] = $this->oldid;
392 $keys[
'$PAGELOG'] =
'';
395 $keys[
'$OLDID'] =
'';
396 $keys[
'$NEWPAGE'] =
'';
399 SpecialPage::getTitleFor(
'Log' )->getCanonicalURL( [
'page' => $this->title->getPrefixedDBkey() ] )
400 )->inContentLanguage()->text();
404 $keys[
'$PAGETITLE'] = $this->title->getPrefixedText();
405 $keys[
'$PAGETITLE_URL'] = $this->title->getCanonicalURL();
406 $keys[
'$PAGEMINOREDIT'] = $this->minorEdit ?
407 "\n\n" .
wfMessage(
'enotif_minoredit' )->inContentLanguage()->text() :
409 $keys[
'$UNWATCHURL'] = $this->title->getCanonicalURL(
'action=unwatch' );
411 if ( $this->editor->isAnon() ) {
412 # real anon (user:xxx.xxx.xxx.xxx)
413 $keys[
'$PAGEEDITOR'] =
wfMessage(
'enotif_anon_editor', $this->editor->getName() )
414 ->inContentLanguage()->text();
415 $keys[
'$PAGEEDITOR_EMAIL'] =
wfMessage(
'noemailtitle' )->inContentLanguage()->text();
416 } elseif ( $this->editor->isTemp() ) {
417 $keys[
'$PAGEEDITOR'] =
wfMessage(
'enotif_temp_editor', $this->editor->getName() )
418 ->inContentLanguage()->text();
419 $keys[
'$PAGEEDITOR_EMAIL'] =
wfMessage(
'noemailtitle' )->inContentLanguage()->text();
421 $keys[
'$PAGEEDITOR'] = $config->get( MainConfigNames::EnotifUseRealName ) &&
422 $this->editor->getRealName() !==
''
423 ? $this->editor->getRealName() : $this->editor->getName();
424 $emailPage = SpecialPage::getSafeTitleFor(
'Emailuser', $this->editor->getName() );
425 $keys[
'$PAGEEDITOR_EMAIL'] = $emailPage->getCanonicalURL();
428 $keys[
'$PAGEEDITOR_WIKI'] = $this->editor->getUserPage()->getCanonicalURL();
433 # Replace this after transforming the message, T37019
434 $postTransformKeys[
'$PAGESUMMARY'] = $this->summary ==
'' ?
' - ' : $this->summary;
441 $this->subject =
wfMessage(
'enotif_subject_' . $this->pageStatus )->inContentLanguage()
442 ->params( $pageTitle, $keys[
'$PAGEEDITOR'] )->text();
447 $keys[
'$PAGEINTRO'] =
wfMessage(
'enotif_body_intro_' . $this->pageStatus )
448 ->inContentLanguage()
449 ->params( $pageTitle, $keys[
'$PAGEEDITOR'], $pageTitleUrl )
452 $body =
wfMessage(
'enotif_body' )->inContentLanguage()->plain();
453 $body = strtr( $body, $keys );
454 $body = $messageCache->
transform( $body,
false,
null, $this->title );
455 $this->body = wordwrap( strtr( $body, $postTransformKeys ), 72 );
457 # Reveal the page editor's address as REPLY-TO address only if
458 # the user has not opted-out and the option is enabled at the
459 # global configuration level.
461 $config->get( MainConfigNames::PasswordSender ),
462 wfMessage(
'emailsender' )->inContentLanguage()->text()
464 if ( $config->get( MainConfigNames::EnotifRevealEditorAddress )
465 && ( $this->editor->getEmail() !=
'' )
466 && $userOptionsLookup->getOption( $this->editor,
'enotifrevealaddr' )
469 if ( $config->get( MainConfigNames::EnotifFromEditor ) ) {
470 $this->from = $editorAddress;
472 $this->from = $adminAddress;
473 $this->replyto = $editorAddress;
476 $this->from = $adminAddress;
478 $config->get( MainConfigNames::NoReplyAddress )
493 if ( !$this->composed_common ) {
494 $this->composeCommonMailtext( $messageCache );
497 if ( MediaWikiServices::getInstance()->getMainConfig()->
get( MainConfigNames::EnotifImpersonal ) ) {
500 $this->sendPersonalised( $user,
$source );
507 private function sendMails() {
508 if ( MediaWikiServices::getInstance()->getMainConfig()->
get( MainConfigNames::EnotifImpersonal ) ) {
509 $this->sendImpersonal( $this->mailTargets );
530 # $PAGEEDITDATE is the time and date of the page change
531 # expressed in terms of individual local time of the notification
532 # recipient, i.e. watching user
533 $mwServices = MediaWikiServices::getInstance();
534 $contLang = $mwServices->getContentLanguage();
535 $watchingUserName = (
536 $mwServices->getMainConfig()->get( MainConfigNames::EnotifUseRealName ) &&
547 $contLang->userDate( $this->timestamp, $watchingUser->
getUser() ),
548 $contLang->userTime( $this->timestamp, $watchingUser->
getUser() )
554 if (
$source === self::WATCHLIST ) {
555 $headers[
'List-Help'] =
'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Watchlist';
567 'replyTo' => $this->replyto,
568 'headers' => $headers,
579 private function sendImpersonal( array $addresses ): ?
StatusValue {
580 if ( count( $addresses ) === 0 ) {
583 $services = MediaWikiServices::getInstance();
584 $contLang = $services->getContentLanguage();
592 wfMessage(
'enotif_impersonal_salutation' )->inContentLanguage()->text(),
593 $contLang->date( $this->timestamp,
false,
false ),
594 $contLang->time( $this->timestamp,
false,
false )
608 'replyTo' => $this->replyto,