Convert Special:ExpandTemplates to OOUI
authorFlorian <florian.schmidt.stargatewissen@gmail.com>
Sat, 28 Nov 2015 22:57:30 +0000 (23:57 +0100)
committerReedy <reedy@wikimedia.org>
Sat, 2 Jan 2016 16:27:10 +0000 (16:27 +0000)
Extra points:
 * Add error message, when $input was not fiven (previous behaviour was, that
   the form was simply displayed again.

Bug: T117748
Change-Id: Ibe4e010fe9d0b2520c2d6964bd66cb2bca3b0bc7

includes/htmlform/HTMLForm.php
includes/specials/SpecialExpandTemplates.php
languages/i18n/en.json
languages/i18n/qqq.json

index d75bdb3..5f05b06 100644 (file)
@@ -526,6 +526,21 @@ class HTMLForm extends ContextSource {
                return false;
        }
 
+       /**
+        * Same as self::show with the difference, that the form will be
+        * added to the output, no matter, if the validation was good or not.
+        * @return bool|Status Whether submission was successful.
+        */
+       function showAlways() {
+               $this->prepareForm();
+
+               $result = $this->tryAuthorizedSubmit();
+
+               $this->displayForm( $result );
+
+               return $result;
+       }
+
        /**
         * Validate all the fields, and call the submission callback
         * function if everything is kosher.
index 06eb276..17c224c 100644 (file)
@@ -95,8 +95,8 @@ class SpecialExpandTemplates extends SpecialPage {
                }
 
                $out = $this->getOutput();
-               $out->addWikiMsg( 'expand_templates_intro' );
-               $out->addHTML( $this->makeForm( $titleStr, $input ) );
+
+               $this->makeForm( $titleStr, $input );
 
                if ( $output !== false ) {
                        if ( $this->generateXML && strlen( $output ) > 0 ) {
@@ -130,6 +130,22 @@ class SpecialExpandTemplates extends SpecialPage {
                }
        }
 
+       /**
+        * Callback for the HTMLForm used in self::makeForm.
+        * Checks, if the input was given, and if not, returns a fatal Status
+        * object with an error message.
+        *
+        * @param array $values The values submitted to the HTMLForm
+        * @return Status
+        */
+       public function onSubmitInput( array $values ) {
+               $status = Status::newGood();
+               if ( !strlen( $values['input'] ) ) {
+                       $status = Status::newFatal( 'expand_templates_input_missing' );
+               }
+               return $status;
+       }
+
        /**
         * Generate a form allowing users to enter information
         *
@@ -138,69 +154,62 @@ class SpecialExpandTemplates extends SpecialPage {
         * @return string
         */
        private function makeForm( $title, $input ) {
-               $self = $this->getPageTitle();
-               $request = $this->getRequest();
-               $user = $this->getUser();
-
-               $form = Xml::openElement(
-                       'form',
-                       array( 'method' => 'post', 'action' => $self->getLocalUrl() )
-               );
-               $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
-
-               $form .= '<p>' . Xml::inputLabel(
-                       $this->msg( 'expand_templates_title' )->plain(),
-                       'wpContextTitle',
-                       'contexttitle',
-                       60,
-                       $title,
-                       array( 'autofocus' => '', 'class' => 'mw-ui-input-inline' )
-               ) . '</p>';
-               $form .= '<p>' . Xml::label(
-                       $this->msg( 'expand_templates_input' )->text(),
-                       'input'
-               ) . '</p>';
-               $form .= Xml::textarea(
-                       'wpInput',
-                       $input,
-                       10,
-                       10,
-                       array( 'id' => 'input' )
+               $fields = array(
+                       'contexttitle' => array(
+                               'type' => 'text',
+                               'label' => $this->msg( 'expand_templates_title' )->plain(),
+                               'name' => 'wpContextTitle',
+                               'id' => 'contexttitle',
+                               'size' => 60,
+                               'default' => $title,
+                               'autofocus' => true,
+                               'cssclass' => 'mw-ui-input-inline',
+                       ),
+                       'input' => array(
+                               'type' => 'textarea',
+                               'name' => 'wpInput',
+                               'label-message' => 'expand_templates_input',
+                               'rows' => 10,
+                               'default' => $input,
+                               'id' => 'input',
+                       ),
+                       'removecomments' => array(
+                               'type' => 'check',
+                               'label-message' => 'expand_templates_remove_comments',
+                               'name' => 'wpRemoveComments',
+                               'id' => 'removecomments',
+                               'default' => $this->removeComments,
+                       ),
+                       'removenowiki' => array(
+                               'type' => 'check',
+                               'label-message' => 'expand_templates_remove_nowiki',
+                               'name' => 'wpRemoveNowiki',
+                               'id' => 'removenowiki',
+                               'default' => $this->removeNowiki,
+                       ),
+                       'generate_xml' => array(
+                               'type' => 'check',
+                               'label-message' => 'expand_templates_generate_xml',
+                               'name' => 'wpGenerateXml',
+                               'id' => 'generate_xml',
+                               'default' => $this->generateXML,
+                       ),
+                       'generate_rawhtml' => array(
+                               'type' => 'check',
+                               'label-message' => 'expand_templates_generate_rawhtml',
+                               'name' => 'wpGenerateRawHtml',
+                               'id' => 'generate_rawhtml',
+                               'default' => $this->generateRawHtml,
+                       ),
                );
 
-               $form .= '<p>' . Xml::checkLabel(
-                       $this->msg( 'expand_templates_remove_comments' )->text(),
-                       'wpRemoveComments',
-                       'removecomments',
-                       $this->removeComments
-               ) . '</p>';
-               $form .= '<p>' . Xml::checkLabel(
-                       $this->msg( 'expand_templates_remove_nowiki' )->text(),
-                       'wpRemoveNowiki',
-                       'removenowiki',
-                       $this->removeNowiki
-               ) . '</p>';
-               $form .= '<p>' . Xml::checkLabel(
-                       $this->msg( 'expand_templates_generate_xml' )->text(),
-                       'wpGenerateXml',
-                       'generate_xml',
-                       $this->generateXML
-               ) . '</p>';
-               $form .= '<p>' . Xml::checkLabel(
-                       $this->msg( 'expand_templates_generate_rawhtml' )->text(),
-                       'wpGenerateRawHtml',
-                       'generate_rawhtml',
-                       $this->generateRawHtml
-               ) . '</p>';
-               $form .= '<p>' . Xml::submitButton(
-                       $this->msg( 'expand_templates_ok' )->text(),
-                       array( 'accesskey' => 's' )
-               ) . '</p>';
-               $form .= "</fieldset>\n";
-               $form .= Html::hidden( 'wpEditToken', $user->getEditToken( '', $request ) );
-               $form .= Xml::closeElement( 'form' );
-
-               return $form;
+               $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
+               $form
+                       ->setSubmitTextMsg( 'expand_templates_ok' )
+                       ->setWrapperLegendMsg( 'expandtemplates' )
+                       ->setHeaderText( $this->msg( 'expand_templates_intro' )->text() )
+                       ->setSubmitCallback( array( $this, 'onSubmitInput' ) )
+                       ->showAlways();
        }
 
        /**
index 2dbd31b..55bb363 100644 (file)
        "expand_templates_preview": "Preview",
        "expand_templates_preview_fail_html": "<em>Because {{SITENAME}} has raw HTML enabled and there was a loss of session data, the preview is hidden as a precaution against JavaScript attacks.</em>\n\n<strong>If this is a legitimate preview attempt, please try again.</strong>\nIf it still does not work, try [[Special:UserLogout|logging out]] and logging back in.",
        "expand_templates_preview_fail_html_anon": "<em>Because {{SITENAME}} has raw HTML enabled and you are not logged in, the preview is hidden as a precaution against JavaScript attacks.</em>\n\n<strong>If this is a legitimate preview attempt, please [[Special:UserLogin|log in]] and try again.</strong>",
+       "expand_templates_input_missing": "You need to provide at least some input text.",
        "pagelanguage": "Page language selector",
        "pagelang-name": "Page",
        "pagelang-language": "Language",
index 5a16a4c..ac7bab1 100644 (file)
        "expand_templates_preview": "{{Identical|Preview}}",
        "expand_templates_preview_fail_html": "Used as error message in Preview section of [[Special:ExpandTemplates]] page.",
        "expand_templates_preview_fail_html_anon": "Used as error message in Preview section of [[Special:ExpandTemplates]] page.",
+       "expand_templates_input_missing": "Used on [[Special:ExpandTemplates]] as an error message, if no input text was provided.",
        "pagelanguage": "Title for page Special:PageLanguage",
        "pagelang-name": "Input label for page name on Special:PageLanguage\n{{Identical|Page}}",
        "pagelang-language": "Language selector label for Special:PageLanguage\n{{Identical|Language}}",