r29929 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r29928‎ | r29929 | r29930 >
Date:18:53, 18 January 2008
Author:catrope
Status:old
Tags:
Comment:
* 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
Modified paths:
  • /trunk/phase3/includes/SpecialIpblocklist.php (modified) (history)
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiUnblock.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiBase.php
@@ -599,6 +599,8 @@
600600 'badipaddress' => array('code' => 'invalidip', 'info' => "Invalid IP address specified"),
601601 'ipb_expiry_invalid' => array('code' => 'invalidexpiry', 'info' => "Invalid expiry time"),
602602 'ipb_already_blocked' => array('code' => 'alreadyblocked', 'info' => "The user you tried to block was already blocked"),
 603+ '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."),
 604+ 'ipb_cant_unblock' => array('code' => 'cantunblock', 'info' => "The block you specified was not found. It may have been unblocked already"),
603605
604606 // API-specific messages
605607 'missingparam' => array('code' => 'no$1', 'info' => "The \$1 parameter must be set"),
@@ -611,6 +613,9 @@
612614 'cantblock' => array('code' => 'cantblock', 'info' => "You don't have permission to block users"),
613615 'canthide' => array('code' => 'canthide', 'info' => "You don't have permission to hide user names from the block log"),
614616 'cantblock-email' => array('code' => 'cantblock-email', 'info' => "You don't have permission to block users from sending e-mail through the wiki"),
 617+ 'unblock-notarget' => array('code' => 'notarget', 'info' => "Either the id or the user parameter must be set"),
 618+ 'unblock-idanduser' => array('code' => 'idanduser', 'info' => "The id and user parameters can\'t be used together"),
 619+ 'cantunblock' => array('code' => 'permissiondenied', 'info' => "You don't have permission to unblock users"),
615620 );
616621
617622 /**
Index: trunk/phase3/includes/api/ApiUnblock.php
@@ -41,7 +41,7 @@
4242
4343 /**
4444 * Unblocks the specified user or provides the reason the unblock failed.
45 - */
 45+ */
4646 public function execute() {
4747 global $wgUser;
4848 $this->getMain()->requestWriteMode();
@@ -55,42 +55,28 @@
5656 }
5757
5858 if(is_null($params['id']) && is_null($params['user']))
59 - $this->dieUsage('Either the id or the user parameter must be set', 'notarget');
 59+ $this->dieUsageMsg(array('unblock-notarget'));
6060 if(!is_null($params['id']) && !is_null($params['user']))
61 - $this->dieUsage('The id and user parameters can\'t be used together', 'idanduser');
 61+ $this->dieUsageMsg(array('unblock-idanduser'));
6262 if(is_null($params['token']))
63 - $this->dieUsage('The token parameter must be set', 'notoken');
 63+ $this->dieUsageMsg(array('missingparam', 'token'));
6464 if(!$wgUser->matchEditToken($params['token']))
65 - $this->dieUsage('Invalid token', 'badtoken');
 65+ $this->dieUsageMsg(array('sessionfailure'));
6666 if(!$wgUser->isAllowed('block'))
67 - $this->dieUsage('You don\'t have permission to unblock users', 'permissiondenied');
 67+ $this->dieUsageMsg(array('cantunblock'));
6868 if(wfReadOnly())
69 - $this->dieUsage('The wiki is in read-only mode', 'readonly');
 69+ $this->dieUsageMsg(array('readonlytext'));
7070
7171 $id = $params['id'];
7272 $user = $params['user'];
73 - $reason = $params['reason'];
 73+ $reason = (is_null($params['reason']) ? '' : $params['reason']);
7474 $dbw = wfGetDb(DB_MASTER);
7575 $dbw->begin();
76 - $retval = IPUnblockForm::doUnblock(&$id, &$user, &$reason, &$range);
 76+ $retval = IPUnblockForm::doUnblock($id, $user, $reason, $range);
 77+ if(!empty($retval))
 78+ $this->dieUsageMsg($retval);
7779
78 - switch($retval)
79 - {
80 - case IPUnblockForm::UNBLOCK_SUCCESS:
81 - break; // We'll deal with that later
82 - case IPUnblockForm::UNBLOCK_NO_SUCH_ID:
83 - $this->dieUsage("There is no block with ID ``$id''", 'nosuchid');
84 - case IPUnblockForm::UNBLOCK_USER_NOT_BLOCKED:
85 - $this->dieUsage("User ``$user'' is not blocked", 'notblocked');
86 - case IPUnblockForm::UNBLOCK_BLOCKED_AS_RANGE:
87 - $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');
88 - case IPUnblockForm::UNBLOCK_UNKNOWNERR:
89 - $this->dieUsage("Unknown error", 'unknownerr');
90 - default:
91 - $this->dieDebug(__METHOD__, "IPBlockForm::doBlock() returned an unknown error ($retval)");
92 - }
9380 $dbw->commit();
94 -
9581 $res['id'] = $id;
9682 $res['user'] = $user;
9783 $res['reason'] = $reason;
Index: trunk/phase3/includes/SpecialIpblocklist.php
@@ -150,7 +150,7 @@
151151 * Backend code for unblocking. doSubmit() wraps around this.
152152 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
153153 * case it contains the range $ip is part of.
154 - * Returns one of UNBLOCK_*
 154+ * @return array array(message key, parameters) on failure, empty array on success
155155 */
156156
157157 static function doUnblock(&$id, &$ip, &$reason, &$range = null)
@@ -158,7 +158,7 @@
159159 if ( $id ) {
160160 $block = Block::newFromID( $id );
161161 if ( !$block ) {
162 - return self::UNBLOCK_NO_SUCH_ID;
 162+ return array('ipb_cant_unblock', htmlspecialchars($id));
163163 }
164164 $ip = $block->getRedactedName();
165165 } else {
@@ -168,19 +168,20 @@
169169 $id = substr( $ip, 1 );
170170 $block = Block::newFromID( $id );
171171 if( !$block ) {
172 - return self::UNBLOCK_NO_SUCH_ID;
 172+ return array('ipb_cant_unblock', htmlspecialchars($id));
173173 }
 174+ $ip = $block->getRedactedName();
174175 } else {
175176 $block = Block::newFromDB( $ip );
176177 if ( !$block ) {
177 - return self::UNBLOCK_USER_NOT_BLOCKED;
 178+ return array('ipb_cant_unblock', htmlspecialchars($id));
178179 }
179180 if( $block->mRangeStart != $block->mRangeEnd
180181 && !strstr( $ip, "/" ) ) {
181182 /* If the specified IP is a single address, and the block is
182183 * a range block, don't unblock the range. */
183184 $range = $block->mAddress;
184 - return self::UNBLOCK_BLOCKED_AS_RANGE;
 185+ return array('ipb_blocked_as_range', $ip, $range);
185186 }
186187 }
187188 }
@@ -189,31 +190,28 @@
190191
191192 # Delete block
192193 if ( !$block->delete() ) {
193 - return self::UNBLOCK_UNKNOWNERR;
 194+ return array('ipb_cant_unblock', htmlspecialchars($id));
194195 }
195196
196197 # Make log entry
197198 $log = new LogPage( 'block' );
198199 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
199 - return self::UNBLOCK_SUCCESS;
 200+ return array();
200201 }
201202
202203 function doSubmit() {
203204 global $wgOut;
204205 $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range);
205 - if($retval == self::UNBLOCK_SUCCESS) {
206 - # Report to the user
207 - $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
208 - $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
209 - $wgOut->redirect( $success );
210 - } else if($retval == self::UNBLOCK_BLOCKED_AS_RANGE) {
211 - $this->showForm( wfMsg( 'ipb_blocked_as_range', $this->ip, $range ) );
212 - } else { // UI code doesn't distinguish between errors much. Maybe it should
213 - if ( !$this->ip && $this->id ) {
214 - $this->ip = '#' . $this->id;
215 - }
216 - $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
 206+ if(!empty($retval))
 207+ {
 208+ $key = array_shift($retval);
 209+ $this->showForm(wfMsgReal($key, $retval));
 210+ return;
217211 }
 212+ # Report to the user
 213+ $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
 214+ $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
 215+ $wgOut->redirect( $success );
218216 }
219217
220218 function showList( $msg ) {

Status & tagging log