r96828 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96827‎ | r96828 | r96829 >
Date:08:49, 12 September 2011
Author:ialex
Status:ok (Comments)
Tags:
Comment:
* Use local context instead of global variables (where possible)
* Made getFormFields() non-static since the only call to it is in that file
Modified paths:
  • /trunk/phase3/includes/specials/SpecialBlock.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialBlock.php
@@ -56,10 +56,8 @@
5757 }
5858
5959 public function execute( $par ) {
60 - global $wgUser, $wgOut, $wgRequest;
61 -
6260 # Permission check
63 - if( !$this->userCanExecute( $wgUser ) ) {
 61+ if( !$this->userCanExecute( $this->getUser() ) ) {
6462 $this->displayRestrictionError();
6563 return;
6664 }
@@ -72,15 +70,16 @@
7371 # Extract variables from the request. Try not to get into a situation where we
7472 # need to extract *every* variable from the form just for processing here, but
7573 # there are legitimate uses for some variables
76 - list( $this->target, $this->type ) = self::getTargetAndType( $par, $wgRequest );
 74+ $request = $this->getRequest();
 75+ list( $this->target, $this->type ) = self::getTargetAndType( $par, $request );
7776 if ( $this->target instanceof User ) {
7877 # Set the 'relevant user' in the skin, so it displays links like Contributions,
7978 # User logs, UserRights, etc.
8079 $this->getSkin()->setRelevantUser( $this->target );
8180 }
8281
83 - list( $this->previousTarget, /*...*/ ) = Block::parseTarget( $wgRequest->getVal( 'wpPreviousTarget' ) );
84 - $this->requestedHideUser = $wgRequest->getBool( 'wpHideUser' );
 82+ list( $this->previousTarget, /*...*/ ) = Block::parseTarget( $request->getVal( 'wpPreviousTarget' ) );
 83+ $this->requestedHideUser = $request->getBool( 'wpHideUser' );
8584
8685 # bug 15810: blocked admins should have limited access here
8786 $status = self::checkUnblockSelf( $this->target );
@@ -88,10 +87,11 @@
8988 throw new ErrorPageError( 'badaccess', $status );
9089 }
9190
92 - $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
93 - $wgOut->addModules( 'mediawiki.special', 'mediawiki.special.block' );
 91+ $out = $this->getOutput();
 92+ $out->setPageTitle( wfMsg( 'blockip-title' ) );
 93+ $out->addModules( 'mediawiki.special', 'mediawiki.special.block' );
9494
95 - $fields = self::getFormFields();
 95+ $fields = $this->getFormFields();
9696 $this->maybeAlterFormDefaults( $fields );
9797
9898 $form = new HTMLForm( $fields, $this->getContext() );
@@ -108,8 +108,8 @@
109109 $this->doPostText( $form );
110110
111111 if( $form->show() ){
112 - $wgOut->setPageTitle( wfMsg( 'blockipsuccesssub' ) );
113 - $wgOut->addWikiMsg( 'blockipsuccesstext', $this->target );
 112+ $out->setPageTitle( wfMsg( 'blockipsuccesssub' ) );
 113+ $out->addWikiMsg( 'blockipsuccesstext', $this->target );
114114 }
115115 }
116116
@@ -117,9 +117,11 @@
118118 * Get the HTMLForm descriptor array for the block form
119119 * @return Array
120120 */
121 - protected static function getFormFields(){
122 - global $wgUser, $wgBlockAllowsUTEdit;
 121+ protected function getFormFields(){
 122+ global $wgBlockAllowsUTEdit;
123123
 124+ $user = $this->getUser();
 125+
124126 $a = array(
125127 'Target' => array(
126128 'type' => 'text',
@@ -150,7 +152,7 @@
151153 ),
152154 );
153155
154 - if( self::canBlockEmail( $wgUser ) ) {
 156+ if( self::canBlockEmail( $user ) ) {
155157 $a['DisableEmail'] = array(
156158 'type' => 'check',
157159 'label-message' => 'ipbemailban',
@@ -172,7 +174,7 @@
173175 );
174176
175177 # Allow some users to hide name from block log, blocklist and listusers
176 - if( $wgUser->isAllowed( 'hideuser' ) ) {
 178+ if( $user->isAllowed( 'hideuser' ) ) {
177179 $a['HideUser'] = array(
178180 'type' => 'check',
179181 'label-message' => 'ipbhidename',
@@ -181,7 +183,7 @@
182184 }
183185
184186 # Watchlist their user page? (Only if user is logged in)
185 - if( $wgUser->isLoggedIn() ) {
 187+ if( $user->isLoggedIn() ) {
186188 $a['Watch'] = array(
187189 'type' => 'check',
188190 'label-message' => 'ipbwatchuser',
@@ -219,8 +221,6 @@
220222 * already blocked)
221223 */
222224 protected function maybeAlterFormDefaults( &$fields ){
223 - global $wgRequest, $wgUser;
224 -
225225 # This will be overwritten by request data
226226 $fields['Target']['default'] = (string)$this->target;
227227
@@ -252,7 +252,7 @@
253253
254254 $fields['Reason']['default'] = $block->mReason;
255255
256 - if( $wgRequest->wasPosted() ){
 256+ if( $this->getRequest()->wasPosted() ){
257257 # Ok, so we got a POST submission asking us to reblock a user. So show the
258258 # confirm checkbox; the user will only see it if they haven't previously
259259 $fields['Confirm']['type'] = 'check';
@@ -281,7 +281,7 @@
282282 }
283283
284284 # Or if the user is trying to block themselves
285 - if( (string)$this->target === $wgUser->getName() ){
 285+ if( (string)$this->target === $this->getUser()->getName() ){
286286 $fields['Confirm']['type'] = 'check';
287287 unset( $fields['Confirm']['default'] );
288288 $this->preErrors[] = 'ipb-blockingself';
@@ -331,10 +331,8 @@
332332 * @return void
333333 */
334334 protected function doHeaderText( HTMLForm &$form ){
335 - global $wgRequest;
336 -
337335 # Don't need to do anything if the form has been posted
338 - if( !$wgRequest->wasPosted() && $this->preErrors ){
 336+ if( !$this->getRequest()->wasPosted() && $this->preErrors ){
339337 $s = HTMLForm::formatErrors( $this->preErrors );
340338 if( $s ){
341339 $form->addHeaderText( Html::rawElement(
@@ -352,8 +350,6 @@
353351 * @return void
354352 */
355353 protected function doPostText( HTMLForm &$form ){
356 - global $wgUser, $wgLang;
357 -
358354 # Link to the user's contributions, if applicable
359355 if( $this->target instanceof User ){
360356 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->target->getName() );
@@ -379,8 +375,10 @@
380376 wfMsg( 'ipb-blocklist' )
381377 );
382378
 379+ $user = $this->getUser();
 380+
383381 # Link to edit the block dropdown reasons, if applicable
384 - if ( $wgUser->isAllowed( 'editinterface' ) ) {
 382+ if ( $user->isAllowed( 'editinterface' ) ) {
385383 $links[] = Linker::link(
386384 Title::makeTitle( NS_MEDIAWIKI, 'Ipbreason-dropdown' ),
387385 wfMsgHtml( 'ipb-edit-dropdown' ),
@@ -392,7 +390,7 @@
393391 $form->addPostText( Html::rawElement(
394392 'p',
395393 array( 'class' => 'mw-ipb-conveniencelinks' ),
396 - $wgLang->pipeList( $links )
 394+ $this->getLang()->pipeList( $links )
397395 ) );
398396
399397 if( $this->target instanceof User ){
@@ -414,7 +412,7 @@
415413 $form->addPostText( $out );
416414
417415 # Add suppression block entries if allowed
418 - if( $wgUser->isAllowed( 'suppressionlog' ) ) {
 416+ if( $user->isAllowed( 'suppressionlog' ) ) {
419417 LogEventsList::showLogExtract(
420418 $out,
421419 'suppress',

Comments

#Comment by Aaron Schulz (talk | contribs)   21:04, 28 September 2011

Why did you make getFormFields() dynamic? Why not just pass in a $user object. Perhaps someone might want to refactor this function for more general use outside the special page.

#Comment by IAlex (talk | contribs)   15:12, 29 September 2011

It does not only depend on the User object since there's a call to wfMsg() inside it. I did not modify that call yet but I will get there one day.

#Comment by Aaron Schulz (talk | contribs)   15:46, 29 September 2011

Why does HTMLForm need the msg text and not just the key for 'other'? The other items are just passed messages keys. Maybe HTMLForm could be refactored here...possibly, I'll have to look more.

Status & tagging log