jenkins-bot has submitted this change and it was merged.

Change subject: Update Special:ChangeEmail to use HTMLForm
......................................................................


Update Special:ChangeEmail to use HTMLForm

Makes the code shorter and easier to read.

Change-Id: I629cee4264fad6cde98495c0e8daffe5ea245b48
---
M includes/specials/SpecialChangeEmail.php
1 file changed, 84 insertions(+), 160 deletions(-)

Approvals:
  Legoktm: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/specials/SpecialChangeEmail.php 
b/includes/specials/SpecialChangeEmail.php
index 7fc4a17..d3e7398 100644
--- a/includes/specials/SpecialChangeEmail.php
+++ b/includes/specials/SpecialChangeEmail.php
@@ -26,18 +26,11 @@
  *
  * @ingroup SpecialPage
  */
-class SpecialChangeEmail extends UnlistedSpecialPage {
+class SpecialChangeEmail extends FormSpecialPage {
        /**
-        * Users password
-        * @var string
+        * @var Status
         */
-       protected $mPassword;
-
-       /**
-        * Users new email address
-        * @var string
-        */
-       protected $mNewEmail;
+       private $status;
 
        public function __construct() {
                parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
@@ -57,196 +50,128 @@
         * @param string $par
         */
        function execute( $par ) {
-               global $wgAuth;
-
-               $this->setHeaders();
-               $this->outputHeader();
-
                $out = $this->getOutput();
                $out->disallowUserJs();
                $out->addModules( 'mediawiki.special.changeemail' );
 
+               return parent::execute( $par );
+       }
+
+       protected function checkExecutePermissions( User $user ) {
+               global $wgAuth;
+
                if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
-                       $this->error( 'cannotchangeemail' );
-
-                       return;
+                       throw new ErrorPageError( 'changeemail', 
'cannotchangeemail' );
                }
-
-               $user = $this->getUser();
-               $request = $this->getRequest();
 
                $this->requireLogin( 'changeemail-no-info' );
 
-               if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) 
{
-                       $this->doReturnTo();
-
-                       return;
-               }
-
-               $this->checkReadOnly();
-               $this->checkPermissions();
-
                // This could also let someone check the current email address, 
so
                // require both permissions.
-               if ( !$user->isAllowed( 'viewmyprivateinfo' ) ) {
+               if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
                        throw new PermissionsError( 'viewmyprivateinfo' );
                }
 
-               $this->mPassword = $request->getVal( 'wpPassword' );
-               $this->mNewEmail = $request->getVal( 'wpNewEmail' );
-
-               if ( $request->wasPosted()
-                       && $user->matchEditToken( $request->getVal( 'token' ) )
-               ) {
-                       $info = $this->attemptChange( $user, $this->mPassword, 
$this->mNewEmail );
-                       if ( $info === true ) {
-                               $this->doReturnTo();
-                       } elseif ( $info === 'eauth' ) {
-                               # Notify user that a confirmation email has 
been sent...
-                               $out->wrapWikiMsg( "<div class='error' 
style='clear: both;'>\n$1\n</div>",
-                                       'eauthentsent', $user->getName() );
-                               $this->doReturnTo( 'soft' ); // just show the 
link to go back
-                               return; // skip form
-                       }
-               }
-
-               $this->showForm();
+               parent::checkExecutePermissions( $user );
        }
 
-       /**
-        * @param string $type
-        */
-       protected function doReturnTo( $type = 'hard' ) {
+       protected function getFormFields() {
+               $user = $this->getUser();
+
+               $fields = array(
+                       'Name' => array(
+                               'type' => 'info',
+                               'label-message' => 'username',
+                               'default' => $user->getName(),
+                       ),
+                       'OldEmail' => array(
+                               'type' => 'info',
+                               'label-message' => 'changeemail-oldemail',
+                               'default' => $user->getEmail() ?: $this->msg( 
'changeemail-none' )->text(),
+                       ),
+                       'NewEmail' => array(
+                               'type' => 'email',
+                               'label-message' => 'changeemail-newemail',
+                       ),
+               );
+
+               if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' ) 
) {
+                       $fields['Password'] = array(
+                               'type' => 'password',
+                               'label-message' => 'changeemail-password',
+                               'autofocus' => true,
+                       );
+               }
+
+               return $fields;
+       }
+
+       protected function alterForm( HTMLForm $form ) {
+               $form->setId( 'mw-changeemail-form' );
+               $form->setTableId( 'mw-changeemail-table' );
+               $form->setWrapperLegendMsg( 'changeemail-header' );
+               $form->setSubmitTextMsg( 'changeemail-submit' );
+               $form->addButton( 'wpCancel',  $this->msg( 'changeemail-cancel' 
)->text() );
+               $form->addHiddenField( 'returnto', $this->getRequest()->getVal( 
'returnto' ) );
+       }
+
+       public function onSubmit( array $data ) {
+               if ( $this->getRequest()->getBool( 'wpCancel' ) ) {
+                       $status = Status::newGood( true );
+               } else {
+                       $password = isset( $data['Password'] ) ? 
$data['Password'] : null;
+                       $status = $this->attemptChange( $this->getUser(), 
$password, $data['NewEmail'] );
+               }
+
+               $this->status = $status;
+
+               return $status;
+       }
+
+       public function onSuccess() {
                $titleObj = Title::newFromText( $this->getRequest()->getVal( 
'returnto' ) );
                if ( !$titleObj instanceof Title ) {
                        $titleObj = Title::newMainPage();
                }
-               if ( $type == 'hard' ) {
+
+               if ( $this->status->value === true ) {
                        $this->getOutput()->redirect( $titleObj->getFullURL() );
-               } else {
-                       $this->getOutput()->addReturnTo( $titleObj );
+               } elseif ( $this->status->value === 'eauth' ) {
+                       # Notify user that a confirmation email has been sent...
+                       $this->getOutput()->wrapWikiMsg( "<div class='error' 
style='clear: both;'>\n$1\n</div>",
+                               'eauthentsent', $this->getUser()->getName() );
+                       $this->getOutput()->addReturnTo( $titleObj ); // just 
show the link to go back
                }
-       }
-
-       /**
-        * @param string $msg
-        */
-       protected function error( $msg ) {
-               $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", 
$msg );
-       }
-
-       protected function showForm() {
-               $user = $this->getUser();
-
-               $oldEmailText = $user->getEmail()
-                       ? $user->getEmail()
-                       : $this->msg( 'changeemail-none' )->text();
-
-               $this->getOutput()->addHTML(
-                       Xml::fieldset( $this->msg( 'changeemail-header' 
)->text() ) .
-                               Xml::openElement( 'form',
-                                       array(
-                                               'method' => 'post',
-                                               'action' => 
$this->getPageTitle()->getLocalURL(),
-                                               'id' => 'mw-changeemail-form' ) 
) . "\n" .
-                               Html::hidden( 'token', $user->getEditToken() ) 
. "\n" .
-                               Html::hidden( 'returnto', 
$this->getRequest()->getVal( 'returnto' ) ) . "\n" .
-                               $this->msg( 'changeemail-text' 
)->parseAsBlock() . "\n" .
-                               Xml::openElement( 'table', array( 'id' => 
'mw-changeemail-table' ) ) . "\n"
-               );
-               $items = array(
-                       array( 'wpName', 'username', 'text', $user->getName() ),
-                       array( 'wpOldEmail', 'changeemail-oldemail', 'text', 
$oldEmailText ),
-                       array( 'wpNewEmail', 'changeemail-newemail', 'email', 
$this->mNewEmail ),
-               );
-               if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' ) 
) {
-                       $items[] = array( 'wpPassword', 'changeemail-password', 
'password', $this->mPassword );
-               }
-
-               $this->getOutput()->addHTML(
-                       $this->pretty( $items ) .
-                               "\n" .
-                               "<tr>\n" .
-                               "<td></td>\n" .
-                               '<td class="mw-input">' .
-                               Xml::submitButton( $this->msg( 
'changeemail-submit' )->text() ) .
-                               Xml::submitButton( $this->msg( 
'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
-                               "</td>\n" .
-                               "</tr>\n" .
-                               Xml::closeElement( 'table' ) .
-                               Xml::closeElement( 'form' ) .
-                               Xml::closeElement( 'fieldset' ) . "\n"
-               );
-       }
-
-       /**
-        * @param array $fields
-        * @return string
-        */
-       protected function pretty( $fields ) {
-               $out = '';
-               foreach ( $fields as $list ) {
-                       list( $name, $label, $type, $value ) = $list;
-                       if ( $type == 'text' ) {
-                               $field = htmlspecialchars( $value );
-                       } else {
-                               $attribs = array( 'id' => $name );
-                               if ( $name == 'wpPassword' ) {
-                                       $attribs[] = 'autofocus';
-                               }
-                               $field = Html::input( $name, $value, $type, 
$attribs );
-                       }
-                       $out .= "<tr>\n";
-                       $out .= "\t<td class='mw-label'>";
-                       if ( $type != 'text' ) {
-                               $out .= Xml::label( $this->msg( $label 
)->text(), $name );
-                       } else {
-                               $out .= $this->msg( $label )->escaped();
-                       }
-                       $out .= "</td>\n";
-                       $out .= "\t<td class='mw-input'>";
-                       $out .= $field;
-                       $out .= "</td>\n";
-                       $out .= "</tr>";
-               }
-
-               return $out;
        }
 
        /**
         * @param User $user
         * @param string $pass
         * @param string $newaddr
-        * @return bool|string True or string on success, false on failure
+        * @return Status
         */
        protected function attemptChange( User $user, $pass, $newaddr ) {
                global $wgAuth;
 
                if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
-                       $this->error( 'invalidemailaddress' );
-
-                       return false;
+                       return Status::newFatal( 'invalidemailaddress' );
                }
 
                $throttleCount = LoginForm::incLoginThrottle( $user->getName() 
);
                if ( $throttleCount === true ) {
                        $lang = $this->getLanguage();
                        $throttleInfo = $this->getConfig()->get( 
'PasswordAttemptThrottle' );
-                       $this->error( array(
+                       return Status::newFatal(
                                'changeemail-throttled',
                                $lang->formatDuration( $throttleInfo['seconds'] 
)
-                       ) );
-
-                       return false;
+                       );
                }
 
                if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' )
                        && !$user->checkTemporaryPassword( $pass )
                        && !$user->checkPassword( $pass )
                ) {
-                       $this->error( 'wrongpassword' );
-
-                       return false;
+                       return Status::newFatal( 'wrongpassword' );
                }
 
                if ( $throttleCount ) {
@@ -256,12 +181,7 @@
                $oldaddr = $user->getEmail();
                $status = $user->setEmailWithConfirmation( $newaddr );
                if ( !$status->isGood() ) {
-                       $this->getOutput()->addHTML(
-                               '<p class="error">' .
-                                       $this->getOutput()->parseInline( 
$status->getWikiText( 'mailerror' ) ) .
-                                       '</p>' );
-
-                       return false;
+                       return $status;
                }
 
                wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr 
) );
@@ -270,7 +190,11 @@
 
                $wgAuth->updateExternalDB( $user );
 
-               return $status->value;
+               return $status;
+       }
+
+       public function requiresUnblock() {
+               return false;
        }
 
        protected function getGroupName() {

-- 
To view, visit https://gerrit.wikimedia.org/r/142878
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I629cee4264fad6cde98495c0e8daffe5ea245b48
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: IAlex <coderev...@emsenhuber.ch>
Gerrit-Reviewer: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: IAlex <coderev...@emsenhuber.ch>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Parent5446 <tylerro...@gmail.com>
Gerrit-Reviewer: Reedy <re...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to