Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
7.63% covered (danger)
7.63%
9 / 118
16.13% covered (danger)
16.13%
5 / 31
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractBlock
7.63% covered (danger)
7.63%
9 / 118
16.13% covered (danger)
16.13%
5 / 31
2897.50
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
30
 getBy
n/a
0 / 0
n/a
0 / 0
0
 getByName
n/a
0 / 0
n/a
0 / 0
0
 getId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getReasonComment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setReason
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHideName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setHideName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isSitewide
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isCreateAccountBlocked
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isEmailBlocked
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isUsertalkEditAllowed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isHardblock
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 appliesToRight
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
90
 getTarget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRedactedTarget
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getTargetUserIdentity
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getTargetName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isBlocking
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getExpiry
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isIndefinite
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setExpiry
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTarget
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 getWikiId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 appliesToUsertalk
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
72
 appliesToTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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
 appliesToPasswordReset
0.00% covered (danger)
0.00%
0 / 1
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
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Block;
22
23use InvalidArgumentException;
24use LogicException;
25use MediaWiki\CommentStore\CommentStoreComment;
26use MediaWiki\DAO\WikiAwareEntityTrait;
27use MediaWiki\MainConfigNames;
28use MediaWiki\MediaWikiServices;
29use MediaWiki\Message\Message;
30use MediaWiki\Title\Title;
31use MediaWiki\User\UserIdentity;
32
33/**
34 * @note Extensions should not subclass this, as MediaWiki currently does not
35 *   support custom block types.
36 * @since 1.34 Factored out from DatabaseBlock (previously Block).
37 */
38abstract class AbstractBlock implements Block {
39    use WikiAwareEntityTrait;
40
41    /** @var CommentStoreComment */
42    protected $reason;
43
44    /** @var string */
45    protected $timestamp = '';
46
47    /** @var string */
48    protected $expiry = '';
49
50    /** @var bool */
51    protected $blockEmail = false;
52
53    /** @var bool */
54    protected $allowUsertalk = false;
55
56    /** @var bool */
57    protected $blockCreateAccount = false;
58
59    /** @var bool */
60    protected $hideName = false;
61
62    /** @var bool */
63    protected $isHardblock;
64
65    /** @var BlockTarget|null */
66    protected $target;
67
68    /** @var bool */
69    protected $isSitewide = true;
70
71    /** @var string|false */
72    protected $wikiId;
73
74    /**
75     * Create a new block with specified parameters on a user, IP or IP range.
76     *
77     * @param array $options Parameters of the block, with supported options:
78     *  - target: (BlockTarget) The target object (since 1.44)
79     *  - address: (string|UserIdentity) Target user name, user identity object,
80     *     IP address or IP range.
81     *  - wiki: (string|false) The wiki the block has been issued in,
82     *    self::LOCAL for the local wiki (since 1.38)
83     *  - reason: (string|Message|CommentStoreComment) Reason for the block
84     *  - timestamp: (string) The time at which the block comes into effect,
85     *    in any format supported by wfTimestamp()
86     *  - decodedTimestamp: (string) The timestamp in MW 14-character format
87     *  - hideName: (bool) Hide the target user name
88     *  - anonOnly: (bool) Used if the target is an IP address. The block only
89     *    applies to anon and temporary users using this IP address, and not to
90     *    logged-in users.
91     */
92    public function __construct( array $options = [] ) {
93        $defaults = [
94            'wiki'            => self::LOCAL,
95            'reason'          => '',
96            'timestamp'       => '',
97            'hideName'        => false,
98            'anonOnly'        => false,
99        ];
100
101        $options += $defaults;
102
103        $this->wikiId = $options['wiki'];
104        if ( isset( $options['target'] ) ) {
105            if ( !( $options['target'] instanceof BlockTarget ) ) {
106                throw new InvalidArgumentException( 'Invalid block target' );
107            }
108            $this->setTarget( $options['target'] );
109        } elseif ( isset( $options['address'] ) ) {
110            $this->setTarget( $options['address'] );
111        } else {
112            $this->setTarget( null );
113        }
114        $this->setReason( $options['reason'] );
115        if ( isset( $options['decodedTimestamp'] ) ) {
116            $this->setTimestamp( $options['decodedTimestamp'] );
117        } else {
118            $this->setTimestamp( wfTimestamp( TS_MW, $options['timestamp'] ) );
119        }
120        $this->setHideName( (bool)$options['hideName'] );
121        $this->isHardblock( !$options['anonOnly'] );
122    }
123
124    /**
125     * Get the user id of the blocking sysop
126     *
127     * @param string|false $wikiId (since 1.38)
128     * @return int (0 for foreign users)
129     */
130    abstract public function getBy( $wikiId = self::LOCAL ): int;
131
132    /**
133     * Get the username of the blocking sysop
134     *
135     * @return string
136     */
137    abstract public function getByName();
138
139    /**
140     * @inheritDoc
141     */
142    public function getId( $wikiId = self::LOCAL ): ?int {
143        $this->assertWiki( $wikiId );
144        return null;
145    }
146
147    /**
148     * Get the reason for creating the block.
149     *
150     * @since 1.35
151     * @return CommentStoreComment
152     */
153    public function getReasonComment(): CommentStoreComment {
154        return $this->reason;
155    }
156
157    /**
158     * Set the reason for creating the block.
159     *
160     * @since 1.33
161     * @param string|Message|CommentStoreComment $reason
162     */
163    public function setReason( $reason ) {
164        $this->reason = CommentStoreComment::newUnsavedComment( $reason );
165    }
166
167    /**
168     * Get whether the block hides the target's username
169     *
170     * @since 1.33
171     * @return bool The block hides the username
172     */
173    public function getHideName() {
174        return $this->hideName;
175    }
176
177    /**
178     * Set whether the block hides the target's username
179     *
180     * @since 1.33
181     * @param bool $hideName The block hides the username
182     */
183    public function setHideName( $hideName ) {
184        $this->hideName = $hideName;
185    }
186
187    /**
188     * Indicates that the block is a sitewide block. This means the user is
189     * prohibited from editing any page on the site (other than their own talk
190     * page).
191     *
192     * @since 1.33
193     * @param null|bool $x
194     * @return bool
195     */
196    public function isSitewide( $x = null ): bool {
197        return wfSetVar( $this->isSitewide, $x );
198    }
199
200    /**
201     * Get or set the flag indicating whether this block blocks the target from
202     * creating an account. (Note that the flag may be overridden depending on
203     * global configs.)
204     *
205     * @since 1.33
206     * @param null|bool $x Value to set (if null, just get the property value)
207     * @return bool Value of the property
208     */
209    public function isCreateAccountBlocked( $x = null ): bool {
210        return wfSetVar( $this->blockCreateAccount, $x );
211    }
212
213    /**
214     * Get or set the flag indicating whether this block blocks the target from
215     * sending emails. (Note that the flag may be overridden depending on
216     * global configs.)
217     *
218     * @since 1.33
219     * @param null|bool $x Value to set (if null, just get the property value)
220     * @return bool Value of the property
221     */
222    public function isEmailBlocked( $x = null ) {
223        return wfSetVar( $this->blockEmail, $x );
224    }
225
226    /**
227     * Get or set the flag indicating whether this block blocks the target from
228     * editing their own user talk page. (Note that the flag may be overridden
229     * depending on global configs.)
230     *
231     * @since 1.33
232     * @param null|bool $x Value to set (if null, just get the property value)
233     * @return bool Value of the property
234     */
235    public function isUsertalkEditAllowed( $x = null ) {
236        return wfSetVar( $this->allowUsertalk, $x );
237    }
238
239    /**
240     * Get/set whether the block is a hard block (affects logged-in users on a
241     * given IP/range).
242     *
243     * Note that temporary users are not considered logged-in here - they are
244     * always blocked by IP-address blocks.
245     *
246     * Note that user blocks are always hard blocks, since the target is logged
247     * in by definition.
248     *
249     * @since 1.36 Moved up from DatabaseBlock
250     * @param bool|null $x
251     * @return bool
252     */
253    public function isHardblock( $x = null ): bool {
254        wfSetVar( $this->isHardblock, $x );
255
256        return $this->getType() == self::TYPE_USER
257            ? true
258            : $this->isHardblock;
259    }
260
261    /**
262     * Determine whether the block prevents a given right. A right may be
263     * allowed or disallowed by default, or determined from a property on the
264     * block object. For certain rights, the property may be overridden
265     * according to global configs.
266     *
267     * @since 1.33
268     * @param string $right
269     * @return bool|null The block applies to the right, or null if
270     *  unsure (e.g. unrecognized right or unset property)
271     */
272    public function appliesToRight( $right ) {
273        $blockDisablesLogin = MediaWikiServices::getInstance()->getMainConfig()
274            ->get( MainConfigNames::BlockDisablesLogin );
275
276        $res = null;
277        switch ( $right ) {
278            case 'autocreateaccount':
279            case 'createaccount':
280                $res = $this->isCreateAccountBlocked();
281                break;
282            case 'sendemail':
283                $res = $this->isEmailBlocked();
284                break;
285            case 'upload':
286                // Sitewide blocks always block upload. This may be overridden in a subclass.
287                $res = $this->isSitewide();
288                break;
289            case 'read':
290                $res = false;
291                break;
292        }
293        if ( !$res && $blockDisablesLogin ) {
294            // If a block would disable login, then it should
295            // prevent any right that all users cannot do
296            $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
297            $anon = MediaWikiServices::getInstance()->getUserFactory()->newAnonymous();
298            $res = $permissionManager->userHasRight( $anon, $right ) ? $res : true;
299        }
300
301        return $res;
302    }
303
304    public function getTarget(): ?BlockTarget {
305        return $this->target;
306    }
307
308    public function getRedactedTarget(): ?BlockTarget {
309        $target = $this->getTarget();
310        if ( $this->getType() === Block::TYPE_AUTO
311            && !( $target instanceof AutoBlockTarget )
312        ) {
313            $id = $this->getId( $this->wikiId );
314            if ( $id === null ) {
315                throw new LogicException( 'no ID available for autoblock redaction' );
316            }
317            $target = new AutoBlockTarget( $id, $this->wikiId );
318        }
319        return $target;
320    }
321
322    /**
323     * Get the type of target for this particular block.
324     * @return int|null AbstractBlock::TYPE_ constant
325     */
326    public function getType(): ?int {
327        return $this->target ? $this->target->getType() : null;
328    }
329
330    /**
331     * @since 1.37
332     * @return ?UserIdentity
333     */
334    public function getTargetUserIdentity(): ?UserIdentity {
335        return $this->target instanceof BlockTargetWithUserIdentity
336            ? $this->target->getUserIdentity() : null;
337    }
338
339    /**
340     * @since 1.37
341     * @return string
342     */
343    public function getTargetName(): string {
344        return (string)$this->target;
345    }
346
347    /**
348     * @param BlockTarget|UserIdentity|string $target
349     *
350     * @return bool
351     * @since 1.37
352     */
353    public function isBlocking( $target ): bool {
354        $targetName = $target instanceof UserIdentity
355            ? $target->getName()
356            : (string)$target;
357
358        return $targetName === $this->getTargetName();
359    }
360
361    /**
362     * Get the block expiry time
363     *
364     * @since 1.19
365     * @return string
366     */
367    public function getExpiry(): string {
368        return $this->expiry;
369    }
370
371    /** @inheritDoc */
372    public function isIndefinite(): bool {
373        return wfIsInfinity( $this->getExpiry() );
374    }
375
376    /**
377     * Set the block expiry time
378     *
379     * @since 1.33
380     * @param string $expiry
381     */
382    public function setExpiry( $expiry ) {
383        // Force string so getExpiry() return typehint doesn't break things
384        $this->expiry = (string)$expiry;
385    }
386
387    /**
388     * Get the timestamp indicating when the block was created
389     *
390     * @since 1.33
391     * @return string
392     */
393    public function getTimestamp(): string {
394        return $this->timestamp;
395    }
396
397    /**
398     * Set the timestamp indicating when the block was created
399     *
400     * @since 1.33
401     * @param string $timestamp
402     */
403    public function setTimestamp( $timestamp ) {
404        // Force string so getTimestamp() return typehint doesn't break things
405        $this->timestamp = (string)$timestamp;
406    }
407
408    /**
409     * Set the target for this block
410     * @param BlockTarget|string|UserIdentity|null $target
411     */
412    public function setTarget( $target ) {
413        // Small optimization to make this code testable, this is what would happen anyway
414        if ( $target === '' || $target === null ) {
415            $this->target = null;
416        } elseif ( $target instanceof BlockTarget ) {
417            $this->assertWiki( $target->getWikiId() );
418            $this->target = $target;
419        } else {
420            $parsedTarget = MediaWikiServices::getInstance()
421                ->getCrossWikiBlockTargetFactory()
422                ->getFactory( $this->wikiId )
423                ->newFromLegacyUnion( $target );
424            $this->assertWiki( $parsedTarget->getWikiId() );
425            $this->target = $parsedTarget;
426        }
427    }
428
429    /**
430     * @since 1.38
431     * @return string|false
432     */
433    public function getWikiId() {
434        return $this->wikiId;
435    }
436
437    /**
438     * Determine whether the block allows the user to edit their own
439     * user talk page. This is done separately from
440     * AbstractBlock::appliesToRight because there is no right for
441     * editing one's own user talk page and because the user's talk
442     * page needs to be passed into the block object, which is unaware
443     * of the user.
444     *
445     * The bl_allow_usertalk flag (which corresponds to the property
446     * allowUsertalk) is used on sitewide blocks and partial blocks
447     * that contain a namespace restriction on the user talk namespace,
448     * but do not contain a page restriction on the user's talk page.
449     * For all other (i.e. most) partial blocks, the flag is ignored,
450     * and the user can always edit their user talk page unless there
451     * is a page restriction on their user talk page, in which case
452     * they can never edit it. (Ideally the flag would be stored as
453     * null in these cases, but the database field isn't nullable.)
454     *
455     * This method does not validate that the passed in talk page belongs to the
456     * block target since the target (an IP) might not be the same as the user's
457     * talk page (if they are logged in).
458     *
459     * @since 1.33
460     * @param Title|null $usertalk The user's user talk page. If null,
461     *  and if the target is a User, the target's userpage is used
462     * @return bool The user can edit their talk page
463     */
464    public function appliesToUsertalk( ?Title $usertalk = null ) {
465        if ( !$usertalk ) {
466            if ( $this->target instanceof BlockTargetWithUserPage ) {
467                $usertalk = Title::makeTitle(
468                    NS_USER_TALK,
469                    $this->target->getUserPage()->getDBkey()
470                );
471            } else {
472                throw new InvalidArgumentException(
473                    '$usertalk must be provided if block target is not a user/IP'
474                );
475            }
476        }
477
478        if ( $usertalk->getNamespace() !== NS_USER_TALK ) {
479            throw new InvalidArgumentException(
480                '$usertalk must be a user talk page'
481            );
482        }
483
484        if ( !$this->isSitewide() ) {
485            if ( $this->appliesToPage( $usertalk->getArticleID() ) ) {
486                return true;
487            }
488            if ( !$this->appliesToNamespace( NS_USER_TALK ) ) {
489                return false;
490            }
491        }
492
493        // This is a type of block which uses the bl_allow_usertalk
494        // flag. The flag can still be overridden by global configs.
495        if ( !MediaWikiServices::getInstance()->getMainConfig()
496            ->get( MainConfigNames::BlockAllowsUTEdit )
497        ) {
498            return true;
499        }
500        return !$this->isUsertalkEditAllowed();
501    }
502
503    /**
504     * Checks if a block applies to a particular title
505     *
506     * This check does not consider whether `$this->isUsertalkEditAllowed`
507     * returns false, as the identity of the user making the hypothetical edit
508     * isn't known here (particularly in the case of IP hard blocks, range
509     * blocks, and auto-blocks).
510     *
511     * @param Title $title
512     * @return bool
513     */
514    public function appliesToTitle( Title $title ) {
515        return $this->isSitewide();
516    }
517
518    /**
519     * Checks if a block applies to a particular namespace
520     *
521     * @since 1.33
522     *
523     * @param int $ns
524     * @return bool
525     */
526    public function appliesToNamespace( $ns ) {
527        return $this->isSitewide();
528    }
529
530    /**
531     * Checks if a block applies to a particular page
532     *
533     * This check does not consider whether `$this->isUsertalkEditAllowed`
534     * returns false, as the identity of the user making the hypothetical edit
535     * isn't known here (particularly in the case of IP hard blocks, range
536     * blocks, and auto-blocks).
537     *
538     * @since 1.33
539     *
540     * @param int $pageId
541     * @return bool
542     */
543    public function appliesToPage( $pageId ) {
544        return $this->isSitewide();
545    }
546
547    /**
548     * Check if the block prevents a user from resetting their password
549     *
550     * @since 1.33
551     * @return bool The block blocks password reset
552     */
553    public function appliesToPasswordReset() {
554        return $this->isCreateAccountBlocked();
555    }
556
557    /**
558     * @return AbstractBlock[]
559     */
560    public function toArray(): array {
561        return [ $this ];
562    }
563
564}