From fde755c089e0da51cdf26430dcf776d3fe190719 Mon Sep 17 00:00:00 2001 From: Douglas Gardner Date: Sat, 16 Aug 2014 03:01:48 +0100 Subject: [PATCH] Use FormSpecialPage for Special:RandomInCategory This commit moves [[Special:RandomInCategory]] from using the SpecialPage class to using the FormSpecialPage class, in the process moving from a simple input box in the middle of a sentence to a "proper" form. Bug: 67581 Bug: 67253 Change-Id: Ibf8694c0643f7cd3964ff9f62ffc6049f0978416 --- includes/specials/SpecialRandomInCategory.php | 61 +++++++++++-------- languages/i18n/en.json | 4 +- languages/i18n/qqq.json | 4 +- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/includes/specials/SpecialRandomInCategory.php b/includes/specials/SpecialRandomInCategory.php index 28a1bc58b1..570ab3bf82 100644 --- a/includes/specials/SpecialRandomInCategory.php +++ b/includes/specials/SpecialRandomInCategory.php @@ -46,7 +46,7 @@ * * @ingroup SpecialPage */ -class SpecialRandomInCategory extends SpecialPage { +class SpecialRandomInCategory extends FormSpecialPage { protected $extra = array(); // Extra SQL statements protected $category = false; // Title object of category protected $maxOffset = 30; // Max amount to fudge randomness by. @@ -67,10 +67,35 @@ class SpecialRandomInCategory extends SpecialPage { $this->minTimestamp = null; } - public function execute( $par ) { + protected function getFormFields() { + $form = array( + 'category' => array( + 'type' => 'text', + 'label-message' => 'randomincategory-category', + 'required' => true, + ) + ); + + return $form; + } + + public function requiresWrite() { + return false; + } + + public function requiresUnblock() { + return false; + } + + protected function setParameter( $par ) { + // if subpage present, fake form submission + $this->onSubmit( array( 'category' => $par ) ); + } + + public function onSubmit( array $data ) { $cat = false; - $categoryStr = $this->getRequest()->getText( 'category', $par ); + $categoryStr = $data['category']; if ( $categoryStr ) { $cat = Title::newFromText( $categoryStr, NS_CATEGORY ); @@ -86,41 +111,25 @@ class SpecialRandomInCategory extends SpecialPage { } if ( !$this->category && $categoryStr ) { - $this->setHeaders(); - $this->getOutput()->addWikiMsg( 'randomincategory-invalidcategory', + $msg = $this->msg( 'randomincategory-invalidcategory', wfEscapeWikiText( $categoryStr ) ); - return; + return Status::newFatal( $msg ); + } elseif ( !$this->category ) { - $this->setHeaders(); - $input = Html::input( 'category' ); - $submitText = $this->msg( 'randomincategory-selectcategory-submit' )->text(); - $submit = Html::input( '', $submitText, 'submit' ); - - $msg = $this->msg( 'randomincategory-selectcategory' ); - $form = Html::rawElement( 'form', array( 'action' => wfScript() ), - Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . - $msg->rawParams( $input, $submit )->parse() - ); - $this->getOutput()->addHtml( $form ); - - return; + return; // no data sent } $title = $this->getRandomTitle(); if ( is_null( $title ) ) { - $this->setHeaders(); - $this->getOutput()->addWikiMsg( 'randomincategory-nopages', + $msg = $this->msg( 'randomincategory-nopages', $this->category->getText() ); - return; + return Status::newFatal( $msg ); } - $query = $this->getRequest()->getValues(); - unset( $query['title'] ); - unset( $query['category'] ); - $this->getOutput()->redirect( $title->getFullURL( $query ) ); + $this->getOutput()->redirect( $title->getFullURL() ); } /** diff --git a/languages/i18n/en.json b/languages/i18n/en.json index a43a7421a0..2af3d3fc78 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -1520,8 +1520,8 @@ "randomincategory": "Random page in category", "randomincategory-invalidcategory": "\"$1\" is not a valid category name.", "randomincategory-nopages": "There are no pages in the [[:Category:$1|$1]] category.", - "randomincategory-selectcategory": "Get random page from category: $1 $2.", - "randomincategory-selectcategory-submit": "Go", + "randomincategory-category": "Category:", + "randomincategory-legend": "Random page in category", "randomredirect": "Random redirect", "randomredirect-nopages": "There are no redirects in the namespace \"$1\".", "statistics": "Statistics", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index 74e295432b..e1967f8fd5 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -1682,8 +1682,8 @@ "randomincategory": "{{doc-special|RandomInCategory}}", "randomincategory-invalidcategory": "Message shown if an invalid category is specified.\n\nParameters:\n* $1 - the invalid category name given\n\n(Note, if the category is simply empty, but could possibly exist, {{msg-mw|randomincategory-nopages}} is shown instead)", "randomincategory-nopages": "Message shown from [[Special:RandomInCategory]] if the category is empty.\n\nParameters:\n* $1 - the category name (without the namespace prefix)", - "randomincategory-selectcategory": "Shown on [[Special:RandomInCategory]] if no category is selected.\n\nDisplays a form allowing the user to input a category name.\n\nParameters:\n* $1 - the text field input box\n* $2 - the go button which has the label {{msg-mw|randomincategory-selectcategory-submit}}.", - "randomincategory-selectcategory-submit": "Used as label for the Submit button which is used as $2 in {{msg-mw|Randomincategory-selectcategory}}.\n{{Identical|Go}}", + "randomincategory-category": "Shown on [[Special:RandomInCategory]] as a label for a text box allowing the user to input a category name.", + "randomincategory-legend": "Legend to the form on [[Special:RandomInCategory]]. {{identical|randomicategory}}", "randomredirect": "{{doc-special|RandomRedirect}}", "randomredirect-nopages": "Parameters:\n* $1 - list of namespaces\n* $2 - (Unused) number of namespaces\nSee also:\n* {{msg-mw|Randompage-nopages}}", "statistics": "{{doc-special|Statistics}}\n{{Identical|Statistics}}", -- 2.20.1