r29928 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r29927‎ | r29928 | r29929 >
Date:17:48, 18 January 2008
Author:catrope
Status:old
Tags:
Comment:
* Refactoring IPBlockForm::doBlock() to return message keys
* Refactoring ApiBlock accordingly
* Adding check for blockemail right to ApiBlock
* Adding more messages to ApiBase::$messageMap
* Fixing E_NOTICE in SpecialIpblocklist.php
Modified paths:
  • /trunk/phase3/includes/SpecialBlockip.php (modified) (history)
  • /trunk/phase3/includes/SpecialIpblocklist.php (modified) (history)
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiBlock.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialBlockip.php
@@ -282,17 +282,10 @@
283283 }
284284 }
285285
286 - const BLOCK_SUCCESS = 0; // Success
287 - const BLOCK_RANGE_INVALID = 1; // Invalid IP range
288 - const BLOCK_RANGE_DISABLED = 2; // Sysops can't block ranges
289 - const BLOCK_NONEXISTENT_USER = 3; // No such user
290 - const BLOCK_IP_INVALID = 4; // Invalid IP address
291 - const BLOCK_EXPIRY_INVALID = 5; // Invalid expiry time
292 - const BLOCK_ALREADY_BLOCKED = 6; // User is already blocked
293286 /**
294287 * Backend block code.
295288 * $userID and $expiry will be filled accordingly
296 - * Returns one of the BLOCK_* constants
 289+ * @return array(message key, arguments) on failure, empty array on success
297290 */
298291 function doBlock(&$userId = null, &$expiry = null)
299292 {
@@ -313,23 +306,23 @@
314307 # IPv4
315308 if ( $wgSysopRangeBans ) {
316309 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
317 - return self::BLOCK_RANGE_INVALID;
 310+ return array('ip_range_invalid');
318311 }
319312 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
320313 } else {
321314 # Range block illegal
322 - return self::BLOCK_RANGE_DISABLED;
 315+ return array('range_block_disabled');
323316 }
324317 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
325318 # IPv6
326319 if ( $wgSysopRangeBans ) {
327320 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
328 - return self::BLOCK_RANGE_INVALID;
 321+ return array('ip_range_invalid');
329322 }
330323 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
331324 } else {
332325 # Range block illegal
333 - return self::BLOCK_RANGE_DISABLED;
 326+ return array('range_block_disabled');
334327 }
335328 } else {
336329 # Username block
@@ -337,13 +330,12 @@
338331 $user = User::newFromName( $this->BlockAddress );
339332 if( !is_null( $user ) && $user->getID() ) {
340333 # Use canonical name
341 - $this->BlockAddress = $user->getName();
342334 $userId = $user->getID();
343335 } else {
344 - return self::BLOCK_NONEXISTENT_USER;
 336+ return array('nosuchusershort', htmlspecialchars($user->getName()));
345337 }
346338 } else {
347 - return self::BLOCK_IP_INVALID;
 339+ return array('badipaddress');
348340 }
349341 }
350342 }
@@ -361,7 +353,7 @@
362354 $expirestr = $this->BlockOther;
363355
364356 if (strlen($expirestr) == 0) {
365 - return self::BLOCK_EXPIRY_INVALID;
 357+ return array('ipb_expiry_invalid');
366358 }
367359
368360 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
@@ -371,7 +363,7 @@
372364 $expiry = strtotime( $expirestr );
373365
374366 if ( $expiry < 0 || $expiry === false ) {
375 - return self::BLOCK_EXPIRY_INVALID;
 367+ return array('ipb_expiry_invalid');
376368 }
377369
378370 $expiry = wfTimestamp( TS_MW, $expiry );
@@ -387,7 +379,7 @@
388380 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
389381
390382 if ( !$block->insert() ) {
391 - return self::BLOCK_ALREADY_BLOCKED;
 383+ return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress));
392384 }
393385
394386 wfRunHooks('BlockIpComplete', array($block, $wgUser));
@@ -404,8 +396,10 @@
405397 $reasonstr, $logParams );
406398
407399 # Report to the user
408 - return self::BLOCK_SUCCESS;
 400+ return array();
409401 }
 402+ else
 403+ return array('hookaborted');
410404 }
411405
412406 /**
@@ -416,34 +410,14 @@
417411 {
418412 global $wgOut;
419413 $retval = $this->doBlock();
420 - switch($retval)
421 - {
422 - case self::BLOCK_RANGE_INVALID:
423 - $this->showForm( wfMsg( 'ip_range_invalid' ) );
424 - return;
425 - case self::BLOCK_RANGE_DISABLED:
426 - $this->showForm( wfMsg( 'range_block_disabled' ) );
427 - return;
428 - case self::BLOCK_NONEXISTENT_USER:
429 - $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
430 - return;
431 - case self::BLOCK_IP_INVALID:
432 - $this->showForm( wfMsg( 'badipaddress' ) );
433 - return;
434 - case self::BLOCK_EXPIRY_INVALID:
435 - $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
436 - return;
437 - case self::BLOCK_ALREADY_BLOCKED:
438 - $this->showForm( wfMsg( 'ipb_already_blocked', htmlspecialchars( $this->BlockAddress ) ) );
439 - return;
440 - case self::BLOCK_SUCCESS:
441 - $titleObj = SpecialPage::getTitleFor( 'Blockip' );
442 - $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
443 - urlencode( $this->BlockAddress ) ) );
444 - return;
445 - default:
446 - throw new MWException( __METHOD__ . ": Unknown return value ``{$retval}''" );
 414+ if(empty($retval)) {
 415+ $titleObj = SpecialPage::getTitleFor( 'Blockip' );
 416+ $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
 417+ urlencode( $this->BlockAddress ) ) );
 418+ return;
447419 }
 420+ $key = array_shift($retval);
 421+ $this->showForm(wfMsgReal($key, $retval));
448422 }
449423
450424 function showSuccess() {
Index: trunk/phase3/includes/api/ApiBlock.php
@@ -61,21 +61,23 @@
6262 }
6363
6464 if(is_null($params['user']))
65 - $this->dieUsage('The user parameter must be set', 'nouser');
 65+ $this->dieUsageMsg(array('missingparam', 'user'));
6666 if(is_null($params['token']))
67 - $this->dieUsage('The token parameter must be set', 'notoken');
 67+ $this->dieUsageMsg(array('missingparam', 'token'));
6868 if(!$wgUser->matchEditToken($params['token']))
69 - $this->dieUsage('Invalid token', 'badtoken');
 69+ $this->dieUsageMsg(array('sessionfailure'));
7070 if(!$wgUser->isAllowed('block'))
71 - $this->dieUsage('You don\'t have permission to block users', 'permissiondenied');
 71+ $this->dieUsageMsg(array('cantblock'));
7272 if($params['hidename'] && !$wgUser->isAllowed('hideuser'))
73 - $this->dieUsage('You don\'t have permission to hide user names from the block log', 'nohide');
 73+ $this->dieUsageMsg(array('canthide'));
 74+ if($params['noemail'] && !$wgUser->isAllowed('blockemail'))
 75+ $this->dieUsageMsg(array('cantblock-email'));
7476 if(wfReadOnly())
75 - $this->dieUsage('The wiki is in read-only mode', 'readonly');
 77+ $this->dieUsageMsg(array('readonlytext'));
7678
7779 $form = new IPBlockForm('');
7880 $form->BlockAddress = $params['user'];
79 - $form->BlockReason = $params['reason'];
 81+ $form->BlockReason = (is_null($params['reason']) ? '' : $params['reason']);
8082 $form->BlockReasonList = 'other';
8183 $form->BlockExpiry = ($params['expiry'] == 'never' ? 'infinite' : $params['expiry']);
8284 $form->BlockOther = '';
@@ -88,27 +90,11 @@
8991 $dbw = wfGetDb(DB_MASTER);
9092 $dbw->begin();
9193 $retval = $form->doBlock($userID, $expiry);
92 - switch($retval)
93 - {
94 - case IPBlockForm::BLOCK_SUCCESS:
95 - break; // We'll deal with that later
96 - case IPBlockForm::BLOCK_RANGE_INVALID:
97 - $this->dieUsage("Invalid IP range ``{$params['user']}''", 'invalidrange');
98 - case IPBlockForm::BLOCK_RANGE_DISABLED:
99 - $this->dieUsage('Blocking IP ranges has been disabled', 'rangedisabled');
100 - case IPBlockForm::BLOCK_NONEXISTENT_USER:
101 - $this->dieUsage("User ``{$params['user']}'' doesn't exist", 'nosuchuser');
102 - case IPBlockForm::BLOCK_IP_INVALID:
103 - $this->dieUsage("Invaild IP address ``{$params['user']}''", 'invalidip');
104 - case IPBlockForm::BLOCK_EXPIRY_INVALID:
105 - $this->dieUsage("Invalid expiry time ``{$params['expiry']}''", 'invalidexpiry');
106 - case IPBlockForm::BLOCK_ALREADY_BLOCKED:
107 - $this->dieUsage("User ``{$params['user']}'' is already blocked", 'alreadyblocked');
108 - default:
109 - $this->dieDebug(__METHOD__, "IPBlockForm::doBlock() returned an unknown error ($retval)");
110 - }
 94+ if(!empty($retval))
 95+ // We don't care about multiple errors, just report one of them
 96+ $this->dieUsageMsg($retval);
 97+
11198 $dbw->commit();
112 -
11399 $res['user'] = $params['user'];
114100 $res['userID'] = $userID;
115101 $res['expiry'] = ($expiry == Block::infinity() ? 'infinite' : $expiry);
@@ -152,7 +138,7 @@
153139 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
154140 'nocreate' => 'Prevent account creation',
155141 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
156 - 'noemail' => 'Prevent user from sending e-mail through the wiki',
 142+ 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
157143 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)'
158144 );
159145 }
Index: trunk/phase3/includes/api/ApiBase.php
@@ -593,6 +593,12 @@
594594 'cantmove-titleprotected' => array('code' => 'protectedtitle', 'info' => "The destination article has been protected from creation"),
595595 // 'badarticleerror' => shouldn't happen
596596 // 'badtitletext' => shouldn't happen
 597+ 'ip_range_invalid' => array('code' => 'invalidrange', 'info' => "Invalid IP range"),
 598+ 'range_block_disabled' => array('code' => 'rangedisabled', 'info' => "Blocking IP ranges has been disabled"),
 599+ 'nosuchusershort' => array('code' => 'nosuchuser', 'info' => "The user you specified doesn't exist"),
 600+ 'badipaddress' => array('code' => 'invalidip', 'info' => "Invalid IP address specified"),
 601+ 'ipb_expiry_invalid' => array('code' => 'invalidexpiry', 'info' => "Invalid expiry time"),
 602+ 'ipb_already_blocked' => array('code' => 'alreadyblocked', 'info' => "The user you tried to block was already blocked"),
597603
598604 // API-specific messages
599605 'missingparam' => array('code' => 'no$1', 'info' => "The \$1 parameter must be set"),
@@ -602,6 +608,9 @@
603609 'pastexpiry' => array('code' => 'pastexpiry', 'info' => "Expiry time is in the past"),
604610 'create-titleexists' => array('code' => 'create-titleexists', 'info' => "Existing titles can't be protected with 'create'"),
605611 'missingtitle-createonly' => array('code' => 'missingtitle-createonly', 'info' => "Missing titles can only be protected with 'create'"),
 612+ 'cantblock' => array('code' => 'cantblock', 'info' => "You don't have permission to block users"),
 613+ 'canthide' => array('code' => 'canthide', 'info' => "You don't have permission to hide user names from the block log"),
 614+ 'cantblock-email' => array('code' => 'cantblock-email', 'info' => "You don't have permission to block users from sending e-mail through the wiki"),
606615 );
607616
608617 /**
Index: trunk/phase3/includes/SpecialIpblocklist.php
@@ -200,7 +200,7 @@
201201
202202 function doSubmit() {
203203 global $wgOut;
204 - $retval = self::doUnblock(&$this->id, &$this->ip, &$this->reason, &$range);
 204+ $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range);
205205 if($retval == self::UNBLOCK_SUCCESS) {
206206 # Report to the user
207207 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );

Status & tagging log