Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
38.46% |
5 / 13 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| BlockUtils | |
38.46% |
5 / 13 |
|
50.00% |
2 / 4 |
18.42 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseBlockTarget | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| parseBlockTargetRow | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| validateTarget | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @license GPL-2.0-or-later |
| 5 | * @file |
| 6 | */ |
| 7 | |
| 8 | namespace MediaWiki\Block; |
| 9 | |
| 10 | use MediaWiki\Status\Status; |
| 11 | use MediaWiki\User\UserIdentity; |
| 12 | |
| 13 | /** |
| 14 | * Backend class for blocking utils |
| 15 | * |
| 16 | * This service should contain any methods that are useful |
| 17 | * to more than one blocking-related class and don't fit any |
| 18 | * other service. |
| 19 | * |
| 20 | * For now, this includes only |
| 21 | * - block target parsing |
| 22 | * - block target validation |
| 23 | * - parsing the target and type of a block in the database |
| 24 | * |
| 25 | * @deprecated since 1.44 use BlockTargetFactory |
| 26 | * @since 1.36 |
| 27 | */ |
| 28 | class BlockUtils { |
| 29 | private BlockTargetFactory $blockTargetFactory; |
| 30 | |
| 31 | public function __construct( BlockTargetFactory $blockTargetFactory ) { |
| 32 | $this->blockTargetFactory = $blockTargetFactory; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * From string specification or UserIdentity, get the block target and the |
| 37 | * type of target. |
| 38 | * |
| 39 | * Note that, except for null, it is always safe to treat the target |
| 40 | * as a string; for UserIdentityValue objects this will return |
| 41 | * UserIdentityValue::__toString() which in turn gives |
| 42 | * UserIdentityValue::getName(). |
| 43 | * |
| 44 | * If the type is not null, it will be an AbstractBlock::TYPE_ constant. |
| 45 | * |
| 46 | * Since 1.42, it is no longer safe to pass a value from the database field |
| 47 | * ipb_address/bt_address to this method, since the username is normalized. |
| 48 | * Use parseBlockTargetRow() instead. (T346683) |
| 49 | * |
| 50 | * @param string|UserIdentity|null $target |
| 51 | * @return array [ UserIdentity|String|null, int|null ] |
| 52 | */ |
| 53 | public function parseBlockTarget( $target ): array { |
| 54 | $targetObj = $this->blockTargetFactory->newFromLegacyUnion( $target ); |
| 55 | if ( $targetObj ) { |
| 56 | return $targetObj->getLegacyTuple(); |
| 57 | } else { |
| 58 | return [ null, null ]; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * From a row which must contain bt_auto, bt_user, bt_address and bl_id, |
| 64 | * and optionally bt_user_text, determine the block target and type. |
| 65 | * |
| 66 | * @since 1.42 |
| 67 | * @param \stdClass $row |
| 68 | * @return array [ UserIdentity|String|null, int|null ] |
| 69 | */ |
| 70 | public function parseBlockTargetRow( $row ) { |
| 71 | $target = $this->blockTargetFactory->newFromRowRedacted( $row ); |
| 72 | if ( $target ) { |
| 73 | return $target->getLegacyTuple(); |
| 74 | } else { |
| 75 | return [ null, null ]; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Validate block target |
| 81 | * |
| 82 | * @param string|UserIdentity $value |
| 83 | * |
| 84 | * @return Status |
| 85 | */ |
| 86 | public function validateTarget( $value ): Status { |
| 87 | $target = $this->blockTargetFactory->newFromLegacyUnion( $value ); |
| 88 | if ( $target ) { |
| 89 | return Status::wrap( $target->validateForCreation() ); |
| 90 | } else { |
| 91 | return Status::newFatal( 'badipaddress' ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } |