Index: trunk/phase3/includes/specials/SpecialBlock.php |
— | — | @@ -205,7 +205,7 @@ |
206 | 206 | protected function maybeAlterFormDefaults( &$fields ){ |
207 | 207 | $fields['Target']['default'] = (string)$this->target; |
208 | 208 | |
209 | | - $block = self::getBlockFromTargetAndType( $this->target, $this->type ); |
| 209 | + $block = Block::newFromTargetAndType( $this->target, $this->type ); |
210 | 210 | |
211 | 211 | if( $block instanceof Block && !$block->mAuto # The block exists and isn't an autoblock |
212 | 212 | && ( $this->type != Block::TYPE_RANGE # The block isn't a rangeblock |
— | — | @@ -405,51 +405,15 @@ |
406 | 406 | case 4: |
407 | 407 | break 2; |
408 | 408 | } |
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 ); |
424 | 412 | } |
425 | 413 | } |
426 | 414 | return array( null, null ); |
427 | 415 | } |
428 | 416 | |
429 | 417 | /** |
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 | | - /** |
454 | 418 | * HTMLForm field validation-callback for Target field. |
455 | 419 | * @since 1.18 |
456 | 420 | * @return Message |
Index: trunk/phase3/includes/Block.php |
— | — | @@ -27,6 +27,8 @@ |
28 | 28 | const TYPE_USER = 1; |
29 | 29 | const TYPE_IP = 2; |
30 | 30 | const TYPE_RANGE = 3; |
| 31 | + const TYPE_AUTO = 4; |
| 32 | + const TYPE_ID = 5; |
31 | 33 | |
32 | 34 | function __construct( $address = '', $user = 0, $by = 0, $reason = '', |
33 | 35 | $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0, |
— | — | @@ -910,4 +912,87 @@ |
911 | 913 | |
912 | 914 | return $expiry; |
913 | 915 | } |
| 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 | + } |
914 | 999 | } |