MediaWiki REL1_35
AbstractBlock.php
Go to the documentation of this file.
1<?php
22
25use InvalidArgumentException;
28use Message;
30use Title;
31use User;
32use Wikimedia\IPUtils;
33
38abstract class AbstractBlock {
43 protected $mReason;
44
46 protected $reason;
47
53
58 public $mExpiry = '';
59
61 protected $mBlockEmail = false;
62
64 protected $allowUsertalk = false;
65
67 protected $blockCreateAccount = false;
68
73 public $mHideName = false;
74
76 protected $target;
77
82 protected $type;
83
85 protected $isSitewide = true;
86
87 # TYPE constants
88 public const TYPE_USER = 1;
89 public const TYPE_IP = 2;
90 public const TYPE_RANGE = 3;
91 public const TYPE_AUTO = 4;
92 public const TYPE_ID = 5;
93
103 public function __construct( array $options = [] ) {
104 $defaults = [
105 'address' => '',
106 'reason' => '',
107 'timestamp' => '',
108 'hideName' => false,
109 ];
110
111 $options += $defaults;
112
113 $this->setTarget( $options['address'] );
114
115 $this->setReason( $options['reason'] );
116 $this->setTimestamp( wfTimestamp( TS_MW, $options['timestamp'] ) );
117 $this->setHideName( (bool)$options['hideName'] );
118 }
119
125 abstract public function getBy();
126
132 abstract public function getByName();
133
138 public function getId() {
139 return null;
140 }
141
149 abstract public function getIdentifier();
150
161 public function getReason() {
162 $language = RequestContext::getMain()->getLanguage();
163 return $this->reason->message->inLanguage( $language )->plain();
164 }
165
172 public function getReasonComment() {
173 return $this->reason;
174 }
175
182 public function setReason( $reason ) {
183 $this->reason = CommentStoreComment::newUnsavedComment( $reason );
184 }
185
192 public function getHideName() {
193 return $this->mHideName;
194 }
195
202 public function setHideName( $hideName ) {
203 $this->mHideName = $hideName;
204 }
205
215 public function isSitewide( $x = null ) {
216 return wfSetVar( $this->isSitewide, $x );
217 }
218
228 public function isCreateAccountBlocked( $x = null ) {
229 return wfSetVar( $this->blockCreateAccount, $x );
230 }
231
241 public function isEmailBlocked( $x = null ) {
242 return wfSetVar( $this->mBlockEmail, $x );
243 }
244
254 public function isUsertalkEditAllowed( $x = null ) {
255 return wfSetVar( $this->allowUsertalk, $x );
256 }
257
269 public function appliesToRight( $right ) {
270 $config = RequestContext::getMain()->getConfig();
271 $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
272
273 $res = null;
274 switch ( $right ) {
275 case 'edit':
276 // TODO: fix this case to return proper value
277 $res = true;
278 break;
279 case 'createaccount':
280 $res = $this->isCreateAccountBlocked();
281 break;
282 case 'sendemail':
283 $res = $this->isEmailBlocked();
284 break;
285 case 'upload':
286 // Until T6995 is completed
287 $res = $this->isSitewide();
288 break;
289 case 'read':
290 $res = false;
291 break;
292 case 'purge':
293 $res = false;
294 break;
295 }
296 if ( !$res && $blockDisablesLogin ) {
297 // If a block would disable login, then it should
298 // prevent any right that all users cannot do
299 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
300 $anon = new User;
301 $res = $permissionManager->userHasRight( $anon, $right ) ? $res : true;
302 }
303
304 return $res;
305 }
306
316 public function prevents( $action, $x = null ) {
317 wfDeprecated( __METHOD__, '1.33' );
318 $config = RequestContext::getMain()->getConfig();
319 $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
320 $blockAllowsUTEdit = $config->get( 'BlockAllowsUTEdit' );
321
322 $res = null;
323 switch ( $action ) {
324 case 'edit':
325 # For now... <evil laugh>
326 $res = true;
327 break;
328 case 'createaccount':
329 $res = wfSetVar( $this->blockCreateAccount, $x );
330 break;
331 case 'sendemail':
332 $res = wfSetVar( $this->mBlockEmail, $x );
333 break;
334 case 'upload':
335 // Until T6995 is completed
336 $res = $this->isSitewide();
337 break;
338 case 'editownusertalk':
339 // NOTE: this check is not reliable on partial blocks
340 // since partially blocked users are always allowed to edit
341 // their own talk page unless a restriction exists on the
342 // page or User_talk: namespace
343 wfSetVar( $this->allowUsertalk, $x === null ? null : !$x );
344 $res = !$this->isUsertalkEditAllowed();
345
346 // edit own user talk can be disabled by config
347 if ( !$blockAllowsUTEdit ) {
348 $res = true;
349 }
350 break;
351 case 'read':
352 $res = false;
353 break;
354 case 'purge':
355 $res = false;
356 break;
357 }
358 if ( !$res && $blockDisablesLogin ) {
359 // If a block would disable login, then it should
360 // prevent any action that all users cannot do
361 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
362 $anon = new User;
363 $res = $permissionManager->userHasRight( $anon, $action ) ? $res : true;
364 }
365
366 return $res;
367 }
368
380 public static function parseTarget( $target ) {
381 # We may have been through this before
382 if ( $target instanceof UserIdentity ) {
383 $userObj = User::newFromIdentity( $target );
384 if ( IPUtils::isValid( $target->getName() ) ) {
385 return [ $userObj, self::TYPE_IP ];
386 } else {
387 return [ $userObj, self::TYPE_USER ];
388 }
389 } elseif ( $target === null ) {
390 return [ null, null ];
391 }
392
393 $target = trim( $target );
394
395 if ( IPUtils::isValid( $target ) ) {
396 # We can still create a User if it's an IP address, but we need to turn
397 # off validation checking (which would exclude IP addresses)
398 return [
399 User::newFromName( IPUtils::sanitizeIP( $target ), false ),
401 ];
402
403 } elseif ( IPUtils::isValidRange( $target ) ) {
404 # Can't create a User from an IP range
405 return [ IPUtils::sanitizeRange( $target ), self::TYPE_RANGE ];
406 }
407
408 # Consider the possibility that this is not a username at all
409 # but actually an old subpage (T31797)
410 if ( strpos( $target, '/' ) !== false ) {
411 # An old subpage, drill down to the user behind it
412 $target = explode( '/', $target )[0];
413 }
414
415 $userObj = User::newFromName( $target );
416 if ( $userObj instanceof User ) {
417 # Note that since numbers are valid usernames, a $target of "12345" will be
418 # considered a User. If you want to pass a block ID, prepend a hash "#12345",
419 # since hash characters are not valid in usernames or titles generally.
420 return [ $userObj, self::TYPE_USER ];
421
422 } elseif ( preg_match( '/^#\d+$/', $target ) ) {
423 # Autoblock reference in the form "#12345"
424 return [ substr( $target, 1 ), self::TYPE_AUTO ];
425
426 } else {
427 return [ null, null ];
428 }
429 }
430
435 public function getType() {
436 return $this->type;
437 }
438
449 public function getTargetAndType() {
450 return [ $this->getTarget(), $this->getType() ];
451 }
452
459 public function getTarget() {
460 return $this->target;
461 }
462
469 public function getExpiry() {
470 return $this->mExpiry;
471 }
472
479 public function setExpiry( $expiry ) {
480 $this->mExpiry = $expiry;
481 }
482
489 public function getTimestamp() {
490 return $this->mTimestamp;
491 }
492
499 public function setTimestamp( $timestamp ) {
500 $this->mTimestamp = $timestamp;
501 }
502
507 public function setTarget( $target ) {
508 list( $this->target, $this->type ) = static::parseTarget( $target );
509 }
510
516 public function getBlocker() {
517 wfDeprecated( __METHOD__, '1.35' );
518 return null;
519 }
520
526 public function setBlocker( $user ) {
527 wfDeprecated( __METHOD__, '1.35' );
528 }
529
539 public function getPermissionsError( IContextSource $context ) {
541 ->getBlockErrorFormatter()->getMessage(
542 $this,
543 $context->getUser(),
544 $context->getLanguage(),
545 $context->getRequest()->getIP()
546 );
547 return array_merge( [ [ $message->getKey() ], $message->getParams() ] );
548 }
549
559 public function getBlockErrorParams( IContextSource $context ) {
560 wfDeprecated( __METHOD__, '1.35' );
562 ->getBlockErrorFormatter()->getMessage(
563 $this,
564 $context->getUser(),
565 $context->getLanguage(),
566 $context->getRequest()->getIP()
567 )->getParams();
568 }
569
597 public function appliesToUsertalk( Title $usertalk = null ) {
598 if ( !$usertalk ) {
599 if ( $this->target instanceof User ) {
600 $usertalk = $this->target->getTalkPage();
601 } else {
602 throw new InvalidArgumentException(
603 '$usertalk must be provided if block target is not a user/IP'
604 );
605 }
606 }
607
608 if ( $usertalk->getNamespace() !== NS_USER_TALK ) {
609 throw new InvalidArgumentException(
610 '$usertalk must be a user talk page'
611 );
612 }
613
614 if ( !$this->isSitewide() ) {
615 if ( $this->appliesToPage( $usertalk->getArticleID() ) ) {
616 return true;
617 }
618 if ( !$this->appliesToNamespace( NS_USER_TALK ) ) {
619 return false;
620 }
621 }
622
623 // This is a type of block which uses the ipb_allow_usertalk
624 // flag. The flag can still be overridden by global configs.
625 $config = RequestContext::getMain()->getConfig();
626 if ( !$config->get( 'BlockAllowsUTEdit' ) ) {
627 return true;
628 }
629 return !$this->isUsertalkEditAllowed();
630 }
631
643 public function appliesToTitle( Title $title ) {
644 return $this->isSitewide();
645 }
646
655 public function appliesToNamespace( $ns ) {
656 return $this->isSitewide();
657 }
658
672 public function appliesToPage( $pageId ) {
673 return $this->isSitewide();
674 }
675
685 public function shouldTrackWithCookie( $isAnon ) {
686 wfDeprecated( __METHOD__, '1.34' );
687 return false;
688 }
689
696 public function appliesToPasswordReset() {
697 return $this->isCreateAccountBlocked();
698 }
699
700}
wfSetVar(&$dest, $source, $force=false)
Sets dest to source and returns the original value of dest If source is NULL, it just returns the val...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
CommentStoreComment represents a comment stored by CommentStore.
appliesToTitle(Title $title)
Checks if a block applies to a particular title.
isUsertalkEditAllowed( $x=null)
Get or set the flag indicating whether this block blocks the target from editing their own user talk ...
getTarget()
Get the target for this particular block.
getBy()
Get the user id of the blocking sysop.
static parseTarget( $target)
From an existing block, get the target and the type of target.
setTarget( $target)
Set the target for this block, and update $this->type accordingly.
prevents( $action, $x=null)
Get/set whether the block prevents a given action.
int null $type
AbstractBlock::TYPE_ constant.
getIdentifier()
Get the information that identifies this block, such that a user could look up everything that can be...
getHideName()
Get whether the block hides the target's username.
getBlocker()
Get the user who implemented this block.
shouldTrackWithCookie( $isAnon)
Check if the block should be tracked with a cookie.
appliesToUsertalk(Title $usertalk=null)
Determine whether the block allows the user to edit their own user talk page.
appliesToNamespace( $ns)
Checks if a block applies to a particular namespace.
setHideName( $hideName)
Set whether ths block hides the target's username.
setTimestamp( $timestamp)
Set the timestamp indicating when the block was created.
appliesToPasswordReset()
Check if the block prevents a user from resetting their password.
appliesToRight( $right)
Determine whether the block prevents a given right.
getByName()
Get the username of the blocking sysop.
isCreateAccountBlocked( $x=null)
Get or set the flag indicating whether this block blocks the target from creating an account.
getTimestamp()
Get the timestamp indicating when the block was created.
getReason()
Get the reason given for creating the block, as a string.
getReasonComment()
Get the reason for creating the block.
getType()
Get the type of target for this particular block.
__construct(array $options=[])
Create a new block with specified parameters on a user, IP or IP range.
getPermissionsError(IContextSource $context)
Get the key and parameters for the corresponding error message.
getBlockErrorParams(IContextSource $context)
Get block information used in different block error messages.
CommentStoreComment $reason
appliesToPage( $pageId)
Checks if a block applies to a particular page.
isEmailBlocked( $x=null)
Get or set the flag indicating whether this block blocks the target from sending emails.
isSitewide( $x=null)
Indicates that the block is a sitewide block.
getExpiry()
Get the block expiry time.
setReason( $reason)
Set the reason for creating the block.
setBlocker( $user)
Set the user who implemented (or will implement) this block.
setExpiry( $expiry)
Set the block expiry time.
getTargetAndType()
Get the target and target type for this particular block.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:161
Group all the pieces relevant to the context of a request into one instance @newable.
Represents a title within MediaWiki.
Definition Title.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2150
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition User.php:597
const NS_USER_TALK
Definition Defines.php:73
Interface for objects which can provide a MediaWiki context on request.
Interface for objects representing user identity.