Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.46% covered (danger)
38.46%
5 / 13
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockUtils
38.46% covered (danger)
38.46%
5 / 13
50.00% covered (danger)
50.00%
2 / 4
18.42
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseBlockTarget
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 parseBlockTargetRow
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 validateTarget
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22namespace MediaWiki\Block;
23
24use MediaWiki\Status\Status;
25use MediaWiki\User\UserIdentity;
26
27/**
28 * Backend class for blocking utils
29 *
30 * This service should contain any methods that are useful
31 * to more than one blocking-related class and don't fit any
32 * other service.
33 *
34 * For now, this includes only
35 * - block target parsing
36 * - block target validation
37 * - parsing the target and type of a block in the database
38 *
39 * @deprecated since 1.44 use BlockTargetFactory
40 * @since 1.36
41 */
42class BlockUtils {
43    private BlockTargetFactory $blockTargetFactory;
44
45    public function __construct( BlockTargetFactory $blockTargetFactory ) {
46        $this->blockTargetFactory = $blockTargetFactory;
47    }
48
49    /**
50     * From string specification or UserIdentity, get the block target and the
51     * type of target.
52     *
53     * Note that, except for null, it is always safe to treat the target
54     * as a string; for UserIdentityValue objects this will return
55     * UserIdentityValue::__toString() which in turn gives
56     * UserIdentityValue::getName().
57     *
58     * If the type is not null, it will be an AbstractBlock::TYPE_ constant.
59     *
60     * Since 1.42, it is no longer safe to pass a value from the database field
61     * ipb_address/bt_address to this method, since the username is normalized.
62     * Use parseBlockTargetRow() instead. (T346683)
63     *
64     * @param string|UserIdentity|null $target
65     * @return array [ UserIdentity|String|null, int|null ]
66     */
67    public function parseBlockTarget( $target ): array {
68        $targetObj = $this->blockTargetFactory->newFromLegacyUnion( $target );
69        if ( $targetObj ) {
70            return $targetObj->getLegacyTuple();
71        } else {
72            return [ null, null ];
73        }
74    }
75
76    /**
77     * From a row which must contain bt_auto, bt_user, bt_address and bl_id,
78     * and optionally bt_user_text, determine the block target and type.
79     *
80     * @since 1.42
81     * @param \stdClass $row
82     * @return array [ UserIdentity|String|null, int|null ]
83     */
84    public function parseBlockTargetRow( $row ) {
85        $target = $this->blockTargetFactory->newFromRowRedacted( $row );
86        if ( $target ) {
87            return $target->getLegacyTuple();
88        } else {
89            return [ null, null ];
90        }
91    }
92
93    /**
94     * Validate block target
95     *
96     * @param string|UserIdentity $value
97     *
98     * @return Status
99     */
100    public function validateTarget( $value ): Status {
101        $target = $this->blockTargetFactory->newFromLegacyUnion( $value );
102        if ( $target ) {
103            return Status::wrap( $target->validateForCreation() );
104        } else {
105            return Status::newFatal( 'badipaddress' );
106        }
107    }
108
109}