From f64f960d9a4b6e510dcc58e04894530f38f5a777 Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Sun, 13 Mar 2011 14:47:34 +0000 Subject: [PATCH] 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. --- includes/Block.php | 85 ++++++++++++++++++++++++++++++ includes/specials/SpecialBlock.php | 44 ++-------------- 2 files changed, 89 insertions(+), 40 deletions(-) diff --git a/includes/Block.php b/includes/Block.php index 9667056749..6541284590 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -27,6 +27,8 @@ class Block { const TYPE_USER = 1; const TYPE_IP = 2; const TYPE_RANGE = 3; + const TYPE_AUTO = 4; + const TYPE_ID = 5; function __construct( $address = '', $user = 0, $by = 0, $reason = '', $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0, @@ -910,4 +912,87 @@ class Block { return $expiry; } + + # FIXME: everything above here is a mess, needs much cleaning up + + /** + * Given a target and the target's type, get a Block object if possible + * @param $target String|User|Int a block target, which may be one of several types: + * * A user to block, in which case $target will be a User + * * An IP to block, in which case $target will be a User generated by using + * User::newFromName( $ip, false ) to turn off name validation + * * An IP range, in which case $target will be a String "123.123.123.123/18" etc + * * The ID of an existing block, in which case $target will be an Int + * @param $type Block::TYPE_ constant the type of block as described above + * @return Block|null (null if the target is not blocked) + */ + public static function newFromTargetAndType( $target, $type ){ + if( $target instanceof User ){ + if( $type == Block::TYPE_IP ){ + return Block::newFromDB( $target->getName(), 0 ); + } elseif( $type == Block::TYPE_USER ) { + return Block::newFromDB( '', $target->getId() ); + } else { + # Should be unreachable; + return null; + } + + } elseif( $type == Block::TYPE_RANGE ){ + return Block::newFromDB( '', $target ); + + } elseif( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ){ + return Block::newFromID( $target ); + + } else { + return null; + } + } + + public static function newFromTarget( $target ){ + list( $target, $type ) = self::parseTarget( $target ); + return self::newFromTargetAndType( $target, $type ); + } + + /** + * From an existing Block, get the target and the type of target. Note that it is + * always safe to treat the target as a string; for User objects this will return + * User::__toString() which in turn gives User::getName(). + * @return array( User|String, Block::TYPE_ constant ) + */ + public static function parseTarget( $target ){ + $target = trim( $target ); + + $userObj = User::newFromName( $target ); + if( $userObj instanceof User ){ + return array( $userObj, Block::TYPE_USER ); + + } elseif( IP::isValid( $target ) ){ + # We can still create a User if it's an IP address, but we need to turn + # off validation checking (which would exclude IP addresses) + return array( + User::newFromName( IP::sanitizeIP( $target ), false ), + Block::TYPE_IP + ); + + } elseif( IP::isValidBlock( $target ) ){ + # Can't create a User from an IP range + return array( Block::normaliseRange( $target ), Block::TYPE_RANGE ); + + } elseif( preg_match( '/^#\d+$/', $target ) ){ + # Autoblock reference in the form "#12345" + return array( substr( $target, 1 ), Block::TYPE_AUTO ); + + } elseif( preg_match( '/^\d+$/', $target ) ){ + # Block id reference as a pure number + return array( $target, Block::TYPE_ID ); + } + } + + /** + * Get the target and target type for this particular Block + * @return array( User|String, Block::TYPE_ constant ) + */ + public function getTargetAndType(){ + return self::parseTarget( $this->mAddress ); + } } diff --git a/includes/specials/SpecialBlock.php b/includes/specials/SpecialBlock.php index 08d8fe05e5..22c8a0049e 100644 --- a/includes/specials/SpecialBlock.php +++ b/includes/specials/SpecialBlock.php @@ -205,7 +205,7 @@ class SpecialBlock extends SpecialPage { protected function maybeAlterFormDefaults( &$fields ){ $fields['Target']['default'] = (string)$this->target; - $block = self::getBlockFromTargetAndType( $this->target, $this->type ); + $block = Block::newFromTargetAndType( $this->target, $this->type ); if( $block instanceof Block && !$block->mAuto # The block exists and isn't an autoblock && ( $this->type != Block::TYPE_RANGE # The block isn't a rangeblock @@ -405,50 +405,14 @@ class SpecialBlock extends SpecialPage { case 4: break 2; } - - $userObj = User::newFromName( $target ); - if( $userObj instanceof User ){ - return array( $userObj, Block::TYPE_USER ); - } elseif( IP::isValid( $target ) ){ - # We can still create a User if it's an IP address, but we need to turn - # off validation checking (which would exclude IP addresses) - return array( - User::newFromName( IP::sanitizeIP( $target ), false ), - Block::TYPE_IP - ); - break; - } elseif( IP::isValidBlock( $target ) ){ - # Can't create a User from an IP range - return array( Block::normaliseRange( $target ), Block::TYPE_RANGE ); + list( $target, $type ) = Block::parseTarget( $target ); + if( $type !== null ){ + return array( $target, $type ); } } return array( null, null ); } - /** - * Given a target and the target's type, get a block object if possible - * @param $target String|User - * @param $type Block::TYPE_ constant - * @return Block|null - * TODO: this probably belongs in Block.php when that mess is cleared up - */ - public static function getBlockFromTargetAndType( $target, $type ){ - if( $target instanceof User ){ - if( $type == Block::TYPE_IP ){ - return Block::newFromDB( $target->getName(), 0 ); - } elseif( $type == Block::TYPE_USER ) { - return Block::newFromDB( '', $target->getId() ); - } else { - # Should be unreachable; - return null; - } - } elseif( $type == Block::TYPE_RANGE ){ - return Block::newFromDB( '', $target ); - } else { - return null; - } - } - /** * HTMLForm field validation-callback for Target field. * @since 1.18 -- 2.20.1