Merge "OutputPage.php: Remove misleading comment"
[lhc/web/wiklou.git] / includes / mail / EmailNotification.php
index 6c9972c..9bda12c 100644 (file)
  * Visit the documentation pages under http://meta.wikipedia.com/Enotif
  */
 class EmailNotification {
+
+       /**
+        * Notification is due to user's user talk being edited
+        */
+       const USER_TALK = 'user_talk';
+       /**
+        * Notification is due to a watchlisted page being edited
+        */
+       const WATCHLIST = 'watchlist';
+       /**
+        * Notification because user is notified for all changes
+        */
+       const ALL_CHANGES = 'all_changes';
+
        protected $subject, $body, $replyto, $from;
        protected $timestamp, $summary, $minorEdit, $oldid, $composed_common, $pageStatus;
        protected $mailTargets = array();
@@ -60,11 +74,15 @@ class EmailNotification {
        /**
         * @param User $editor The editor that triggered the update.  Their notification
         *  timestamp will not be updated(they have already seen it)
-        * @param Title $title The title to update timestamps for
+        * @param LinkTarget $linkTarget The link target of the title to update timestamps for
         * @param string $timestamp Set the update timestamp to this value
-        * @return int[]
+        * @return int[] Array of user IDs
         */
-       public static function updateWatchlistTimestamp( User $editor, Title $title, $timestamp ) {
+       public static function updateWatchlistTimestamp(
+               User $editor,
+               LinkTarget $linkTarget,
+               $timestamp
+       ) {
                global $wgEnotifWatchlist, $wgShowUpdatedMarker;
 
                if ( !$wgEnotifWatchlist && !$wgShowUpdatedMarker ) {
@@ -76,8 +94,8 @@ class EmailNotification {
                        array( 'wl_user' ),
                        array(
                                'wl_user != ' . intval( $editor->getID() ),
-                               'wl_namespace' => $title->getNamespace(),
-                               'wl_title' => $title->getDBkey(),
+                               'wl_namespace' => $linkTarget->getNamespace(),
+                               'wl_title' => $linkTarget->getDBkey(),
                                'wl_notificationtimestamp IS NULL',
                        ), __METHOD__
                );
@@ -91,14 +109,14 @@ class EmailNotification {
                        // Update wl_notificationtimestamp for all watching users except the editor
                        $fname = __METHOD__;
                        $dbw->onTransactionIdle(
-                               function () use ( $dbw, $timestamp, $watchers, $title, $fname ) {
+                               function () use ( $dbw, $timestamp, $watchers, $linkTarget, $fname ) {
                                        $dbw->update( 'watchlist',
                                                array( /* SET */
                                                        'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
                                                ), array( /* WHERE */
                                                        'wl_user' => $watchers,
-                                                       'wl_namespace' => $title->getNamespace(),
-                                                       'wl_title' => $title->getDBkey(),
+                                                       'wl_namespace' => $linkTarget->getNamespace(),
+                                                       'wl_title' => $linkTarget->getDBkey(),
                                                ), $fname
                                        );
                                }
@@ -124,7 +142,7 @@ class EmailNotification {
        public function notifyOnPageChange( $editor, $title, $timestamp, $summary,
                $minorEdit, $oldid = false, $pageStatus = 'changed'
        ) {
-               global $wgEnotifUseJobQ, $wgEnotifMinorEdits, $wgUsersNotifiedOnAllChanges, $wgEnotifUserTalk;
+               global $wgEnotifMinorEdits, $wgUsersNotifiedOnAllChanges, $wgEnotifUserTalk;
 
                if ( $title->getNamespace() < 0 ) {
                        return;
@@ -134,11 +152,10 @@ class EmailNotification {
                $watchers = self::updateWatchlistTimestamp( $editor, $title, $timestamp );
 
                $sendEmail = true;
+               // $watchers deals with $wgEnotifWatchlist.
                // If nobody is watching the page, and there are no users notified on all changes
                // don't bother creating a job/trying to send emails, unless it's a
                // talk page with an applicable notification.
-               //
-               // $watchers deals with $wgEnotifWatchlist
                if ( !count( $watchers ) && !count( $wgUsersNotifiedOnAllChanges ) ) {
                        $sendEmail = false;
                        // Only send notification for non minor edits, unless $wgEnotifMinorEdits
@@ -153,34 +170,20 @@ class EmailNotification {
                        }
                }
 
-               if ( !$sendEmail ) {
-                       return;
-               }
-
-               if ( $wgEnotifUseJobQ ) {
-                       $params = array(
-                               'editor' => $editor->getName(),
-                               'editorID' => $editor->getID(),
-                               'timestamp' => $timestamp,
-                               'summary' => $summary,
-                               'minorEdit' => $minorEdit,
-                               'oldid' => $oldid,
-                               'watchers' => $watchers,
-                               'pageStatus' => $pageStatus
-                       );
-                       $job = new EnotifNotifyJob( $title, $params );
-                       JobQueueGroup::singleton()->lazyPush( $job );
-               } else {
-                       $this->actuallyNotifyOnPageChange(
-                               $editor,
+               if ( $sendEmail ) {
+                       JobQueueGroup::singleton()->lazyPush( new EnotifNotifyJob(
                                $title,
-                               $timestamp,
-                               $summary,
-                               $minorEdit,
-                               $oldid,
-                               $watchers,
-                               $pageStatus
-                       );
+                               array(
+                                       'editor' => $editor->getName(),
+                                       'editorID' => $editor->getID(),
+                                       'timestamp' => $timestamp,
+                                       'summary' => $summary,
+                                       'minorEdit' => $minorEdit,
+                                       'oldid' => $oldid,
+                                       'watchers' => $watchers,
+                                       'pageStatus' => $pageStatus
+                               )
+                       ) );
                }
        }
 
@@ -203,6 +206,7 @@ class EmailNotification {
        public function actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit,
                $oldid, $watchers, $pageStatus = 'changed' ) {
                # we use $wgPasswordSender as sender's address
+               global $wgUsersNotifiedOnAllChanges;
                global $wgEnotifWatchlist, $wgBlockDisablesLogin;
                global $wgEnotifMinorEdits, $wgEnotifUserTalk;
 
@@ -236,7 +240,7 @@ class EmailNotification {
                                && $this->canSendUserTalkEmail( $editor, $title, $minorEdit )
                        ) {
                                $targetUser = User::newFromName( $title->getText() );
-                               $this->compose( $targetUser );
+                               $this->compose( $targetUser, self::USER_TALK );
                                $userTalkId = $targetUser->getId();
                        }
 
@@ -249,24 +253,24 @@ class EmailNotification {
                                                && ( !$minorEdit || $watchingUser->getOption( 'enotifminoredits' ) )
                                                && $watchingUser->isEmailConfirmed()
                                                && $watchingUser->getID() != $userTalkId
+                                               && !in_array( $watchingUser->getName(), $wgUsersNotifiedOnAllChanges )
                                                && !( $wgBlockDisablesLogin && $watchingUser->isBlocked() )
                                        ) {
                                                if ( Hooks::run( 'SendWatchlistEmailNotification', array( $watchingUser, $title, $this ) ) ) {
-                                                       $this->compose( $watchingUser );
+                                                       $this->compose( $watchingUser, self::WATCHLIST );
                                                }
                                        }
                                }
                        }
                }
 
-               global $wgUsersNotifiedOnAllChanges;
                foreach ( $wgUsersNotifiedOnAllChanges as $name ) {
                        if ( $editor->getName() == $name ) {
                                // No point notifying the user that actually made the change!
                                continue;
                        }
                        $user = User::newFromName( $name );
-                       $this->compose( $user );
+                       $this->compose( $user, self::ALL_CHANGES );
                }
 
                $this->sendMails();
@@ -427,8 +431,9 @@ class EmailNotification {
         *
         * Call sendMails() to send any mails that were queued.
         * @param User $user
+        * @param string $source
         */
-       function compose( $user ) {
+       function compose( $user, $source ) {
                global $wgEnotifImpersonal;
 
                if ( !$this->composed_common ) {
@@ -438,7 +443,7 @@ class EmailNotification {
                if ( $wgEnotifImpersonal ) {
                        $this->mailTargets[] = MailAddress::newFromUser( $user );
                } else {
-                       $this->sendPersonalised( $user );
+                       $this->sendPersonalised( $user, $source );
                }
        }
 
@@ -458,10 +463,11 @@ class EmailNotification {
         * Returns true if the mail was sent successfully.
         *
         * @param User $watchingUser
+        * @param string $source
         * @return bool
         * @private
         */
-       function sendPersonalised( $watchingUser ) {
+       function sendPersonalised( $watchingUser, $source ) {
                global $wgContLang, $wgEnotifUseRealName;
                // From the PHP manual:
                //   Note: The to parameter cannot be an address in the form of
@@ -482,8 +488,14 @@ class EmailNotification {
                                $wgContLang->userTime( $this->timestamp, $watchingUser ) ),
                        $this->body );
 
+               $headers = array();
+               if ( $source === self::WATCHLIST ) {
+                       $headers['List-Help'] = 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Watchlist';
+               }
+
                return UserMailer::send( $to, $this->from, $this->subject, $body, array(
                        'replyTo' => $this->replyto,
+                       'headers' => $headers,
                ) );
        }