From 152463e335bec70154082f6116a123d5f4fef243 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Fri, 18 Jan 2008 18:53:57 +0000 Subject: [PATCH] * Refactored IPUnblockForm::doUnblock() to return an array of message keys and parameters * Refactored IPUnblockForm::doSubmit() and ApiUnblock accordingly * Added even more messages to ApiBase::$messageMap --- includes/SpecialIpblocklist.php | 36 ++++++++++++++++----------------- includes/api/ApiBase.php | 5 +++++ includes/api/ApiUnblock.php | 36 ++++++++++----------------------- 3 files changed, 33 insertions(+), 44 deletions(-) diff --git a/includes/SpecialIpblocklist.php b/includes/SpecialIpblocklist.php index c46e847988..287fac62d3 100644 --- a/includes/SpecialIpblocklist.php +++ b/includes/SpecialIpblocklist.php @@ -150,7 +150,7 @@ class IPUnblockForm { * Backend code for unblocking. doSubmit() wraps around this. * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which * case it contains the range $ip is part of. - * Returns one of UNBLOCK_* + * @return array array(message key, parameters) on failure, empty array on success */ static function doUnblock(&$id, &$ip, &$reason, &$range = null) @@ -158,7 +158,7 @@ class IPUnblockForm { if ( $id ) { $block = Block::newFromID( $id ); if ( !$block ) { - return self::UNBLOCK_NO_SUCH_ID; + return array('ipb_cant_unblock', htmlspecialchars($id)); } $ip = $block->getRedactedName(); } else { @@ -168,19 +168,20 @@ class IPUnblockForm { $id = substr( $ip, 1 ); $block = Block::newFromID( $id ); if( !$block ) { - return self::UNBLOCK_NO_SUCH_ID; + return array('ipb_cant_unblock', htmlspecialchars($id)); } + $ip = $block->getRedactedName(); } else { $block = Block::newFromDB( $ip ); if ( !$block ) { - return self::UNBLOCK_USER_NOT_BLOCKED; + return array('ipb_cant_unblock', htmlspecialchars($id)); } if( $block->mRangeStart != $block->mRangeEnd && !strstr( $ip, "/" ) ) { /* If the specified IP is a single address, and the block is * a range block, don't unblock the range. */ $range = $block->mAddress; - return self::UNBLOCK_BLOCKED_AS_RANGE; + return array('ipb_blocked_as_range', $ip, $range); } } } @@ -189,31 +190,28 @@ class IPUnblockForm { # Delete block if ( !$block->delete() ) { - return self::UNBLOCK_UNKNOWNERR; + return array('ipb_cant_unblock', htmlspecialchars($id)); } # Make log entry $log = new LogPage( 'block' ); $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason ); - return self::UNBLOCK_SUCCESS; + return array(); } function doSubmit() { global $wgOut; $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range); - if($retval == self::UNBLOCK_SUCCESS) { - # Report to the user - $titleObj = SpecialPage::getTitleFor( "Ipblocklist" ); - $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) ); - $wgOut->redirect( $success ); - } else if($retval == self::UNBLOCK_BLOCKED_AS_RANGE) { - $this->showForm( wfMsg( 'ipb_blocked_as_range', $this->ip, $range ) ); - } else { // UI code doesn't distinguish between errors much. Maybe it should - if ( !$this->ip && $this->id ) { - $this->ip = '#' . $this->id; - } - $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) ); + if(!empty($retval)) + { + $key = array_shift($retval); + $this->showForm(wfMsgReal($key, $retval)); + return; } + # Report to the user + $titleObj = SpecialPage::getTitleFor( "Ipblocklist" ); + $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) ); + $wgOut->redirect( $success ); } function showList( $msg ) { diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 9043815329..682dffa518 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -599,6 +599,8 @@ abstract class ApiBase { 'badipaddress' => array('code' => 'invalidip', 'info' => "Invalid IP address specified"), 'ipb_expiry_invalid' => array('code' => 'invalidexpiry', 'info' => "Invalid expiry time"), 'ipb_already_blocked' => array('code' => 'alreadyblocked', 'info' => "The user you tried to block was already blocked"), + 'ipb_blocked_as_range' => array('code' => 'blockedasrange', 'info' => "IP address ``\$1'' was blocked as part of range ``\$2''. You can't unblock the IP invidually, but you can unblock the range as a whole."), + 'ipb_cant_unblock' => array('code' => 'cantunblock', 'info' => "The block you specified was not found. It may have been unblocked already"), // API-specific messages 'missingparam' => array('code' => 'no$1', 'info' => "The \$1 parameter must be set"), @@ -611,6 +613,9 @@ abstract class ApiBase { 'cantblock' => array('code' => 'cantblock', 'info' => "You don't have permission to block users"), 'canthide' => array('code' => 'canthide', 'info' => "You don't have permission to hide user names from the block log"), 'cantblock-email' => array('code' => 'cantblock-email', 'info' => "You don't have permission to block users from sending e-mail through the wiki"), + 'unblock-notarget' => array('code' => 'notarget', 'info' => "Either the id or the user parameter must be set"), + 'unblock-idanduser' => array('code' => 'idanduser', 'info' => "The id and user parameters can\'t be used together"), + 'cantunblock' => array('code' => 'permissiondenied', 'info' => "You don't have permission to unblock users"), ); /** diff --git a/includes/api/ApiUnblock.php b/includes/api/ApiUnblock.php index a09c19ae0c..842267b5f3 100644 --- a/includes/api/ApiUnblock.php +++ b/includes/api/ApiUnblock.php @@ -41,7 +41,7 @@ class ApiUnblock extends ApiBase { /** * Unblocks the specified user or provides the reason the unblock failed. - */ + */ public function execute() { global $wgUser; $this->getMain()->requestWriteMode(); @@ -55,42 +55,28 @@ class ApiUnblock extends ApiBase { } if(is_null($params['id']) && is_null($params['user'])) - $this->dieUsage('Either the id or the user parameter must be set', 'notarget'); + $this->dieUsageMsg(array('unblock-notarget')); if(!is_null($params['id']) && !is_null($params['user'])) - $this->dieUsage('The id and user parameters can\'t be used together', 'idanduser'); + $this->dieUsageMsg(array('unblock-idanduser')); if(is_null($params['token'])) - $this->dieUsage('The token parameter must be set', 'notoken'); + $this->dieUsageMsg(array('missingparam', 'token')); if(!$wgUser->matchEditToken($params['token'])) - $this->dieUsage('Invalid token', 'badtoken'); + $this->dieUsageMsg(array('sessionfailure')); if(!$wgUser->isAllowed('block')) - $this->dieUsage('You don\'t have permission to unblock users', 'permissiondenied'); + $this->dieUsageMsg(array('cantunblock')); if(wfReadOnly()) - $this->dieUsage('The wiki is in read-only mode', 'readonly'); + $this->dieUsageMsg(array('readonlytext')); $id = $params['id']; $user = $params['user']; - $reason = $params['reason']; + $reason = (is_null($params['reason']) ? '' : $params['reason']); $dbw = wfGetDb(DB_MASTER); $dbw->begin(); - $retval = IPUnblockForm::doUnblock(&$id, &$user, &$reason, &$range); + $retval = IPUnblockForm::doUnblock($id, $user, $reason, $range); + if(!empty($retval)) + $this->dieUsageMsg($retval); - switch($retval) - { - case IPUnblockForm::UNBLOCK_SUCCESS: - break; // We'll deal with that later - case IPUnblockForm::UNBLOCK_NO_SUCH_ID: - $this->dieUsage("There is no block with ID ``$id''", 'nosuchid'); - case IPUnblockForm::UNBLOCK_USER_NOT_BLOCKED: - $this->dieUsage("User ``$user'' is not blocked", 'notblocked'); - case IPUnblockForm::UNBLOCK_BLOCKED_AS_RANGE: - $this->dieUsage("IP address ``$user'' was blocked as part of range ``$range''. You can't unblock the IP invidually, but you can unblock the range as a whole.", 'blockedasrange'); - case IPUnblockForm::UNBLOCK_UNKNOWNERR: - $this->dieUsage("Unknown error", 'unknownerr'); - default: - $this->dieDebug(__METHOD__, "IPBlockForm::doBlock() returned an unknown error ($retval)"); - } $dbw->commit(); - $res['id'] = $id; $res['user'] = $user; $res['reason'] = $reason; -- 2.20.1