Temporarily reverting r99863 to see if I can pin down test failures
[lhc/web/wiklou.git] / includes / UserMailer.php
index 4b8ee6b..649fa20 100644 (file)
@@ -67,6 +67,8 @@ class MailAddress {
                        } else {
                                return $this->address;
                        }
+               } else {
+                       return "";
                }
        }
 
@@ -119,6 +121,24 @@ class UserMailer {
                return implode( $endl, $string );
        }
 
+       /**
+        * Create a value suitable for the MessageId Header
+        *
+        * @return String
+        */
+       static function makeMsgId() {
+               global $wgSMTP, $wgServer;
+
+               $msgid = uniqid( wfWikiID() . ".", true ); /* true required for cygwin */
+               if ( is_array($wgSMTP) && isset($wgSMTP['IDHost']) && $wgSMTP['IDHost'] ) {
+                       $domain = $wgSMTP['IDHost'];
+               } else {
+                       $url = wfParseUrl($wgServer);
+                       $domain = $url['host'];
+               }
+               return "<$msgid@$domain>";
+       }
+
        /**
         * This function will perform a direct (authenticated) login to
         * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an
@@ -143,11 +163,25 @@ class UserMailer {
                $headers['From'] = $from->toString();
                $headers['Return-Path'] = $from->toString();
 
+               $dest = array();
+               if ( is_array( $to ) ) {
+                       foreach ( $to as $u ) {
+                               if ( $u->address ) {
+                                       $dest[] = $u->address;
+                               }
+                       }
+               } else if( $to->address ) {
+                       $dest[] = $to->address;
+               }
+               if ( count( $dest ) == 0 ) {
+                       return Status::newFatal( 'user-mail-no-addy' );
+               }
+
                if ( $wgEnotifImpersonal ) {
                        $headers['To'] = 'undisclosed-recipients:;';
                }
                else {
-                       $headers['To'] = implode( ", ", (array )$dest );
+                       $headers['To'] = implode( ", ", $dest );
                }
 
                if ( $replyto ) {
@@ -159,11 +193,18 @@ class UserMailer {
                $headers['Content-type'] = ( is_null( $contentType ) ?
                        'text/plain; charset=UTF-8' : $contentType );
                $headers['Content-transfer-encoding'] = '8bit';
-               // @todo FIXME
-               $headers['Message-ID'] = "<$msgid@" . $wgSMTP['IDHost'] . '>';
+
+               $headers['Message-ID'] = self::makeMsgId();
                $headers['X-Mailer'] = 'MediaWiki mailer';
                $headers['From'] = $from->toString();
 
+               $ret = wfRunHooks( 'AlternateUserMailer', array( $headers, $to, $from, $subject, $body ) );
+               if ( $ret === false ) {
+                       return Status::newGood();
+               } else if ( $ret !== true ) {
+                       return Status::newFatal( 'php-mail-error', $ret );
+               }
+
                if ( is_array( $wgSMTP ) ) {
                        if ( function_exists( 'stream_resolve_include_path' ) ) {
                                $found = stream_resolve_include_path( 'Mail.php' );
@@ -175,20 +216,6 @@ class UserMailer {
                        }
                        require_once( 'Mail.php' );
 
-                       $msgid = str_replace( " ", "_", microtime() );
-                       if ( function_exists( 'posix_getpid' ) ) {
-                               $msgid .= '.' . posix_getpid();
-                       }
-
-                       if ( is_array( $to ) ) {
-                               $dest = array();
-                               foreach ( $to as $u ) {
-                                       $dest[] = $u->address;
-                               }
-                       } else {
-                               $dest = $to->address;
-                       }
-
                        wfSuppressWarnings();
 
                        // Create the mail object using the Mail::factory method
@@ -199,8 +226,8 @@ class UserMailer {
                                return Status::newFatal( 'pear-mail-error', $mail_object->getMessage() );
                        }
 
-                       wfDebug( "Sending mail via PEAR::Mail to $dest\n" );
-                       $chunks = array_chunk( (array)$dest, $wgEnotifMaxRecips );
+                       wfDebug( "Sending mail via PEAR::Mail\n" );
+                       $chunks = array_chunk( $dest, $wgEnotifMaxRecips );
                        foreach ( $chunks as $chunk ) {
                                $status = self::sendWithPear( $mail_object, $chunk, $headers, $body );
                                if ( !$status->isOK() ) {
@@ -229,11 +256,13 @@ class UserMailer {
                        ini_set( 'html_errors', '0' );
                        set_error_handler( 'UserMailer::errorHandler' );
 
-                       if ( !is_array( $to ) ) {
-                               $to = array( $to );
-                       }
-                       foreach ( $to as $recip ) {
-                               $sent = mail( $recip->toString(), self::quotedPrintable( $subject ), $body, $headers, $wgAdditionalMailParams );
+                       $safeMode = wfIniGetBool( 'safe_mode' );
+                       foreach ( $dest as $recip ) {
+                               if ( $safeMode ) {
+                                       $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers );
+                               } else {
+                                       $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers, $wgAdditionalMailParams );
+                               }
                        }
 
                        restore_error_handler();
@@ -244,7 +273,7 @@ class UserMailer {
                                return Status::newFatal( 'php-mail-error', self::$mErrorString );
                        } elseif ( ! $sent ) {
                                // mail function only tells if there's an error
-                               wfDebug( "Error sending mail\n" );
+                               wfDebug( "Unknown error sending mail\n" );
                                return Status::newFatal( 'php-mail-error-unknown' );
                        } else {
                                return Status::newGood();
@@ -340,7 +369,8 @@ class EmailNotification {
         * @param $oldid (default: false)
         */
        public function notifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid = false ) {
-               global $wgEnotifUseJobQ, $wgEnotifWatchlist, $wgShowUpdatedMarker;
+               global $wgEnotifUseJobQ, $wgEnotifWatchlist, $wgShowUpdatedMarker, $wgEnotifMinorEdits,
+                       $wgUsersNotifiedOnAllChanges, $wgEnotifUserTalk;
 
                if ( $title->getNamespace() < 0 ) {
                        return;
@@ -379,6 +409,24 @@ class EmailNotification {
                        }
                }
 
+               $sendEmail = true;
+               // 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
+               // $watchers deals with $wgEnotifWatchlist
+               if ( !count( $watchers ) && !count( $wgUsersNotifiedOnAllChanges ) ) {
+                       $sendEmail = false;
+                       // Only send notification for non minor edits, unless $wgEnotifMinorEdits
+                       if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
+                               $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK );
+                               if ( $wgEnotifUserTalk && $isUserTalkPage && $this->canSendUserTalkEmail( $editor, $title, $minorEdit ) ) {
+                                       $sendEmail = true;
+                               }
+                       }
+               }
+
+               if ( !$sendEmail ) {
+                       return;
+               }
                if ( $wgEnotifUseJobQ ) {
                        $params = array(
                                'editor' => $editor->getName(),
@@ -394,7 +442,6 @@ class EmailNotification {
                } else {
                        $this->actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid, $watchers );
                }
-
        }
 
        /**
@@ -435,25 +482,11 @@ class EmailNotification {
                $userTalkId = false;
 
                if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
-                       if ( $wgEnotifUserTalk && $isUserTalkPage ) {
+
+                       if ( $wgEnotifUserTalk && $isUserTalkPage && $this->canSendUserTalkEmail( $editor, $title, $minorEdit ) ) {
                                $targetUser = User::newFromName( $title->getText() );
-                               if ( !$targetUser || $targetUser->isAnon() ) {
-                                       wfDebug( __METHOD__ . ": user talk page edited, but user does not exist\n" );
-                               } elseif ( $targetUser->getId() == $editor->getId() ) {
-                                       wfDebug( __METHOD__ . ": user edited their own talk page, no notification sent\n" );
-                               } elseif ( $targetUser->getOption( 'enotifusertalkpages' ) &&
-                                       ( !$minorEdit || $targetUser->getOption( 'enotifminoredits' ) ) )
-                               {
-                                       if ( $targetUser->isEmailConfirmed() ) {
-                                               wfDebug( __METHOD__ . ": sending talk page update notification\n" );
-                                               $this->compose( $targetUser );
-                                               $userTalkId = $targetUser->getId();
-                                       } else {
-                                               wfDebug( __METHOD__ . ": talk page owner doesn't have validated email\n" );
-                                       }
-                               } else {
-                                       wfDebug( __METHOD__ . ": talk page owner doesn't want notifications\n" );
-                               }
+                               $this->compose( $targetUser );
+                               $userTalkId = $targetUser->getId();
                        }
 
                        if ( $wgEnotifWatchlist ) {
@@ -481,6 +514,39 @@ class EmailNotification {
                wfProfileOut( __METHOD__ );
        }
 
+       /**
+        * @param $editor User
+        * @param $title Title bool
+        * @param $minorEdit
+        * @return bool
+        */
+       private function canSendUserTalkEmail( $editor, $title, $minorEdit ) {
+               global $wgEnotifUserTalk;
+               $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK );
+
+               if ( $wgEnotifUserTalk && $isUserTalkPage ) {
+                       $targetUser = User::newFromName( $title->getText() );
+
+                       if ( !$targetUser || $targetUser->isAnon() ) {
+                               wfDebug( __METHOD__ . ": user talk page edited, but user does not exist\n" );
+                       } elseif ( $targetUser->getId() == $editor->getId() ) {
+                               wfDebug( __METHOD__ . ": user edited their own talk page, no notification sent\n" );
+                       } elseif ( $targetUser->getOption( 'enotifusertalkpages' ) &&
+                               ( !$minorEdit || $targetUser->getOption( 'enotifminoredits' ) ) )
+                       {
+                               if ( $targetUser->isEmailConfirmed() ) {
+                                       wfDebug( __METHOD__ . ": sending talk page update notification\n" );
+                                       return true;
+                               } else {
+                                       wfDebug( __METHOD__ . ": talk page owner doesn't have validated email\n" );
+                               }
+                       } else {
+                               wfDebug( __METHOD__ . ": talk page owner doesn't want notifications\n" );
+                       }
+               }
+           return false;
+       }
+
        /**
         * Generate the generic "this page has been changed" e-mail text.
         */
@@ -505,7 +571,7 @@ class EmailNotification {
                $keys    = array();
 
                if ( $this->oldid ) {
-                       $difflink = $this->title->getFullUrl( 'diff=0&oldid=' . $this->oldid );
+                       $difflink = $this->title->getCanonicalUrl( 'diff=0&oldid=' . $this->oldid );
                        $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastvisited', $difflink );
                        $keys['$OLDID']   = $this->oldid;
                        $keys['$CHANGEDORCREATED'] = wfMsgForContent( 'changed' );
@@ -522,17 +588,17 @@ class EmailNotification {
                         * revision.
                         */
                        $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastdiff',
-                                       $this->title->getFullURL( "oldid={$this->oldid}&diff=next" ) );
+                                       $this->title->getCanonicalUrl( "oldid={$this->oldid}&diff=next" ) );
                }
 
                $body = strtr( $body, $keys );
                $pagetitle = $this->title->getPrefixedText();
                $keys['$PAGETITLE']          = $pagetitle;
-               $keys['$PAGETITLE_URL']      = $this->title->getFullUrl();
+               $keys['$PAGETITLE_URL']      = $this->title->getCanonicalUrl();
 
                $keys['$PAGEMINOREDIT']      = $medit;
                $keys['$PAGESUMMARY']        = $summary;
-               $keys['$UNWATCHURL']         = $this->title->getFullUrl( 'action=unwatch' );
+               $keys['$UNWATCHURL']         = $this->title->getCanonicalUrl( 'action=unwatch' );
 
                $subject = strtr( $subject, $keys );
 
@@ -567,10 +633,10 @@ class EmailNotification {
                        $subject = str_replace( '$PAGEEDITOR', $name, $subject );
                        $keys['$PAGEEDITOR']          = $name;
                        $emailPage = SpecialPage::getSafeTitleFor( 'Emailuser', $name );
-                       $keys['$PAGEEDITOR_EMAIL'] = $emailPage->getFullUrl();
+                       $keys['$PAGEEDITOR_EMAIL'] = $emailPage->getCanonicalUrl();
                }
                $userPage = $editor->getUserPage();
-               $keys['$PAGEEDITOR_WIKI'] = $userPage->getFullUrl();
+               $keys['$PAGEEDITOR_WIKI'] = $userPage->getCanonicalUrl();
                $body = strtr( $body, $keys );
                $body = wordwrap( $body, 72 );