Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
48 / 72
35.00% covered (danger)
35.00%
7 / 20
CRAP
0.00% covered (danger)
0.00%
0 / 1
CompositeBlock
66.67% covered (warning)
66.67%
48 / 72
35.00% covered (danger)
35.00%
7 / 20
99.26
0.00% covered (danger)
0.00%
0 / 1
 createFromBlocks
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
4.01
 __construct
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 propHasValue
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 methodReturnsValue
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 getOriginalBlocks
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withOriginalBlocks
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTimestamp
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 getExpiry
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 getIdentifier
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 appliesToRight
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 appliesToUsertalk
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 appliesToTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 appliesToNamespace
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 appliesToPage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 appliesToCreatePageInNamespace
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 appliesToPasswordReset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBy
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getByName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBlocker
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Class for blocks composed from multiple blocks.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Block;
10
11use InvalidArgumentException;
12use MediaWiki\Message\Message;
13use MediaWiki\Page\PageIdentity;
14use MediaWiki\Title\Title;
15use MediaWiki\User\UserIdentity;
16
17/**
18 * Multiple Block class.
19 *
20 * Multiple blocks exist to enforce restrictions from more than one block, if several
21 * blocks apply to a user/IP. Multiple blocks are created temporarily on enforcement.
22 *
23 * @since 1.34
24 */
25class CompositeBlock extends AbstractBlock {
26    /** @var AbstractBlock[] */
27    private $originalBlocks;
28
29    /**
30     * Helper method for merging multiple blocks into a composite block.
31     * @param AbstractBlock ...$blocks
32     * @return self
33     */
34    public static function createFromBlocks( AbstractBlock ...$blocks ): self {
35        $originalBlocks = [];
36        foreach ( $blocks as $block ) {
37            if ( $block instanceof self ) {
38                $originalBlocks = array_merge( $originalBlocks, $block->getOriginalBlocks() );
39            } else {
40                $originalBlocks[] = $block;
41            }
42        }
43        if ( !$originalBlocks ) {
44            throw new InvalidArgumentException( 'No blocks given' );
45        }
46        return new self( [
47            'target' => $originalBlocks[0]->target,
48            'reason' => new Message( 'blockedtext-composite-reason' ),
49            'originalBlocks' => $originalBlocks,
50        ] );
51    }
52
53    /**
54     * Create a new block with specified parameters on a user, IP or IP range.
55     *
56     * @param array $options Parameters of the block, with options supported by
57     *  `AbstractBlock::__construct`, and also:
58     *  - originalBlocks: (Block[]) Blocks that this block is composed from
59     */
60    public function __construct( array $options = [] ) {
61        parent::__construct( $options );
62
63        $defaults = [
64            'originalBlocks' => [],
65        ];
66
67        $options += $defaults;
68
69        $this->originalBlocks = $options[ 'originalBlocks' ];
70
71        $this->setHideName( $this->propHasValue( 'hideName', true ) );
72        $this->setHideBlock( $this->propHasValue( 'hideBlock', true ) );
73        $this->isHardblock( $this->propHasValue( 'isHardblock', true ) );
74        $this->isSitewide( $this->propHasValue( 'isSitewide', true ) );
75        $this->isEmailBlocked( $this->propHasValue( 'blockEmail', true ) );
76        $this->isCreateAccountBlocked( $this->propHasValue( 'blockCreateAccount', true ) );
77        $this->isUsertalkEditAllowed( !$this->propHasValue( 'allowUsertalk', false ) );
78    }
79
80    /**
81     * Determine whether any original blocks have a particular property set to a
82     * particular value.
83     *
84     * @param string $prop
85     * @param mixed $value
86     * @return bool At least one block has the property set to the value
87     */
88    private function propHasValue( $prop, $value ) {
89        foreach ( $this->originalBlocks as $block ) {
90            if ( $block->$prop == $value ) {
91                return true;
92            }
93        }
94        return false;
95    }
96
97    /**
98     * Determine whether any original blocks have a particular method returning a
99     * particular value.
100     *
101     * @param string $method
102     * @param mixed $value
103     * @param mixed ...$params
104     * @return bool At least one block has the method returning the value
105     */
106    private function methodReturnsValue( $method, $value, ...$params ) {
107        foreach ( $this->originalBlocks as $block ) {
108            if ( $block->$method( ...$params ) == $value ) {
109                return true;
110            }
111        }
112        return false;
113    }
114
115    /**
116     * Get the original blocks from which this block is composed
117     *
118     * @since 1.34
119     * @return AbstractBlock[]
120     */
121    public function getOriginalBlocks() {
122        return $this->originalBlocks;
123    }
124
125    /**
126     * Create a clone of the object with the original blocks array set to
127     * something else.
128     *
129     * @since 1.42
130     * @param AbstractBlock[] $blocks
131     * @return self
132     */
133    public function withOriginalBlocks( array $blocks ) {
134        $clone = clone $this;
135        $clone->originalBlocks = $blocks;
136        return $clone;
137    }
138
139    public function toArray(): array {
140        return $this->originalBlocks;
141    }
142
143    /**
144     * @inheritDoc
145     */
146    public function getTimestamp(): string {
147        $minStart = null;
148        foreach ( $this->originalBlocks as $block ) {
149            $startTime = $block->getTimestamp();
150            if ( $minStart === null || $startTime === '' || $startTime < $minStart ) {
151                $minStart = $startTime;
152            }
153        }
154        return $minStart ?? '';
155    }
156
157    /**
158     * @inheritDoc
159     */
160    public function getExpiry(): string {
161        $maxExpiry = null;
162        foreach ( $this->originalBlocks as $block ) {
163            $expiry = $block->getExpiry();
164            if ( $maxExpiry === null || $expiry === '' || $expiry > $maxExpiry ) {
165                $maxExpiry = $expiry;
166            }
167        }
168        return $maxExpiry ?? '';
169    }
170
171    /**
172     * @inheritDoc
173     */
174    public function getIdentifier( $wikiId = self::LOCAL ) {
175        $identifier = [];
176        foreach ( $this->originalBlocks as $block ) {
177            $identifier[] = $block->getIdentifier( $wikiId );
178        }
179        return $identifier;
180    }
181
182    /**
183     * @inheritDoc
184     *
185     * Determines whether the CompositeBlock applies to a right by checking
186     * whether the original blocks apply to that right. Each block can report
187     * true (applies), false (does not apply) or null (unsure). Then:
188     * - If any original blocks apply, this block applies
189     * - If no original blocks apply but any are unsure, this block is unsure
190     * - If all blocks do not apply, this block does not apply
191     */
192    public function appliesToRight( $right ) {
193        $isUnsure = false;
194
195        foreach ( $this->originalBlocks as $block ) {
196            $appliesToRight = $block->appliesToRight( $right );
197
198            if ( $appliesToRight ) {
199                return true;
200            } elseif ( $appliesToRight === null ) {
201                $isUnsure = true;
202            }
203        }
204
205        return $isUnsure ? null : false;
206    }
207
208    /**
209     * @inheritDoc
210     */
211    public function appliesToUsertalk( ?PageIdentity $usertalk = null ): bool {
212        return $this->methodReturnsValue( __FUNCTION__, true, $usertalk );
213    }
214
215    /**
216     * @inheritDoc
217     */
218    public function appliesToTitle( Title $title ) {
219        return $this->methodReturnsValue( __FUNCTION__, true, $title );
220    }
221
222    /**
223     * @inheritDoc
224     */
225    public function appliesToNamespace( $ns ) {
226        return $this->methodReturnsValue( __FUNCTION__, true, $ns );
227    }
228
229    /**
230     * @inheritDoc
231     */
232    public function appliesToPage( $pageId ) {
233        return $this->methodReturnsValue( __FUNCTION__, true, $pageId );
234    }
235
236    /**
237     * @inheritDoc
238     */
239    public function appliesToCreatePageInNamespace( int $ns ): bool {
240        return $this->methodReturnsValue( __FUNCTION__, true, $ns );
241    }
242
243    /**
244     * @inheritDoc
245     */
246    public function appliesToPasswordReset() {
247        return $this->methodReturnsValue( __FUNCTION__, true );
248    }
249
250    /**
251     * @inheritDoc
252     */
253    public function getBy( $wikiId = self::LOCAL ): int {
254        $this->assertWiki( $wikiId );
255        return 0;
256    }
257
258    /**
259     * @inheritDoc
260     */
261    public function getByName() {
262        return '';
263    }
264
265    /**
266     * @inheritDoc
267     */
268    public function getBlocker(): ?UserIdentity {
269        return null;
270    }
271}