r83825 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83824‎ | r83825 | r83826 >
Date:14:47, 13 March 2011
Author:happy-melon
Status:resolved (Comments)
Tags:
Comment:
Some refactoring in Block.php and SpecialBlock.php: move backend stuff into Block.php, and expand the parseTargetAndType() functions to recognise blocks referenced by their block id.
Modified paths:
  • /trunk/phase3/includes/Block.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialBlock.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialBlock.php
@@ -205,7 +205,7 @@
206206 protected function maybeAlterFormDefaults( &$fields ){
207207 $fields['Target']['default'] = (string)$this->target;
208208
209 - $block = self::getBlockFromTargetAndType( $this->target, $this->type );
 209+ $block = Block::newFromTargetAndType( $this->target, $this->type );
210210
211211 if( $block instanceof Block && !$block->mAuto # The block exists and isn't an autoblock
212212 && ( $this->type != Block::TYPE_RANGE # The block isn't a rangeblock
@@ -405,51 +405,15 @@
406406 case 4:
407407 break 2;
408408 }
409 -
410 - $userObj = User::newFromName( $target );
411 - if( $userObj instanceof User ){
412 - return array( $userObj, Block::TYPE_USER );
413 - } elseif( IP::isValid( $target ) ){
414 - # We can still create a User if it's an IP address, but we need to turn
415 - # off validation checking (which would exclude IP addresses)
416 - return array(
417 - User::newFromName( IP::sanitizeIP( $target ), false ),
418 - Block::TYPE_IP
419 - );
420 - break;
421 - } elseif( IP::isValidBlock( $target ) ){
422 - # Can't create a User from an IP range
423 - return array( Block::normaliseRange( $target ), Block::TYPE_RANGE );
 409+ list( $target, $type ) = Block::parseTarget( $target );
 410+ if( $type !== null ){
 411+ return array( $target, $type );
424412 }
425413 }
426414 return array( null, null );
427415 }
428416
429417 /**
430 - * Given a target and the target's type, get a block object if possible
431 - * @param $target String|User
432 - * @param $type Block::TYPE_ constant
433 - * @return Block|null
434 - * TODO: this probably belongs in Block.php when that mess is cleared up
435 - */
436 - public static function getBlockFromTargetAndType( $target, $type ){
437 - if( $target instanceof User ){
438 - if( $type == Block::TYPE_IP ){
439 - return Block::newFromDB( $target->getName(), 0 );
440 - } elseif( $type == Block::TYPE_USER ) {
441 - return Block::newFromDB( '', $target->getId() );
442 - } else {
443 - # Should be unreachable;
444 - return null;
445 - }
446 - } elseif( $type == Block::TYPE_RANGE ){
447 - return Block::newFromDB( '', $target );
448 - } else {
449 - return null;
450 - }
451 - }
452 -
453 - /**
454418 * HTMLForm field validation-callback for Target field.
455419 * @since 1.18
456420 * @return Message
Index: trunk/phase3/includes/Block.php
@@ -27,6 +27,8 @@
2828 const TYPE_USER = 1;
2929 const TYPE_IP = 2;
3030 const TYPE_RANGE = 3;
 31+ const TYPE_AUTO = 4;
 32+ const TYPE_ID = 5;
3133
3234 function __construct( $address = '', $user = 0, $by = 0, $reason = '',
3335 $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
@@ -910,4 +912,87 @@
911913
912914 return $expiry;
913915 }
 916+
 917+ # FIXME: everything above here is a mess, needs much cleaning up
 918+
 919+ /**
 920+ * Given a target and the target's type, get a Block object if possible
 921+ * @param $target String|User|Int a block target, which may be one of several types:
 922+ * * A user to block, in which case $target will be a User
 923+ * * An IP to block, in which case $target will be a User generated by using
 924+ * User::newFromName( $ip, false ) to turn off name validation
 925+ * * An IP range, in which case $target will be a String "123.123.123.123/18" etc
 926+ * * The ID of an existing block, in which case $target will be an Int
 927+ * @param $type Block::TYPE_ constant the type of block as described above
 928+ * @return Block|null (null if the target is not blocked)
 929+ */
 930+ public static function newFromTargetAndType( $target, $type ){
 931+ if( $target instanceof User ){
 932+ if( $type == Block::TYPE_IP ){
 933+ return Block::newFromDB( $target->getName(), 0 );
 934+ } elseif( $type == Block::TYPE_USER ) {
 935+ return Block::newFromDB( '', $target->getId() );
 936+ } else {
 937+ # Should be unreachable;
 938+ return null;
 939+ }
 940+
 941+ } elseif( $type == Block::TYPE_RANGE ){
 942+ return Block::newFromDB( '', $target );
 943+
 944+ } elseif( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ){
 945+ return Block::newFromID( $target );
 946+
 947+ } else {
 948+ return null;
 949+ }
 950+ }
 951+
 952+ public static function newFromTarget( $target ){
 953+ list( $target, $type ) = self::parseTarget( $target );
 954+ return self::newFromTargetAndType( $target, $type );
 955+ }
 956+
 957+ /**
 958+ * From an existing Block, get the target and the type of target. Note that it is
 959+ * always safe to treat the target as a string; for User objects this will return
 960+ * User::__toString() which in turn gives User::getName().
 961+ * @return array( User|String, Block::TYPE_ constant )
 962+ */
 963+ public static function parseTarget( $target ){
 964+ $target = trim( $target );
 965+
 966+ $userObj = User::newFromName( $target );
 967+ if( $userObj instanceof User ){
 968+ return array( $userObj, Block::TYPE_USER );
 969+
 970+ } elseif( IP::isValid( $target ) ){
 971+ # We can still create a User if it's an IP address, but we need to turn
 972+ # off validation checking (which would exclude IP addresses)
 973+ return array(
 974+ User::newFromName( IP::sanitizeIP( $target ), false ),
 975+ Block::TYPE_IP
 976+ );
 977+
 978+ } elseif( IP::isValidBlock( $target ) ){
 979+ # Can't create a User from an IP range
 980+ return array( Block::normaliseRange( $target ), Block::TYPE_RANGE );
 981+
 982+ } elseif( preg_match( '/^#\d+$/', $target ) ){
 983+ # Autoblock reference in the form "#12345"
 984+ return array( substr( $target, 1 ), Block::TYPE_AUTO );
 985+
 986+ } elseif( preg_match( '/^\d+$/', $target ) ){
 987+ # Block id reference as a pure number
 988+ return array( $target, Block::TYPE_ID );
 989+ }
 990+ }
 991+
 992+ /**
 993+ * Get the target and target type for this particular Block
 994+ * @return array( User|String, Block::TYPE_ constant )
 995+ */
 996+ public function getTargetAndType(){
 997+ return self::parseTarget( $this->mAddress );
 998+ }
914999 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r83827Follow-up r83825: fix Block::parseTarget to recognise autoblocks separately, ...happy-melon15:14, 13 March 2011
r83832Follow-up r83825: fix fatal in APIhappy-melon17:02, 13 March 2011

Comments

#Comment by Bryan (talk | contribs)   18:32, 13 March 2011

Does this really need to break backwards compat? You can make SpecialBlockIp::getBlockFromTargetAndType() a wrapper around Block::newFromTargetAndType(), slap wfDeprecated() on it and mark it for removal in 1.20 orso.

#Comment by Happy-melon (talk | contribs)   20:53, 13 March 2011

The previous structure was only in place for 49 revisions! Heaven help us if we need to keep B/C for changes on that timescale...

#Comment by Bryan (talk | contribs)   20:54, 13 March 2011

Oh ok. It's fine then.

Status & tagging log