From b1ff754dd7889b757fa280d71905add34b7e566f Mon Sep 17 00:00:00 2001 From: Florian Date: Sat, 28 Nov 2015 23:57:30 +0100 Subject: [PATCH] Convert Special:ExpandTemplates to OOUI 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 | 15 +++ includes/specials/SpecialExpandTemplates.php | 135 ++++++++++--------- languages/i18n/en.json | 1 + languages/i18n/qqq.json | 1 + 4 files changed, 89 insertions(+), 63 deletions(-) diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index d75bdb3366..5f05b06bfb 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -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. diff --git a/includes/specials/SpecialExpandTemplates.php b/includes/specials/SpecialExpandTemplates.php index 06eb276944..17c224c2ff 100644 --- a/includes/specials/SpecialExpandTemplates.php +++ b/includes/specials/SpecialExpandTemplates.php @@ -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 .= "
" . $this->msg( 'expandtemplates' )->escaped() . "\n"; - - $form .= '

' . Xml::inputLabel( - $this->msg( 'expand_templates_title' )->plain(), - 'wpContextTitle', - 'contexttitle', - 60, - $title, - array( 'autofocus' => '', 'class' => 'mw-ui-input-inline' ) - ) . '

'; - $form .= '

' . Xml::label( - $this->msg( 'expand_templates_input' )->text(), - 'input' - ) . '

'; - $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 .= '

' . Xml::checkLabel( - $this->msg( 'expand_templates_remove_comments' )->text(), - 'wpRemoveComments', - 'removecomments', - $this->removeComments - ) . '

'; - $form .= '

' . Xml::checkLabel( - $this->msg( 'expand_templates_remove_nowiki' )->text(), - 'wpRemoveNowiki', - 'removenowiki', - $this->removeNowiki - ) . '

'; - $form .= '

' . Xml::checkLabel( - $this->msg( 'expand_templates_generate_xml' )->text(), - 'wpGenerateXml', - 'generate_xml', - $this->generateXML - ) . '

'; - $form .= '

' . Xml::checkLabel( - $this->msg( 'expand_templates_generate_rawhtml' )->text(), - 'wpGenerateRawHtml', - 'generate_rawhtml', - $this->generateRawHtml - ) . '

'; - $form .= '

' . Xml::submitButton( - $this->msg( 'expand_templates_ok' )->text(), - array( 'accesskey' => 's' ) - ) . '

'; - $form .= "
\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(); } /** diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 2dbd31ba73..55bb36307b 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -3843,6 +3843,7 @@ "expand_templates_preview": "Preview", "expand_templates_preview_fail_html": "Because {{SITENAME}} has raw HTML enabled and there was a loss of session data, the preview is hidden as a precaution against JavaScript attacks.\n\nIf this is a legitimate preview attempt, please try again.\nIf it still does not work, try [[Special:UserLogout|logging out]] and logging back in.", "expand_templates_preview_fail_html_anon": "Because {{SITENAME}} has raw HTML enabled and you are not logged in, the preview is hidden as a precaution against JavaScript attacks.\n\nIf this is a legitimate preview attempt, please [[Special:UserLogin|log in]] and try again.", + "expand_templates_input_missing": "You need to provide at least some input text.", "pagelanguage": "Page language selector", "pagelang-name": "Page", "pagelang-language": "Language", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index 5a16a4ccf8..ac7bab1328 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -4018,6 +4018,7 @@ "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}}", -- 2.20.1