r103123 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103122‎ | r103123 | r103124 >
Date:02:25, 15 November 2011
Author:johnduhart
Status:ok
Tags:
Comment:
Convert SpecialBlock to subclass FormSpecialPage

Also fixes the problem introduced in r103115
Modified paths:
  • /trunk/phase3/includes/specials/SpecialBlock.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialBlock.php
@@ -27,7 +27,7 @@
2828 *
2929 * @ingroup SpecialPage
3030 */
31 -class SpecialBlock extends SpecialPage {
 31+class SpecialBlock extends FormSpecialPage {
3232 /** The maximum number of edits a user can have and still be hidden
3333 * TODO: config setting? */
3434 const HIDEUSER_CONTRIBLIMIT = 1000;
@@ -55,10 +55,28 @@
5656 parent::__construct( 'Block', 'block' );
5757 }
5858
59 - public function execute( $par ) {
60 - $this->checkPermissions();
61 - $this->checkReadOnly();
 59+ /**
 60+ * Checks that the user can unblock themselves if they are trying to do so
 61+ *
 62+ * @param User $user
 63+ * @throws ErrorPageError
 64+ */
 65+ protected function checkExecutePermissions( User $user ) {
 66+ parent::checkExecutePermissions( $user );
6267
 68+ # bug 15810: blocked admins should have limited access here
 69+ $status = self::checkUnblockSelf( $this->target, $user );
 70+ if ( $status !== true ) {
 71+ throw new ErrorPageError( 'badaccess', $status );
 72+ }
 73+ }
 74+
 75+ /**
 76+ * Handle some magic here
 77+ *
 78+ * @param $par String
 79+ */
 80+ protected function setParameter( $par ) {
6381 # Extract variables from the request. Try not to get into a situation where we
6482 # need to extract *every* variable from the form just for processing here, but
6583 # there are legitimate uses for some variables
@@ -72,39 +90,31 @@
7391
7492 list( $this->previousTarget, /*...*/ ) = Block::parseTarget( $request->getVal( 'wpPreviousTarget' ) );
7593 $this->requestedHideUser = $request->getBool( 'wpHideUser' );
 94+ }
7695
77 - # bug 15810: blocked admins should have limited access here
78 - $status = self::checkUnblockSelf( $this->target, $user );
79 - if ( $status !== true ) {
80 - throw new ErrorPageError( 'badaccess', $status );
81 - }
82 -
83 - $this->setHeaders();
84 - $this->outputHeader();
85 -
86 - $out = $this->getOutput();
87 - $out->setPageTitle( $this->msg( 'blockip-title' ) );
88 - $out->addModules( array( 'mediawiki.special', 'mediawiki.special.block' ) );
89 -
90 - $fields = $this->getFormFields();
91 - $this->maybeAlterFormDefaults( $fields );
92 -
93 - $form = new HTMLForm( $fields, $this->getContext() );
94 - $form->setWrapperLegend( wfMsg( 'blockip-legend' ) );
 96+ /**
 97+ * Customizes the HTMLForm a bit
 98+ *
 99+ * @param $form HTMLForm
 100+ */
 101+ protected function alterForm( HTMLForm $form ) {
 102+ $form->setWrapperLegendMsg( 'blockip-legend' );
 103+ $form->setHeaderText( '' );
95104 $form->setSubmitCallback( array( __CLASS__, 'processUIForm' ) );
96105
97 - $t = $this->alreadyBlocked
98 - ? wfMsg( 'ipb-change-block' )
99 - : wfMsg( 'ipbsubmit' );
100 - $form->setSubmitText( $t );
 106+ $msg = $this->alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit';
 107+ $form->setSubmitTextMsg( $msg );
101108
102 - $this->doPreText( $form );
103 - $this->doHeadertext( $form );
104 - $this->doPostText( $form );
105 -
106 - if( $form->show() ){
107 - $out->setPageTitle( $this->msg( 'blockipsuccesssub' ) );
108 - $out->addWikiMsg( 'blockipsuccesstext', $this->target );
 109+ # Don't need to do anything if the form has been posted
 110+ if( !$this->getRequest()->wasPosted() && $this->preErrors ){
 111+ $s = HTMLForm::formatErrors( $this->preErrors );
 112+ if( $s ){
 113+ $form->addHeaderText( Html::rawElement(
 114+ 'div',
 115+ array( 'class' => 'error' ),
 116+ $s
 117+ ) );
 118+ }
109119 }
110120 }
111121
@@ -205,13 +215,15 @@
206216 'label-message' => 'ipb-confirm',
207217 );
208218
 219+ $this->maybeAlterFormDefaults( $a );
 220+
209221 return $a;
210222 }
211223
212224 /**
213225 * If the user has already been blocked with similar settings, load that block
214226 * and change the defaults for the form fields to match the existing settings.
215 - * @param &$fields Array HTMLForm descriptor array
 227+ * @param $fields Array HTMLForm descriptor array
216228 * @return Bool whether fields were altered (that is, whether the target is
217229 * already blocked)
218230 */
@@ -285,11 +297,10 @@
286298
287299 /**
288300 * Add header elements like block log entries, etc.
289 - * @param $form HTMLForm
290301 * @return void
291302 */
292 - protected function doPreText( HTMLForm &$form ){
293 - $form->addPreText( wfMsgExt( 'blockiptext', 'parse' ) );
 303+ protected function preText(){
 304+ $text = $this->msg( 'blockiptext' )->parse();
294305
295306 $otherBlockMessages = array();
296307 if( $this->target !== null ) {
@@ -315,36 +326,18 @@
316327 $list
317328 ) . "\n";
318329
319 - $form->addPreText( $s );
 330+ $text .= $s;
320331 }
321332 }
322 - }
323333
324 - /**
325 - * Add header text inside the form, just underneath where the errors would go
326 - * @param $form HTMLForm
327 - * @return void
328 - */
329 - protected function doHeaderText( HTMLForm &$form ){
330 - # Don't need to do anything if the form has been posted
331 - if( !$this->getRequest()->wasPosted() && $this->preErrors ){
332 - $s = HTMLForm::formatErrors( $this->preErrors );
333 - if( $s ){
334 - $form->addHeaderText( Html::rawElement(
335 - 'div',
336 - array( 'class' => 'error' ),
337 - $s
338 - ) );
339 - }
340 - }
 334+ return $text;
341335 }
342336
343337 /**
344338 * Add footer elements to the form
345 - * @param $form HTMLForm
346339 * @return void
347340 */
348 - protected function doPostText( HTMLForm &$form ){
 341+ protected function postText(){
349342 # Link to the user's contributions, if applicable
350343 if( $this->target instanceof User ){
351344 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->target->getName() );
@@ -382,11 +375,11 @@
383376 );
384377 }
385378
386 - $form->addPostText( Html::rawElement(
 379+ $text = Html::rawElement(
387380 'p',
388381 array( 'class' => 'mw-ipb-conveniencelinks' ),
389382 $this->getLang()->pipeList( $links )
390 - ) );
 383+ );
391384
392385 if( $this->target instanceof User ){
393386 # Get relevant extracts from the block and suppression logs, if possible
@@ -404,7 +397,7 @@
405398 'showIfEmpty' => false
406399 )
407400 );
408 - $form->addPostText( $out );
 401+ $text .= $out;
409402
410403 # Add suppression block entries if allowed
411404 if( $user->isAllowed( 'suppressionlog' ) ) {
@@ -421,9 +414,11 @@
422415 )
423416 );
424417
425 - $form->addPostText( $out );
 418+ $text .= $out;
426419 }
427420 }
 421+
 422+ return $text;
428423 }
429424
430425 /**
@@ -482,6 +477,7 @@
483478 * @since 1.18
484479 * @param $value String
485480 * @param $alldata Array
 481+ * @param $form HTMLForm
486482 * @return Message
487483 */
488484 public static function validateTargetField( $value, $alldata, $form ) {
@@ -536,6 +532,9 @@
537533
538534 /**
539535 * Submit callback for an HTMLForm object, will simply pass
 536+ * @param $data array
 537+ * @param $form HTMLForm
 538+ * @return Bool|String
540539 */
541540 public static function processUIForm( array $data, HTMLForm $form ) {
542541 return self::processForm( $data, $form->getContext() );
@@ -544,6 +543,7 @@
545544 /**
546545 * Given the form data, actually implement a block
547546 * @param $data Array
 547+ * @param $context IContextSource
548548 * @return Bool|String
549549 */
550550 public static function processForm( array $data, IContextSource $context ){
@@ -867,6 +867,26 @@
868868
869869 return implode( ',', $flags );
870870 }
 871+
 872+ /**
 873+ * Process the form on POST submission.
 874+ * @param $data Array
 875+ * @return Bool|Array true for success, false for didn't-try, array of errors on failure
 876+ */
 877+ public function onSubmit( array $data ) {
 878+ // This isn't used since we need that HTMLForm that's passed in the
 879+ // second parameter. See alterForm for the real function
 880+ }
 881+
 882+ /**
 883+ * Do something exciting on successful processing of the form, most likely to show a
 884+ * confirmation message
 885+ */
 886+ public function onSuccess() {
 887+ $out = $this->getOutput();
 888+ $out->setPageTitle( $this->msg( 'blockipsuccesssub' ) );
 889+ $out->addWikiMsg( 'blockipsuccesstext', $this->target );
 890+ }
871891 }
872892
873893 # BC @since 1.18

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r103115Updated core SpecialPages to use new functions introduced in r103111 and r103095johnduhart01:34, 15 November 2011

Status & tagging log