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 
37 abstract class AbstractBlock implements Block {
39  protected $reason;
40 
45  public $mTimestamp = '';
46 
51  public $mExpiry = '';
52 
54  protected $mBlockEmail = false;
55 
57  protected $allowUsertalk = false;
58 
60  protected $blockCreateAccount = false;
61 
66  public $mHideName = false;
67 
69  protected $isHardblock;
70 
72  protected $target;
73 
78  protected $type;
79 
81  protected $isSitewide = true;
82 
92  public function __construct( array $options = [] ) {
93  $defaults = [
94  'address' => '',
95  'reason' => '',
96  'timestamp' => '',
97  'hideName' => false,
98  'anonOnly' => false,
99  ];
100 
101  $options += $defaults;
102 
103  $this->setTarget( $options['address'] );
104 
105  $this->setReason( $options['reason'] );
106  $this->setTimestamp( wfTimestamp( TS_MW, $options['timestamp'] ) );
107  $this->setHideName( (bool)$options['hideName'] );
108  $this->isHardblock( !$options['anonOnly'] );
109  }
110 
116  abstract public function getBy();
117 
123  abstract public function getByName();
124 
129  public function getId(): ?int {
130  return null;
131  }
132 
143  public function getReason() {
144  $language = RequestContext::getMain()->getLanguage();
145  return $this->reason->message->inLanguage( $language )->plain();
146  }
147 
155  return $this->reason;
156  }
157 
164  public function setReason( $reason ) {
166  }
167 
174  public function getHideName() {
175  return $this->mHideName;
176  }
177 
184  public function setHideName( $hideName ) {
185  $this->mHideName = $hideName;
186  }
187 
197  public function isSitewide( $x = null ): bool {
198  return wfSetVar( $this->isSitewide, $x );
199  }
200 
210  public function isCreateAccountBlocked( $x = null ): bool {
211  return wfSetVar( $this->blockCreateAccount, $x );
212  }
213 
223  public function isEmailBlocked( $x = null ) {
224  return wfSetVar( $this->mBlockEmail, $x );
225  }
226 
236  public function isUsertalkEditAllowed( $x = null ) {
237  return wfSetVar( $this->allowUsertalk, $x );
238  }
239 
249  public function isHardblock( $x = null ): bool {
250  wfSetVar( $this->isHardblock, $x );
251 
252  return $this->getType() == self::TYPE_USER
253  ? true
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 'createaccount':
275  $res = $this->isCreateAccountBlocked();
276  break;
277  case 'sendemail':
278  $res = $this->isEmailBlocked();
279  break;
280  case 'upload':
281  // Sitewide blocks always block upload. This may be overridden in a subclass.
282  $res = $this->isSitewide();
283  break;
284  case 'read':
285  $res = false;
286  break;
287  case 'purge':
288  $res = false;
289  break;
290  }
291  if ( !$res && $blockDisablesLogin ) {
292  // If a block would disable login, then it should
293  // prevent any right that all users cannot do
294  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
295  $anon = new User;
296  $res = $permissionManager->userHasRight( $anon, $right ) ? $res : true;
297  }
298 
299  return $res;
300  }
301 
306  public function getType(): ?int {
307  return $this->type;
308  }
309 
323  public function getTargetAndType() {
324  wfDeprecated( __METHOD__, '1.37' );
325  return [ $this->getTarget(), $this->getType() ];
326  }
327 
336  public function getTarget() {
337  wfDeprecated( __METHOD__, '1.37' );
338  if ( $this->target instanceof UserIdentity ) {
340  ->getUserFactory()
341  ->newFromUserIdentity( $this->target );
342  } else {
343  return $this->target;
344  }
345  }
346 
351  public function getTargetUserIdentity(): ?UserIdentity {
352  return $this->target instanceof UserIdentity ? $this->target : null;
353  }
354 
359  public function getTargetName(): string {
360  return $this->target instanceof UserIdentity
361  ? $this->target->getName()
362  : (string)$this->target;
363  }
364 
371  public function isBlocking( $target ): bool {
372  $targetName = $target instanceof UserIdentity
373  ? $target->getName()
374  : (string)$target;
375 
376  return $targetName === $this->getTargetName();
377  }
378 
385  public function getExpiry(): string {
386  return $this->mExpiry;
387  }
388 
395  public function setExpiry( $expiry ) {
396  // Force string so getExpiry() return typehint doesn't break things
397  $this->mExpiry = (string)$expiry;
398  }
399 
406  public function getTimestamp(): string {
407  return $this->mTimestamp;
408  }
409 
416  public function setTimestamp( $timestamp ) {
417  // Force string so getExpiry() return typehint doesn't break things
418  $this->mTimestamp = (string)$timestamp;
419  }
420 
425  public function setTarget( $target ) {
426  // Small optimization to make this code testable, this is what would happen anyway
427  if ( $target === '' ) {
428  $this->target = null;
429  $this->type = null;
430  } else {
431  list( $this->target, $this->type ) = MediaWikiServices::getInstance()
432  ->getBlockUtils()
433  ->parseBlockTarget( $target );
434  }
435  }
436 
447  $message = MediaWikiServices::getInstance()
448  ->getBlockErrorFormatter()->getMessage(
449  $this,
450  $context->getUser(),
452  $context->getRequest()->getIP()
453  );
454  return array_merge( [ [ $message->getKey() ], $message->getParams() ] );
455  }
456 
484  public function appliesToUsertalk( Title $usertalk = null ) {
485  if ( !$usertalk ) {
486  if ( $this->target instanceof UserIdentity ) {
487  $usertalk = Title::makeTitle(
488  NS_USER_TALK,
489  $this->target->getName()
490  );
491  } else {
492  throw new InvalidArgumentException(
493  '$usertalk must be provided if block target is not a user/IP'
494  );
495  }
496  }
497 
498  if ( $usertalk->getNamespace() !== NS_USER_TALK ) {
499  throw new InvalidArgumentException(
500  '$usertalk must be a user talk page'
501  );
502  }
503 
504  if ( !$this->isSitewide() ) {
505  if ( $this->appliesToPage( $usertalk->getArticleID() ) ) {
506  return true;
507  }
508  if ( !$this->appliesToNamespace( NS_USER_TALK ) ) {
509  return false;
510  }
511  }
512 
513  // This is a type of block which uses the ipb_allow_usertalk
514  // flag. The flag can still be overridden by global configs.
515  $config = RequestContext::getMain()->getConfig();
516  if ( !$config->get( 'BlockAllowsUTEdit' ) ) {
517  return true;
518  }
519  return !$this->isUsertalkEditAllowed();
520  }
521 
533  public function appliesToTitle( Title $title ) {
534  return $this->isSitewide();
535  }
536 
545  public function appliesToNamespace( $ns ) {
546  return $this->isSitewide();
547  }
548 
562  public function appliesToPage( $pageId ) {
563  return $this->isSitewide();
564  }
565 
572  public function appliesToPasswordReset() {
573  return $this->isCreateAccountBlocked();
574  }
575 
576 }
MediaWiki\Block\AbstractBlock\appliesToNamespace
appliesToNamespace( $ns)
Checks if a block applies to a particular namespace.
Definition: AbstractBlock.php:545
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:54
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
MediaWiki\Block\AbstractBlock\getTargetName
getTargetName()
Definition: AbstractBlock.php:359
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:1515
true
return true
Definition: router.php:90
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1668
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:210
MediaWiki\Block\AbstractBlock\getId
getId()
Get the block ID.
Definition: AbstractBlock.php:129
$res
$res
Definition: testCompression.php:57
MediaWiki\Block\AbstractBlock\getTargetAndType
getTargetAndType()
Get the target and target type for this particular block.
Definition: AbstractBlock.php:323
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:484
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:254
MediaWiki\Block\AbstractBlock\appliesToPasswordReset
appliesToPasswordReset()
Check if the block prevents a user from resetting their password.
Definition: AbstractBlock.php:572
MediaWiki\Block\AbstractBlock\$mTimestamp
string $mTimestamp
Definition: AbstractBlock.php:45
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
MediaWiki\Block\AbstractBlock\isHardblock
isHardblock( $x=null)
Get/set whether the block is a hardblock (affects logged-in users on a given IP/range)
Definition: AbstractBlock.php:249
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
MediaWiki\Block\Block\isSitewide
isSitewide()
Indicates that the block is a sitewide block.
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:223
MediaWiki\Block\AbstractBlock\getReasonComment
getReasonComment()
Get the reason for creating the block.
Definition: AbstractBlock.php:154
MediaWiki\User\UserIdentity\getName
getName()
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:651
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:41
MediaWiki\Block\AbstractBlock\getType
getType()
Get the type of target for this particular block.
Definition: AbstractBlock.php:306
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:197
MediaWiki\Block\AbstractBlock\$isHardblock
bool $isHardblock
Definition: AbstractBlock.php:69
MediaWiki\Block\AbstractBlock\$isSitewide
bool $isSitewide
Definition: AbstractBlock.php:81
MediaWiki\Block\AbstractBlock\setTimestamp
setTimestamp( $timestamp)
Set the timestamp indicating when the block was created.
Definition: AbstractBlock.php:416
MediaWiki\Block\Block
Represents a block that may prevent users from performing specific operations.
Definition: Block.php:37
MediaWiki\Block\Block\isHardblock
isHardblock()
Returns whether the block is a hardblock (affects logged-in users on a given IP/range)
MediaWiki\Block\AbstractBlock\__construct
__construct(array $options=[])
Create a new block with specified parameters on a user, IP or IP range.
Definition: AbstractBlock.php:92
MediaWiki\Block\AbstractBlock\$blockCreateAccount
bool $blockCreateAccount
Definition: AbstractBlock.php:60
MediaWiki\Block\AbstractBlock\getPermissionsError
getPermissionsError(IContextSource $context)
Get the key and parameters for the corresponding error message.
Definition: AbstractBlock.php:446
MediaWiki\Block\AbstractBlock\appliesToTitle
appliesToTitle(Title $title)
Checks if a block applies to a particular title.
Definition: AbstractBlock.php:533
MediaWiki\Block\AbstractBlock\$target
UserIdentity string null $target
Definition: AbstractBlock.php:72
MediaWiki\Block\AbstractBlock\setExpiry
setExpiry( $expiry)
Set the block expiry time.
Definition: AbstractBlock.php:395
MediaWiki\Block\AbstractBlock\$mExpiry
string $mExpiry
Definition: AbstractBlock.php:51
IContextSource\getUser
getUser()
MediaWiki\Block\AbstractBlock\getExpiry
getExpiry()
Get the block expiry time.
Definition: AbstractBlock.php:385
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
MediaWiki\Block\AbstractBlock\getTimestamp
getTimestamp()
Get the timestamp indicating when the block was created.
Definition: AbstractBlock.php:406
Title
Represents a title within MediaWiki.
Definition: Title.php:48
MediaWiki\Block\Block\isCreateAccountBlocked
isCreateAccountBlocked()
Get the flag indicating whether this block blocks the target from creating an account.
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:164
MediaWiki\Block\AbstractBlock\isBlocking
isBlocking( $target)
Definition: AbstractBlock.php:371
MediaWiki\Block\AbstractBlock\appliesToPage
appliesToPage( $pageId)
Checks if a block applies to a particular page.
Definition: AbstractBlock.php:562
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:138
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
MediaWiki\Block\AbstractBlock\$reason
CommentStoreComment $reason
Definition: AbstractBlock.php:39
IContextSource\getRequest
getRequest()
MediaWiki\Block\AbstractBlock\getTargetUserIdentity
getTargetUserIdentity()
Definition: AbstractBlock.php:351
MediaWiki\Block\AbstractBlock\getHideName
getHideName()
Get whether the block hides the target's username.
Definition: AbstractBlock.php:174
MediaWiki\Block\AbstractBlock\$type
int null $type
AbstractBlock::TYPE_ constant.
Definition: AbstractBlock.php:78
MediaWiki\Block\AbstractBlock\$mHideName
bool $mHideName
Definition: AbstractBlock.php:66
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:425
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:184
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
MediaWiki\Block\AbstractBlock\getTarget
getTarget()
Get the target for this particular block.
Definition: AbstractBlock.php:336
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:236
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:57
MediaWiki\Block\AbstractBlock\getReason
getReason()
Get the reason given for creating the block, as a string.
Definition: AbstractBlock.php:143