MediaWiki  master
AbstractBlock.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Block;
22 
24 use IContextSource;
25 use InvalidArgumentException;
28 use Message;
29 use RequestContext;
30 use Title;
31 use User;
32 use Wikimedia\IPUtils;
33 
38 abstract class AbstractBlock {
43  protected $mReason;
44 
46  protected $reason;
47 
52  public $mTimestamp;
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 ) {
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 
540  $message = MediaWikiServices::getInstance()
541  ->getBlockErrorFormatter()->getMessage(
542  $this,
543  $context->getUser(),
545  $context->getRequest()->getIP()
546  );
547  return array_merge( [ [ $message->getKey() ], $message->getParams() ] );
548  }
549 
560  wfDeprecated( __METHOD__, '1.35' );
562  ->getBlockErrorFormatter()->getMessage(
563  $this,
564  $context->getUser(),
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 }
MediaWiki\Block\AbstractBlock\appliesToNamespace
appliesToNamespace( $ns)
Checks if a block applies to a particular namespace.
Definition: AbstractBlock.php:655
MediaWiki\Block\AbstractBlock\getBy
getBy()
Get the user id of the blocking sysop.
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:67
MediaWiki\Block\AbstractBlock\$mBlockEmail
bool $mBlockEmail
Definition: AbstractBlock.php:61
MediaWiki\Block\AbstractBlock\$target
User string null $target
Definition: AbstractBlock.php:76
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
wfSetVar
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...
Definition: GlobalFunctions.php:1543
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1808
MediaWiki\Block\AbstractBlock\setBlocker
setBlocker( $user)
Set the user who implemented (or will implement) this block.
Definition: AbstractBlock.php:526
MediaWiki\Block\AbstractBlock\isCreateAccountBlocked
isCreateAccountBlocked( $x=null)
Get or set the flag indicating whether this block blocks the target from creating an account.
Definition: AbstractBlock.php:228
MediaWiki\Block\AbstractBlock\TYPE_AUTO
const TYPE_AUTO
Definition: AbstractBlock.php:91
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:538
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:594
MediaWiki\Block\AbstractBlock\getId
getId()
Get the block ID.
Definition: AbstractBlock.php:138
$res
$res
Definition: testCompression.php:57
MediaWiki\Block\AbstractBlock\getTargetAndType
getTargetAndType()
Get the target and target type for this particular block.
Definition: AbstractBlock.php:449
MediaWiki\Block\AbstractBlock\appliesToUsertalk
appliesToUsertalk(Title $usertalk=null)
Determine whether the block allows the user to edit their own user talk page.
Definition: AbstractBlock.php:597
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:185
MediaWiki\Block\AbstractBlock\TYPE_RANGE
const TYPE_RANGE
Definition: AbstractBlock.php:90
MediaWiki\Block\AbstractBlock\appliesToPasswordReset
appliesToPasswordReset()
Check if the block prevents a user from resetting their password.
Definition: AbstractBlock.php:696
MediaWiki\Block\AbstractBlock\$mTimestamp
string $mTimestamp
Definition: AbstractBlock.php:52
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1026
MediaWiki\Block\AbstractBlock\TYPE_ID
const TYPE_ID
Definition: AbstractBlock.php:92
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
MediaWiki\Block\AbstractBlock\isEmailBlocked
isEmailBlocked( $x=null)
Get or set the flag indicating whether this block blocks the target from sending emails.
Definition: AbstractBlock.php:241
MediaWiki\Block\AbstractBlock\getReasonComment
getReasonComment()
Get the reason for creating the block.
Definition: AbstractBlock.php:172
MediaWiki\Block\AbstractBlock\TYPE_IP
const TYPE_IP
Definition: AbstractBlock.php:89
$title
$title
Definition: testCompression.php:38
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:38
MediaWiki\Block\AbstractBlock\getType
getType()
Get the type of target for this particular block.
Definition: AbstractBlock.php:435
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:215
MediaWiki\Block\AbstractBlock\shouldTrackWithCookie
shouldTrackWithCookie( $isAnon)
Check if the block should be tracked with a cookie.
Definition: AbstractBlock.php:685
MediaWiki\Block\AbstractBlock\getIdentifier
getIdentifier()
Get the information that identifies this block, such that a user could look up everything that can be...
MediaWiki\Block\AbstractBlock\$isSitewide
bool $isSitewide
Definition: AbstractBlock.php:85
MediaWiki\Block\AbstractBlock\setTimestamp
setTimestamp( $timestamp)
Set the timestamp indicating when the block was created.
Definition: AbstractBlock.php:499
MediaWiki\Block\AbstractBlock\parseTarget
static parseTarget( $target)
From an existing block, get the target and the type of target.
Definition: AbstractBlock.php:380
MediaWiki\Block\AbstractBlock\TYPE_USER
const TYPE_USER
Definition: AbstractBlock.php:88
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
MediaWiki\Block\AbstractBlock\getBlocker
getBlocker()
Get the user who implemented this block.
Definition: AbstractBlock.php:516
MediaWiki\Block\AbstractBlock\__construct
__construct(array $options=[])
Create a new block with specified parameters on a user, IP or IP range.
Definition: AbstractBlock.php:103
MediaWiki\Block\AbstractBlock\$blockCreateAccount
bool $blockCreateAccount
Definition: AbstractBlock.php:67
MediaWiki\Block\AbstractBlock\getPermissionsError
getPermissionsError(IContextSource $context)
Get the key and parameters for the corresponding error message.
Definition: AbstractBlock.php:539
MediaWiki\Block\AbstractBlock\appliesToTitle
appliesToTitle(Title $title)
Checks if a block applies to a particular title.
Definition: AbstractBlock.php:643
MediaWiki\Block\AbstractBlock\$mReason
$mReason
Definition: AbstractBlock.php:43
MediaWiki\Block\AbstractBlock\getBlockErrorParams
getBlockErrorParams(IContextSource $context)
Get block information used in different block error messages.
Definition: AbstractBlock.php:559
MediaWiki\Block\AbstractBlock\setExpiry
setExpiry( $expiry)
Set the block expiry time.
Definition: AbstractBlock.php:479
MediaWiki\Block\AbstractBlock\$mExpiry
string $mExpiry
Definition: AbstractBlock.php:58
IContextSource\getUser
getUser()
MediaWiki\Block\AbstractBlock\getExpiry
getExpiry()
Get the block expiry time.
Definition: AbstractBlock.php:469
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:453
MediaWiki\Block\AbstractBlock\prevents
prevents( $action, $x=null)
Get/set whether the block prevents a given action.
Definition: AbstractBlock.php:316
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
MediaWiki\Block\AbstractBlock\getTimestamp
getTimestamp()
Get the timestamp indicating when the block was created.
Definition: AbstractBlock.php:489
MediaWiki\$action
string $action
Cache what action this request is.
Definition: MediaWiki.php:45
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Block\AbstractBlock\appliesToRight
appliesToRight( $right)
Determine whether the block prevents a given right.
Definition: AbstractBlock.php:269
MediaWiki\Block\AbstractBlock\setReason
setReason( $reason)
Set the reason for creating the block.
Definition: AbstractBlock.php:182
MediaWiki\Block\AbstractBlock\appliesToPage
appliesToPage( $pageId)
Checks if a block applies to a particular page.
Definition: AbstractBlock.php:672
MediaWiki\$config
Config $config
Definition: MediaWiki.php:42
Message
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:161
MediaWiki\Block\AbstractBlock\$reason
CommentStoreComment $reason
Definition: AbstractBlock.php:46
IContextSource\getRequest
getRequest()
MediaWiki\Block\AbstractBlock\getHideName
getHideName()
Get whether the block hides the target's username.
Definition: AbstractBlock.php:192
MediaWiki\Block\AbstractBlock\$type
int null $type
AbstractBlock::TYPE_ constant.
Definition: AbstractBlock.php:82
MediaWiki\Block\AbstractBlock\$mHideName
bool $mHideName
Definition: AbstractBlock.php:73
MediaWiki\Block\AbstractBlock
Definition: AbstractBlock.php:38
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:40
MediaWiki\Block\AbstractBlock\setTarget
setTarget( $target)
Set the target for this block, and update $this->type accordingly.
Definition: AbstractBlock.php:507
MediaWiki\Block\AbstractBlock\getByName
getByName()
Get the username of the blocking sysop.
MediaWiki\Block\AbstractBlock\setHideName
setHideName( $hideName)
Set whether ths block hides the target's username.
Definition: AbstractBlock.php:202
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
MediaWiki\Block\AbstractBlock\getTarget
getTarget()
Get the target for this particular block.
Definition: AbstractBlock.php:459
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:1954
MediaWiki\Block\AbstractBlock\isUsertalkEditAllowed
isUsertalkEditAllowed( $x=null)
Get or set the flag indicating whether this block blocks the target from editing their own user talk ...
Definition: AbstractBlock.php:254
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30
IContextSource\getLanguage
getLanguage()
MediaWiki\Block\AbstractBlock\$allowUsertalk
bool $allowUsertalk
Definition: AbstractBlock.php:64
MediaWiki\Block\AbstractBlock\getReason
getReason()
Get the reason given for creating the block, as a string.
Definition: AbstractBlock.php:161