* Converted UserMailer stuff to return a Status object instead of true-or-WikiError
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sat, 4 Dec 2010 13:27:05 +0000 (13:27 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sat, 4 Dec 2010 13:27:05 +0000 (13:27 +0000)
* Made WikiError::isError() compatible with Status objects
* Added Status::getMessage() for backward compatibility

Extensions using WikiError::isError() to detect a failure of UserMailer::send() and realted methods should still work like before

includes/Preferences.php
includes/Status.php
includes/User.php
includes/UserMailer.php
includes/WikiError.php
includes/specials/SpecialConfirmemail.php
includes/specials/SpecialEmailuser.php
includes/specials/SpecialUserlogin.php
languages/messages/MessagesEn.php
maintenance/language/messageTypes.inc
maintenance/language/messages.inc

index deb17d0..9bd5add 100644 (file)
@@ -1272,8 +1272,8 @@ class Preferences {
                                        # Mail a temporary password to the dirty address.
                                        # User can come back through the confirmation URL to re-enable email.
                                        $result = $wgUser->sendConfirmationMail( $oldaddr != '' );
-                                       if ( WikiError::isError( $result ) ) {
-                                               return wfMsg( 'mailerror', htmlspecialchars( $result->getMessage() ) );
+                                       if ( !$result->isGood() ) {
+                                               return htmlspecialchars( $result->getWikiText( 'mailerror' ) );
                                        } elseif ( $entryPoint == 'ui' ) {
                                                $result = 'eauth';
                                        }
index b689931..ffb396c 100644 (file)
@@ -299,4 +299,13 @@ class Status {
                }
                return false;
        }
+
+       /**
+        * Backward compatibility function for WikiError -> Status migration
+        *
+        * @return String
+        */
+       public function getMessage() {
+               return $this->getWikiText();
+       }
 }
index 4029947..5d4444d 100644 (file)
@@ -2893,7 +2893,7 @@ class User {
         * mail to the user's given address.
         *
         * @param $changed Boolean: whether the adress changed
-        * @return \types{\bool,\type{WikiError}} True on success, a WikiError object on failure.
+        * @return Status object
         */
        function sendConfirmationMail( $changed = false ) {
                global $wgLang;
@@ -2923,7 +2923,7 @@ class User {
         * @param $body \string Message body
         * @param $from \string Optional From address; if unspecified, default $wgPasswordSender will be used
         * @param $replyto \string Reply-To address
-        * @return \types{\bool,\type{WikiError}} True on success, a WikiError object on failure
+        * @return Status object
         */
        function sendMail( $subject, $body, $from = null, $replyto = null ) {
                if( is_null( $from ) ) {
index 18d876c..4d7c57a 100644 (file)
@@ -90,9 +90,9 @@ class UserMailer {
                # Based on the result return an error string,
                if( PEAR::isError( $mailResult ) ) {
                        wfDebug( "PEAR::Mail failed: " . $mailResult->getMessage() . "\n" );
-                       return new WikiError( $mailResult->getMessage() );
+                       return Status::newFatal( 'pear-mail-error', $mailResult->getMessage() );
                } else {
-                       return true;
+                       return Status::newGood();
                }
        }
 
@@ -108,7 +108,7 @@ class UserMailer {
         * @param $body String: email's text.
         * @param $replyto MailAddress: optional reply-to email (default: null).
         * @param $contentType String: optional custom Content-Type
-        * @return mixed True on success, a WikiError object on failure.
+        * @return Status object
         */
        static function send( $to, $from, $subject, $body, $replyto=null, $contentType=null ) {
                global $wgSMTP, $wgOutputEncoding, $wgEnotifImpersonal;
@@ -179,19 +179,20 @@ class UserMailer {
                        if( PEAR::isError( $mail_object ) ) {
                                wfDebug( "PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n" );
                                wfRestoreWarnings();
-                               return new WikiError( $mail_object->getMessage() );
+                               return Status::newFatal( 'pear-mail-error', $mail_object->getMessage() );
                        }
 
                        wfDebug( "Sending mail via PEAR::Mail to $dest\n" );
                        $chunks = array_chunk( (array)$dest, $wgEnotifMaxRecips );
                        foreach ($chunks as $chunk) {
-                               $e = self::sendWithPear($mail_object, $chunk, $headers, $body);
-                               if( WikiError::isError( $e ) ) {
+                               $status = self::sendWithPear($mail_object, $chunk, $headers, $body);
+                               if( !$status->isOK() ) {
                                        wfRestoreWarnings();
-                                       return $e;
+                                       return $status;
                                }
                        }
                        wfRestoreWarnings();
+                       return Status::newGood();
                } else  {
                        # In the following $headers = expression we removed "Reply-To: {$from}\r\n" , because it is treated differently
                        # (fifth parameter of the PHP mail function, see some lines below)
@@ -236,13 +237,13 @@ class UserMailer {
 
                        if ( self::$mErrorString ) {
                                wfDebug( "Error sending mail: " . self::$mErrorString . "\n" );
-                               return new WikiError( self::$mErrorString );
+                               return Status::newFatal( 'php-mail-error', self::$mErrorString );
                        } elseif (! $sent ) {
                                //mail function only tells if there's an error
                                wfDebug( "Error sending mail\n" );
-                               return new WikiError( 'mail() failed' );
+                               return Status::newFatal( 'php-mail-error-unknown' );
                        } else {
-                               return true;
+                               return Status::newGood();
                        }
                }
        }
index adda36b..452554c 100644 (file)
@@ -60,7 +60,13 @@ class WikiError {
         * @return bool
         */
        public static function isError( $object ) {
-               return $object instanceof WikiError;
+               if ( $object instanceof WikiError ) {
+                       return true;
+               } elseif ( $object instanceof Status ) {
+                       return !$object->isOK();
+               } else {
+                       return false;
+               }
        }
 }
 
index d7da6b4..e556a60 100644 (file)
@@ -81,11 +81,11 @@ class EmailConfirmation extends UnlistedSpecialPage {
        function showRequestForm() {
                global $wgOut, $wgUser, $wgLang, $wgRequest;
                if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) {
-                       $ok = $wgUser->sendConfirmationMail();
-                       if ( WikiError::isError( $ok ) ) {
-                               $wgOut->addWikiMsg( 'confirmemail_sendfailed', $ok->toString() );
-                       } else {
+                       $status = $wgUser->sendConfirmationMail();
+                       if ( $status->isGood() ) {
                                $wgOut->addWikiMsg( 'confirmemail_sent' );
+                       } else {
+                               $wgOut->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
                        }
                } else {
                        if( $wgUser->isEmailConfirmed() ) {
index c098e88..4d06189 100644 (file)
@@ -142,7 +142,7 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                $wgOut->setPagetitle( wfMsg( 'emailpage' ) );
                $result = $form->show();
                
-               if( $result === true ){
+               if( $result === true || ( $result instanceof Status && $result->isGood() ) ){
                        $wgOut->setPagetitle( wfMsg( 'emailsent' ) );
                        $wgOut->addWikiMsg( 'emailsenttext' );
                        $wgOut->returnToMain( false, $this->mTargetObj->getUserPage() );
@@ -277,10 +277,10 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        $replyTo = null;
                }
 
-               $mailResult = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
+               $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
 
-               if( WikiError::isError( $mailResult ) && false ) {
-                       return $mailResult->getMessage();
+               if( !$status->isGood() && false ) {
+                       return $status;
                } else {
                        // if the user requested a copy of this mail, do this now,
                        // unless they are emailing themselves, in which case one 
@@ -292,20 +292,12 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                                        $subject
                                );
                                wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
-                               $ccResult = UserMailer::send( $from, $from, $cc_subject, $text );
-                               if( WikiError::isError( $ccResult ) ) {
-                                       // At this stage, the user's CC mail has failed, but their
-                                       // original mail has succeeded. It's unlikely, but still, 
-                                       // what to do? We can either show them an error, or we can 
-                                       // say everything was fine, or we can say we sort of failed 
-                                       // AND sort of succeeded. Of these options, simply saying 
-                                       // there was an error is probably best.
-                                       return $ccResult->getMessage();
-                               }
+                               $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
+                               $status->merge( $ccStatus );
                        }
 
                        wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
-                       return true;
+                       return $status;
                }
        }
 }
index 65b408e..c509c48 100644 (file)
@@ -170,8 +170,8 @@ class LoginForm {
                $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $wgOut->setArticleRelated( false );
 
-               if( WikiError::isError( $result ) ) {
-                       $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
+               if( !$result->isGood() ) {
+                       $this->mainLoginForm( wfMsg( 'mailerror', $result->getWikiText() ) );
                } else {
                        $wgOut->addWikiMsg( 'accmailtext', $u->getName(), $u->getEmail() );
                        $wgOut->returnToMain( false );
@@ -199,11 +199,11 @@ class LoginForm {
 
                # Send out an email authentication message if needed
                if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) ) {
-                       $error = $u->sendConfirmationMail();
-                       if( WikiError::isError( $error ) ) {
-                               $wgOut->addWikiMsg( 'confirmemail_sendfailed', $error->getMessage() );
-                       } else {
+                       $status = $u->sendConfirmationMail();
+                       if( $status->isGood() ) {
                                $wgOut->addWikiMsg( 'confirmemail_oncreate' );
+                       } else {
+                               $wgOut->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
                        }
                }
 
@@ -787,11 +787,11 @@ class LoginForm {
                }
 
                $result = $this->mailPasswordInternal( $u, true, 'passwordremindertitle', 'passwordremindertext' );
-               if( WikiError::isError( $result ) ) {
-                       $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
-               } else {
+               if( $result->isGood() ) {
                        $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
                        self::clearLoginToken();
+               } else {
+                       $this->mainLoginForm( $result->getWikiText( 'mailerror' ) );
                }
        }
 
@@ -801,18 +801,18 @@ class LoginForm {
         * @param $throttle Boolean
         * @param $emailTitle String: message name of email title
         * @param $emailText String: message name of email text
-        * @return Mixed: true on success, WikiError on failure
+        * @return Status object
         * @private
         */
        function mailPasswordInternal( $u, $throttle = true, $emailTitle = 'passwordremindertitle', $emailText = 'passwordremindertext' ) {
                global $wgServer, $wgScript, $wgUser, $wgNewPasswordExpiry;
 
                if ( $u->getEmail() == '' ) {
-                       return new WikiError( wfMsg( 'noemail', $u->getName() ) );
+                       return Status::newFatal( 'noemail', $u->getName() );
                }
                $ip = wfGetIP();
                if( !$ip ) {
-                       return new WikiError( wfMsg( 'badipaddress' ) );
+                       return Status::newFatal( 'badipaddress' );
                }
 
                wfRunHooks( 'User::mailPasswordInternal', array( &$wgUser, &$ip, &$u ) );
index 556b850..02fd41e 100644 (file)
@@ -1144,6 +1144,11 @@ Please wait before trying again.',
 * Nederlands|nl', # do not translate or duplicate this message to other languages
 'suspicious-userlogout'      => 'Your request to log out was denied because it looks like it was sent by a broken browser or caching proxy.',
 
+# E-mail sending
+'pear-mail-error'        => '$1', # do not translate or duplicate this message to other languages
+'php-mail-error'         => '$1', # do not translate or duplicate this message to other languages
+'php-mail-error-unknown' => "Unkown error in PHP's mail() function",
+
 # JavaScript password checks
 'password-strength'            => 'Estimated password strength: $1',
 'password-strength-bad'        => 'BAD',
index 8937862..2d1a54d 100644 (file)
@@ -91,6 +91,8 @@ $wgIgnoredMessages = array(
        'loginstart',
        'loginend',
        'loginlanguagelinks',
+       'pear-mail-error',
+       'php-mail-error',
        'markaspatrolledlink',
        'newarticletextanon',
        'newsectionheaderdefaultlevel',
index 27a159f..874b8a2 100644 (file)
@@ -463,6 +463,11 @@ $wgMessageStructure = array(
                'loginlanguagelinks',
                'suspicious-userlogout',
        ),
+       'mail' => array(
+               'pear-mail-error',
+               'php-mail-error',
+               'php-mail-error-unknown',
+       ),
        'passwordstrength' => array(
                'password-strength',
                'password-strength-bad',
@@ -3268,6 +3273,7 @@ XHTML id names.",
        'errors'              => 'General errors',
        'virus'               => 'Virus scanner',
        'login'               => 'Login and logout pages',
+       'mail'                => 'E-mail sending',
        'passwordstrength'    => 'JavaScript password checks',
        'resetpass'           => 'Password reset dialog',
        'toolbar'             => 'Edit page toolbar',