From: Alexandre Emsenhuber Date: Sat, 4 Dec 2010 13:27:05 +0000 (+0000) Subject: * Converted UserMailer stuff to return a Status object instead of true-or-WikiError X-Git-Tag: 1.31.0-rc.0~33580 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22sites_tous%22%29%20.%20%22?a=commitdiff_plain;h=4d19be865b5b37e26b6cbaef9dd7ed60ada42537;p=lhc%2Fweb%2Fwiklou.git * Converted UserMailer stuff to return a Status object instead of true-or-WikiError * 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 --- diff --git a/includes/Preferences.php b/includes/Preferences.php index deb17d088f..9bd5adda4b 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -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'; } diff --git a/includes/Status.php b/includes/Status.php index b6899312f5..ffb396cdb7 100644 --- a/includes/Status.php +++ b/includes/Status.php @@ -299,4 +299,13 @@ class Status { } return false; } + + /** + * Backward compatibility function for WikiError -> Status migration + * + * @return String + */ + public function getMessage() { + return $this->getWikiText(); + } } diff --git a/includes/User.php b/includes/User.php index 4029947b35..5d4444d62b 100644 --- a/includes/User.php +++ b/includes/User.php @@ -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 ) ) { diff --git a/includes/UserMailer.php b/includes/UserMailer.php index 18d876c0da..4d7c57a013 100644 --- a/includes/UserMailer.php +++ b/includes/UserMailer.php @@ -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(); } } } diff --git a/includes/WikiError.php b/includes/WikiError.php index adda36b915..452554c32a 100644 --- a/includes/WikiError.php +++ b/includes/WikiError.php @@ -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; + } } } diff --git a/includes/specials/SpecialConfirmemail.php b/includes/specials/SpecialConfirmemail.php index d7da6b4ef5..e556a60b5a 100644 --- a/includes/specials/SpecialConfirmemail.php +++ b/includes/specials/SpecialConfirmemail.php @@ -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() ) { diff --git a/includes/specials/SpecialEmailuser.php b/includes/specials/SpecialEmailuser.php index c098e88879..4d06189961 100644 --- a/includes/specials/SpecialEmailuser.php +++ b/includes/specials/SpecialEmailuser.php @@ -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; } } } diff --git a/includes/specials/SpecialUserlogin.php b/includes/specials/SpecialUserlogin.php index 65b408efa7..c509c48613 100644 --- a/includes/specials/SpecialUserlogin.php +++ b/includes/specials/SpecialUserlogin.php @@ -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 ) ); diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 556b850fa3..02fd41e8df 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -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', diff --git a/maintenance/language/messageTypes.inc b/maintenance/language/messageTypes.inc index 89378628c1..2d1a54d1b3 100644 --- a/maintenance/language/messageTypes.inc +++ b/maintenance/language/messageTypes.inc @@ -91,6 +91,8 @@ $wgIgnoredMessages = array( 'loginstart', 'loginend', 'loginlanguagelinks', + 'pear-mail-error', + 'php-mail-error', 'markaspatrolledlink', 'newarticletextanon', 'newsectionheaderdefaultlevel', diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 27a159f07c..874b8a204b 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -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',