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 
37 abstract class AbstractBlock {
42  protected $mReason;
43 
45  protected $reason;
46 
51  public $mTimestamp;
52 
57  public $mExpiry = '';
58 
60  protected $mBlockEmail = false;
61 
63  protected $allowUsertalk = false;
64 
66  protected $blockCreateAccount = false;
67 
72  public $mHideName = false;
73 
75  protected $target;
76 
81  protected $type;
82 
84  protected $isSitewide = true;
85 
86  # TYPE constants
87  public const TYPE_USER = 1;
88  public const TYPE_IP = 2;
89  public const TYPE_RANGE = 3;
90  public const TYPE_AUTO = 4;
91  public const TYPE_ID = 5;
92 
102  public function __construct( array $options = [] ) {
103  $defaults = [
104  'address' => '',
105  'reason' => '',
106  'timestamp' => '',
107  'hideName' => false,
108  ];
109 
110  $options += $defaults;
111 
112  $this->setTarget( $options['address'] );
113 
114  $this->setReason( $options['reason'] );
115  $this->setTimestamp( wfTimestamp( TS_MW, $options['timestamp'] ) );
116  $this->setHideName( (bool)$options['hideName'] );
117  }
118 
124  abstract public function getBy();
125 
131  abstract public function getByName();
132 
137  public function getId() {
138  return null;
139  }
140 
148  abstract public function getIdentifier();
149 
160  public function getReason() {
161  $language = RequestContext::getMain()->getLanguage();
162  return $this->reason->message->inLanguage( $language )->plain();
163  }
164 
171  public function getReasonComment() {
172  return $this->reason;
173  }
174 
181  public function setReason( $reason ) {
183  }
184 
191  public function getHideName() {
192  return $this->mHideName;
193  }
194 
201  public function setHideName( $hideName ) {
202  $this->mHideName = $hideName;
203  }
204 
214  public function isSitewide( $x = null ) {
215  return wfSetVar( $this->isSitewide, $x );
216  }
217 
227  public function isCreateAccountBlocked( $x = null ) {
228  return wfSetVar( $this->blockCreateAccount, $x );
229  }
230 
240  public function isEmailBlocked( $x = null ) {
241  return wfSetVar( $this->mBlockEmail, $x );
242  }
243 
253  public function isUsertalkEditAllowed( $x = null ) {
254  return wfSetVar( $this->allowUsertalk, $x );
255  }
256 
268  public function appliesToRight( $right ) {
269  $config = RequestContext::getMain()->getConfig();
270  $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
271 
272  $res = null;
273  switch ( $right ) {
274  case 'edit':
275  // TODO: fix this case to return proper value
276  $res = true;
277  break;
278  case 'createaccount':
279  $res = $this->isCreateAccountBlocked();
280  break;
281  case 'sendemail':
282  $res = $this->isEmailBlocked();
283  break;
284  case 'upload':
285  // Until T6995 is completed
286  $res = $this->isSitewide();
287  break;
288  case 'read':
289  $res = false;
290  break;
291  case 'purge':
292  $res = false;
293  break;
294  }
295  if ( !$res && $blockDisablesLogin ) {
296  // If a block would disable login, then it should
297  // prevent any right that all users cannot do
298  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
299  $anon = new User;
300  $res = $permissionManager->userHasRight( $anon, $right ) ? $res : true;
301  }
302 
303  return $res;
304  }
305 
315  public function prevents( $action, $x = null ) {
316  wfDeprecated( __METHOD__, '1.33' );
317  $config = RequestContext::getMain()->getConfig();
318  $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
319  $blockAllowsUTEdit = $config->get( 'BlockAllowsUTEdit' );
320 
321  $res = null;
322  switch ( $action ) {
323  case 'edit':
324  # For now... <evil laugh>
325  $res = true;
326  break;
327  case 'createaccount':
328  $res = wfSetVar( $this->blockCreateAccount, $x );
329  break;
330  case 'sendemail':
331  $res = wfSetVar( $this->mBlockEmail, $x );
332  break;
333  case 'upload':
334  // Until T6995 is completed
335  $res = $this->isSitewide();
336  break;
337  case 'editownusertalk':
338  // NOTE: this check is not reliable on partial blocks
339  // since partially blocked users are always allowed to edit
340  // their own talk page unless a restriction exists on the
341  // page or User_talk: namespace
342  wfSetVar( $this->allowUsertalk, $x === null ? null : !$x );
343  $res = !$this->isUsertalkEditAllowed();
344 
345  // edit own user talk can be disabled by config
346  if ( !$blockAllowsUTEdit ) {
347  $res = true;
348  }
349  break;
350  case 'read':
351  $res = false;
352  break;
353  case 'purge':
354  $res = false;
355  break;
356  }
357  if ( !$res && $blockDisablesLogin ) {
358  // If a block would disable login, then it should
359  // prevent any action that all users cannot do
360  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
361  $anon = new User;
362  $res = $permissionManager->userHasRight( $anon, $action ) ? $res : true;
363  }
364 
365  return $res;
366  }
367 
379  public static function parseTarget( $target ) {
380  # We may have been through this before
381  if ( $target instanceof UserIdentity ) {
382  $userObj = User::newFromIdentity( $target );
383  if ( IPUtils::isValid( $target->getName() ) ) {
384  return [ $userObj, self::TYPE_IP ];
385  } else {
386  return [ $userObj, self::TYPE_USER ];
387  }
388  } elseif ( $target === null ) {
389  return [ null, null ];
390  }
391 
392  $target = trim( $target );
393 
394  if ( IPUtils::isValid( $target ) ) {
395  # We can still create a User if it's an IP address, but we need to turn
396  # off validation checking (which would exclude IP addresses)
397  return [
398  User::newFromName( IPUtils::sanitizeIP( $target ), false ),
400  ];
401 
402  } elseif ( IPUtils::isValidRange( $target ) ) {
403  # Can't create a User from an IP range
404  return [ IPUtils::sanitizeRange( $target ), self::TYPE_RANGE ];
405  }
406 
407  # Consider the possibility that this is not a username at all
408  # but actually an old subpage (T31797)
409  if ( strpos( $target, '/' ) !== false ) {
410  # An old subpage, drill down to the user behind it
411  $target = explode( '/', $target )[0];
412  }
413 
414  $userObj = User::newFromName( $target );
415  if ( $userObj instanceof User ) {
416  # Note that since numbers are valid usernames, a $target of "12345" will be
417  # considered a User. If you want to pass a block ID, prepend a hash "#12345",
418  # since hash characters are not valid in usernames or titles generally.
419  return [ $userObj, self::TYPE_USER ];
420 
421  } elseif ( preg_match( '/^#\d+$/', $target ) ) {
422  # Autoblock reference in the form "#12345"
423  return [ substr( $target, 1 ), self::TYPE_AUTO ];
424 
425  } else {
426  return [ null, null ];
427  }
428  }
429 
434  public function getType() {
435  return $this->type;
436  }
437 
448  public function getTargetAndType() {
449  return [ $this->getTarget(), $this->getType() ];
450  }
451 
458  public function getTarget() {
459  return $this->target;
460  }
461 
468  public function getExpiry() {
469  return $this->mExpiry;
470  }
471 
478  public function setExpiry( $expiry ) {
479  $this->mExpiry = $expiry;
480  }
481 
488  public function getTimestamp() {
489  return $this->mTimestamp;
490  }
491 
498  public function setTimestamp( $timestamp ) {
499  $this->mTimestamp = $timestamp;
500  }
501 
506  public function setTarget( $target ) {
507  list( $this->target, $this->type ) = static::parseTarget( $target );
508  }
509 
515  public function getBlocker() {
516  wfDeprecated( __METHOD__, '1.35' );
517  return null;
518  }
519 
525  public function setBlocker( $user ) {
526  wfDeprecated( __METHOD__, '1.35' );
527  }
528 
539  $message = MediaWikiServices::getInstance()
540  ->getBlockErrorFormatter()->getMessage(
541  $this,
542  $context->getUser(),
544  $context->getRequest()->getIP()
545  );
546  return array_merge( [ [ $message->getKey() ], $message->getParams() ] );
547  }
548 
559  wfDeprecated( __METHOD__, '1.35' );
561  ->getBlockErrorFormatter()->getMessage(
562  $this,
563  $context->getUser(),
565  $context->getRequest()->getIP()
566  )->getParams();
567  }
568 
596  public function appliesToUsertalk( Title $usertalk = null ) {
597  if ( !$usertalk ) {
598  if ( $this->target instanceof User ) {
599  $usertalk = $this->target->getTalkPage();
600  } else {
601  throw new InvalidArgumentException(
602  '$usertalk must be provided if block target is not a user/IP'
603  );
604  }
605  }
606 
607  if ( $usertalk->getNamespace() !== NS_USER_TALK ) {
608  throw new InvalidArgumentException(
609  '$usertalk must be a user talk page'
610  );
611  }
612 
613  if ( !$this->isSitewide() ) {
614  if ( $this->appliesToPage( $usertalk->getArticleID() ) ) {
615  return true;
616  }
617  if ( !$this->appliesToNamespace( NS_USER_TALK ) ) {
618  return false;
619  }
620  }
621 
622  // This is a type of block which uses the ipb_allow_usertalk
623  // flag. The flag can still be overridden by global configs.
624  $config = RequestContext::getMain()->getConfig();
625  if ( !$config->get( 'BlockAllowsUTEdit' ) ) {
626  return true;
627  }
628  return !$this->isUsertalkEditAllowed();
629  }
630 
642  public function appliesToTitle( Title $title ) {
643  return $this->isSitewide();
644  }
645 
654  public function appliesToNamespace( $ns ) {
655  return $this->isSitewide();
656  }
657 
671  public function appliesToPage( $pageId ) {
672  return $this->isSitewide();
673  }
674 
684  public function shouldTrackWithCookie( $isAnon ) {
685  wfDeprecated( __METHOD__, '1.34' );
686  return false;
687  }
688 
695  public function appliesToPasswordReset() {
696  return $this->isCreateAccountBlocked();
697  }
698 
699 }
MediaWiki\Block\AbstractBlock\appliesToNamespace
appliesToNamespace( $ns)
Checks if a block applies to a particular namespace.
Definition: AbstractBlock.php:654
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:66
MediaWiki\Block\AbstractBlock\$mBlockEmail
bool $mBlockEmail
Definition: AbstractBlock.php:60
MediaWiki\Block\AbstractBlock\$target
User string null $target
Definition: AbstractBlock.php:75
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:149
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:1541
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1806
MediaWiki\Block\AbstractBlock\setBlocker
setBlocker( $user)
Set the user who implemented (or will implement) this block.
Definition: AbstractBlock.php:525
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:227
MediaWiki\Block\AbstractBlock\TYPE_AUTO
const TYPE_AUTO
Definition: AbstractBlock.php:90
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:534
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:590
MediaWiki\Block\AbstractBlock\getId
getId()
Get the block ID.
Definition: AbstractBlock.php:137
$res
$res
Definition: testCompression.php:57
MediaWiki\Block\AbstractBlock\getTargetAndType
getTargetAndType()
Get the target and target type for this particular block.
Definition: AbstractBlock.php:448
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:596
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:180
MediaWiki\Block\AbstractBlock\TYPE_RANGE
const TYPE_RANGE
Definition: AbstractBlock.php:89
MediaWiki\Block\AbstractBlock\appliesToPasswordReset
appliesToPasswordReset()
Check if the block prevents a user from resetting their password.
Definition: AbstractBlock.php:695
MediaWiki\Block\AbstractBlock\$mTimestamp
string $mTimestamp
Definition: AbstractBlock.php:51
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1030
MediaWiki\Block\AbstractBlock\TYPE_ID
const TYPE_ID
Definition: AbstractBlock.php:91
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:240
MediaWiki\Block\AbstractBlock\getReasonComment
getReasonComment()
Get the reason for creating the block.
Definition: AbstractBlock.php:171
MediaWiki\Block\AbstractBlock\TYPE_IP
const TYPE_IP
Definition: AbstractBlock.php:88
$title
$title
Definition: testCompression.php:38
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:34
MediaWiki\Block\AbstractBlock\getType
getType()
Get the type of target for this particular block.
Definition: AbstractBlock.php:434
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:214
MediaWiki\Block\AbstractBlock\shouldTrackWithCookie
shouldTrackWithCookie( $isAnon)
Check if the block should be tracked with a cookie.
Definition: AbstractBlock.php:684
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:84
MediaWiki\Block\AbstractBlock\setTimestamp
setTimestamp( $timestamp)
Set the timestamp indicating when the block was created.
Definition: AbstractBlock.php:498
MediaWiki\Block\AbstractBlock\parseTarget
static parseTarget( $target)
From an existing block, get the target and the type of target.
Definition: AbstractBlock.php:379
MediaWiki\Block\AbstractBlock\TYPE_USER
const TYPE_USER
Definition: AbstractBlock.php:87
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:515
MediaWiki\Block\AbstractBlock\__construct
__construct(array $options=[])
Create a new block with specified parameters on a user, IP or IP range.
Definition: AbstractBlock.php:102
MediaWiki\Block\AbstractBlock\$blockCreateAccount
bool $blockCreateAccount
Definition: AbstractBlock.php:66
MediaWiki\Block\AbstractBlock\getPermissionsError
getPermissionsError(IContextSource $context)
Get the key and parameters for the corresponding error message.
Definition: AbstractBlock.php:538
MediaWiki\Block\AbstractBlock\appliesToTitle
appliesToTitle(Title $title)
Checks if a block applies to a particular title.
Definition: AbstractBlock.php:642
MediaWiki\Block\AbstractBlock\$mReason
$mReason
Definition: AbstractBlock.php:42
MediaWiki\Block\AbstractBlock\getBlockErrorParams
getBlockErrorParams(IContextSource $context)
Get block information used in different block error messages.
Definition: AbstractBlock.php:558
MediaWiki\Block\AbstractBlock\setExpiry
setExpiry( $expiry)
Set the block expiry time.
Definition: AbstractBlock.php:478
MediaWiki\Block\AbstractBlock\$mExpiry
string $mExpiry
Definition: AbstractBlock.php:57
IContextSource\getUser
getUser()
MediaWiki\Block\AbstractBlock\getExpiry
getExpiry()
Get the block expiry time.
Definition: AbstractBlock.php:468
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
MediaWiki\Block\AbstractBlock\prevents
prevents( $action, $x=null)
Get/set whether the block prevents a given action.
Definition: AbstractBlock.php:315
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
MediaWiki\Block\AbstractBlock\getTimestamp
getTimestamp()
Get the timestamp indicating when the block was created.
Definition: AbstractBlock.php:488
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:268
MediaWiki\Block\AbstractBlock\setReason
setReason( $reason)
Set the reason for creating the block.
Definition: AbstractBlock.php:181
MediaWiki\Block\AbstractBlock\appliesToPage
appliesToPage( $pageId)
Checks if a block applies to a particular page.
Definition: AbstractBlock.php:671
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:160
MediaWiki\Block\AbstractBlock\$reason
CommentStoreComment $reason
Definition: AbstractBlock.php:45
IContextSource\getRequest
getRequest()
MediaWiki\Block\AbstractBlock\getHideName
getHideName()
Get whether the block hides the target's username.
Definition: AbstractBlock.php:191
MediaWiki\Block\AbstractBlock\$type
int null $type
AbstractBlock::TYPE_ constant.
Definition: AbstractBlock.php:81
MediaWiki\Block\AbstractBlock\$mHideName
bool $mHideName
Definition: AbstractBlock.php:72
MediaWiki\Block\AbstractBlock
Definition: AbstractBlock.php:37
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:506
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:201
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:458
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2108
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:253
CommentStoreComment
CommentStoreComment represents a comment stored by CommentStore.
Definition: CommentStoreComment.php:29
IContextSource\getLanguage
getLanguage()
MediaWiki\Block\AbstractBlock\$allowUsertalk
bool $allowUsertalk
Definition: AbstractBlock.php:63
MediaWiki\Block\AbstractBlock\getReason
getReason()
Get the reason given for creating the block, as a string.
Definition: AbstractBlock.php:160