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