Florianschmidtwelzow has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/300563

Change subject: Convert Special:EmailUser to use OOUIHTMLForm
......................................................................

Convert Special:EmailUser to use OOUIHTMLForm

It already used HTMLForm for the main form, but the form, which asked for
the username didn't.

Converted the "Enter username" form to use HTMLForm and show it, if no user-
name was passed to the form, show the main "Send e-mail" form, instead.

Extra points:
 - Let HTMLForm::setSubmit*() function return it's own HTMLForm instance

This basically is a squashed version of these changes:
I6231577047c93781496e0f8af6809e2ef49e662a
I3e0c02155428ae400bc3a6d3ed2e66e69ee441fa

Bug: T117791
Change-Id: If8bf42b2bd092706f2e580083b2400121d35c41c
---
M includes/specials/SpecialEmailuser.php
1 file changed, 86 insertions(+), 83 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/63/300563/1

diff --git a/includes/specials/SpecialEmailuser.php 
b/includes/specials/SpecialEmailuser.php
index fb1943f..33459db 100644
--- a/includes/specials/SpecialEmailuser.php
+++ b/includes/specials/SpecialEmailuser.php
@@ -89,7 +89,6 @@
                        'Text' => [
                                'type' => 'textarea',
                                'rows' => 20,
-                               'cols' => 80,
                                'label-message' => 'emailmessage',
                                'required' => true,
                        ],
@@ -103,11 +102,15 @@
 
        public function execute( $par ) {
                $out = $this->getOutput();
+               $request = $this->getRequest();
                $out->addModuleStyles( 'mediawiki.special' );
 
                $this->mTarget = is_null( $par )
-                       ? $this->getRequest()->getVal( 'wpTarget', 
$this->getRequest()->getVal( 'target', '' ) )
+                       ? $request->getVal( 'wpTarget', $request->getVal( 
'target', '' ) )
                        : $par;
+
+               // make sure, that HTMLForm uses the correct target
+               $request->setVal( 'wpTarget', $this->mTarget );
 
                // This needs to be below assignment of $this->mTarget because
                // getDescription() needs it to determine the correct page 
title.
@@ -139,45 +142,22 @@
                                list( $title, $msg, $params ) = $error;
                                throw new ErrorPageError( $title, $msg, $params 
);
                }
-               // Got a valid target user name? Else ask for one.
-               $ret = self::getTarget( $this->mTarget );
-               if ( !$ret instanceof User ) {
-                       if ( $this->mTarget != '' ) {
-                               // Messages used here: notargettext, 
noemailtext, nowikiemailtext
-                               $ret = ( $ret == 'notarget' ) ? 'emailnotarget' 
: ( $ret . 'text' );
-                               $out->wrapWikiMsg( "<p class='error'>$1</p>", 
$ret );
-                       }
-                       $out->addHTML( $this->userForm( $this->mTarget ) );
 
-                       return;
-               }
-
-               $this->mTargetObj = $ret;
-
-               // Set the 'relevant user' in the skin, so it displays links 
like Contributions,
-               // User logs, UserRights, etc.
-               $this->getSkin()->setRelevantUser( $this->mTargetObj );
-
+               // make sure, that a submitted form isn't submitted to a 
subpage (which could be
+               // a non-existing username)
                $context = new DerivativeContext( $this->getContext() );
                $context->setTitle( $this->getPageTitle() ); // Remove subpage
-               $form = new HTMLForm( $this->getFormFields(), $context );
-               // By now we are supposed to be sure that $this->mTarget is a 
user name
-               $form->addPreText( $this->msg( 'emailpagetext', $this->mTarget 
)->parse() );
-               $form->setSubmitTextMsg( 'emailsend' );
-               $form->setSubmitCallback( [ __CLASS__, 'uiSubmit' ] );
-               $form->setWrapperLegendMsg( 'email-legend' );
-               $form->loadData();
+               $this->setContext( $context );
 
-               if ( !Hooks::run( 'EmailUserForm', [ &$form ] ) ) {
-                       return;
-               }
-
-               $result = $form->show();
-
-               if ( $result === true || ( $result instanceof Status && 
$result->isGood() ) ) {
-                       $out->setPageTitle( $this->msg( 'emailsent' ) );
-                       $out->addWikiMsg( 'emailsenttext', $this->mTarget );
-                       $out->returnToMain( false, 
$this->mTargetObj->getUserPage() );
+               // a little hack: HTMLForm will check $this->mTarget only, if 
the form was posted, not
+               // if the user opens Special:EmailUser/Florian (e.g.). So 
check, if the user did that
+               // and show the "Send email to user" form directly, if so. Show 
the "enter username"
+               // form, otherwise.
+               $this->mTargetObj = self::getTarget( $this->mTarget );
+               if ( !$this->mTargetObj instanceof User ) {
+                       $this->userForm( $this->mTarget );
+               } else {
+                       $this->sendEmailForm();
                }
        }
 
@@ -268,47 +248,61 @@
         * @return string Form asking for user name.
         */
        protected function userForm( $name ) {
-               $this->getOutput()->addModules( 'mediawiki.userSuggest' );
-               $string = Html::openElement(
-                               'form',
-                               [ 'method' => 'get', 'action' => wfScript(), 
'id' => 'askusername' ]
-                       ) .
-                       Html::hidden( 'title', 
$this->getPageTitle()->getPrefixedText() ) .
-                       Html::openElement( 'fieldset' ) .
-                       Html::rawElement( 'legend', null, $this->msg( 
'emailtarget' )->parse() ) .
-                       Html::label(
-                               $this->msg( 'emailusername' )->text(),
-                               'emailusertarget'
-                       ) . '&#160;' .
-                       Html::input(
-                               'target',
-                               $name,
-                               'text',
-                               [
-                                       'id' => 'emailusertarget',
-                                       'class' => 'mw-autocomplete-user',  // 
used by mediawiki.userSuggest
-                                       'autofocus' => true,
-                                       'size' => 30,
-                               ]
-                       ) .
-                       ' ' .
-                       Html::submitButton( $this->msg( 'emailusernamesubmit' 
)->text(), [] ) .
-                       Html::closeElement( 'fieldset' ) .
-                       Html::closeElement( 'form' ) . "\n";
+               $form = HTMLForm::factory( 'ooui', [
+                       'Target' => [
+                               'type' => 'user',
+                               'exists' => true,
+                               'label' => $this->msg( 'emailusername' 
)->text(),
+                               'id' => 'emailusertarget',
+                               'autofocus' => true,
+                               'value' => $name,
+                       ]
+               ], $this->getContext() );
 
-               return $string;
+               $form
+                       ->setMethod( 'post' )
+                       ->setSubmitCallback( array( $this, 'sendEmailForm' ) )
+                       ->setSubmitProgressive()
+                       ->setId( 'askusername' )
+                       ->setWrapperLegendMsg( 'emailtarget' )
+                       ->setSubmitTextMsg( 'emailusernamesubmit' )
+                       ->show();
        }
 
-       /**
-        * Submit callback for an HTMLForm object, will simply call submit().
-        *
-        * @since 1.20
-        * @param array $data
-        * @param HTMLForm $form
-        * @return Status|string|bool
-        */
-       public static function uiSubmit( array $data, HTMLForm $form ) {
-               return self::submit( $data, $form->getContext() );
+       public function sendEmailForm() {
+               $out = $this->getOutput();
+
+               $ret = $this->mTargetObj;
+               if ( !$ret instanceof User ) {
+                       if ( $this->mTarget != '' ) {
+                               // Messages used here: notargettext, 
noemailtext, nowikiemailtext
+                               $ret = ( $ret == 'notarget' ) ? 'emailnotarget' 
: ( $ret . 'text' );
+                               return Status::newFatal( $ret );
+                       }
+                       return false;
+               }
+
+               $form = HTMLForm::factory( 'ooui', $this->getFormFields(), 
$this->getContext() );
+               // By now we are supposed to be sure that $this->mTarget is a 
user name
+               $form
+                       ->addPreText( $this->msg( 'emailpagetext', 
$this->mTarget )->parse() )
+                       ->setSubmitTextMsg( 'emailsend' )
+                       ->setSubmitCallback( [ $this, 'submit' ] )
+                       ->setWrapperLegendMsg( 'email-legend' )
+                       ->loadData();
+
+               if ( !Hooks::run( 'EmailUserForm', [ &$form ] ) ) {
+                       return false;
+               }
+
+               $result = $form->show();
+
+               if ( $result === true || ( $result instanceof Status && 
$result->isGood() ) ) {
+                       $out->setPageTitle( $this->msg( 'emailsent' ) );
+                       $out->addWikiMsg( 'emailsenttext', $this->mTarget );
+                       $out->returnToMain( false, $ret->getUserPage() );
+               }
+               return true;
        }
 
        /**
@@ -317,27 +311,36 @@
         * idea to check the edit token and ping limiter in advance.
         *
         * @param array $data
-        * @param IContextSource $context
+        * @param HTMLForm $form
         * @return Status|string|bool Status object, or potentially a String on 
error
         * or maybe even true on success if anything uses the EmailUser hook.
         */
-       public static function submit( array $data, IContextSource $context ) {
-               $config = $context->getConfig();
+       public function submit( array $data, HTMLForm $form ) {
+               $config = $this->getConfig();
 
                $target = self::getTarget( $data['Target'] );
                if ( !$target instanceof User ) {
                        // Messages used here: notargettext, noemailtext, 
nowikiemailtext
-                       return $context->msg( $target . 'text' 
)->parseAsBlock();
+                       return $this->msg( $target . 'text' )->parseAsBlock();
                }
 
                $to = MailAddress::newFromUser( $target );
-               $from = MailAddress::newFromUser( $context->getUser() );
+               $from = MailAddress::newFromUser( $this->getUser() );
                $subject = $data['Subject'];
                $text = $data['Text'];
 
+               // e-mail content and subject are required fields, and the user 
shouldn't be
+               // able to sent the form without it (in most modern browsers, 
except =< IE9). Don't
+               // add an error message, the fields are already marked as 
required and the browser should
+               // reject to submit the form without it. This also makes sure, 
that the form doesn't try
+               // to send an e-mail, when the user submits the user selection 
form.
+               if ( !$text || !$subject ) {
+                       return;
+               }
+
                // Add a standard footer and trim up trailing newlines
                $text = rtrim( $text ) . "\n\n-- \n";
-               $text .= $context->msg( 'emailuserfooter',
+               $text .= $this->msg( 'emailuserfooter',
                        $from->name, $to->name )->inContentLanguage()->text();
 
                $error = '';
@@ -355,7 +358,7 @@
                         * SPF and bounce problems with some mailers (see 
below).
                         */
                        $mailFrom = new MailAddress( $config->get( 
'PasswordSender' ),
-                               wfMessage( 'emailsender' 
)->inContentLanguage()->text() );
+                               $this->msg( 'emailsender' 
)->inContentLanguage()->text() );
                        $replyTo = $from;
                } else {
                        /**
@@ -388,7 +391,7 @@
                        // unless they are emailing themselves, in which case 
one
                        // copy of the message is sufficient.
                        if ( $data['CCMe'] && $to != $from ) {
-                               $cc_subject = $context->msg( 'emailccsubject' 
)->rawParams(
+                               $cc_subject = $this->msg( 'emailccsubject' 
)->rawParams(
                                        $target->getName(), $subject )->text();
 
                                // target and sender are equal, because this is 
the CC for the sender

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If8bf42b2bd092706f2e580083b2400121d35c41c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.stargatewis...@gmail.com>

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

Reply via email to