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 {
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 $isHardblock;
76 
78  protected $target;
79 
84  protected $type;
85 
87  protected $isSitewide = true;
88 
89  # TYPE constants
90  # Do not introduce negative constants without changing BlockUser command object.
91  public const TYPE_USER = 1;
92  public const TYPE_IP = 2;
93  public const TYPE_RANGE = 3;
94  public const TYPE_AUTO = 4;
95  public const TYPE_ID = 5;
96 
106  public function __construct( array $options = [] ) {
107  $defaults = [
108  'address' => '',
109  'reason' => '',
110  'timestamp' => '',
111  'hideName' => false,
112  'anonOnly' => false,
113  ];
114 
115  $options += $defaults;
116 
117  $this->setTarget( $options['address'] );
118 
119  $this->setReason( $options['reason'] );
120  $this->setTimestamp( wfTimestamp( TS_MW, $options['timestamp'] ) );
121  $this->setHideName( (bool)$options['hideName'] );
122  $this->isHardblock( !$options['anonOnly'] );
123  }
124 
130  abstract public function getBy();
131 
137  abstract public function getByName();
138 
143  public function getId() {
144  return null;
145  }
146 
154  abstract public function getIdentifier();
155 
166  public function getReason() {
167  $language = RequestContext::getMain()->getLanguage();
168  return $this->reason->message->inLanguage( $language )->plain();
169  }
170 
177  public function getReasonComment() {
178  return $this->reason;
179  }
180 
187  public function setReason( $reason ) {
189  }
190 
197  public function getHideName() {
198  return $this->mHideName;
199  }
200 
207  public function setHideName( $hideName ) {
208  $this->mHideName = $hideName;
209  }
210 
220  public function isSitewide( $x = null ) {
221  return wfSetVar( $this->isSitewide, $x );
222  }
223 
233  public function isCreateAccountBlocked( $x = null ) {
234  return wfSetVar( $this->blockCreateAccount, $x );
235  }
236 
246  public function isEmailBlocked( $x = null ) {
247  return wfSetVar( $this->mBlockEmail, $x );
248  }
249 
259  public function isUsertalkEditAllowed( $x = null ) {
260  return wfSetVar( $this->allowUsertalk, $x );
261  }
262 
272  public function isHardblock( $x = null ) {
273  wfSetVar( $this->isHardblock, $x );
274 
275  return $this->getType() == self::TYPE_USER
276  ? true
278  }
279 
291  public function appliesToRight( $right ) {
292  $config = RequestContext::getMain()->getConfig();
293  $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
294 
295  $res = null;
296  switch ( $right ) {
297  case 'edit':
298  // TODO: fix this case to return proper value
299  $res = true;
300  break;
301  case 'createaccount':
302  $res = $this->isCreateAccountBlocked();
303  break;
304  case 'sendemail':
305  $res = $this->isEmailBlocked();
306  break;
307  case 'upload':
308  // Until T6995 is completed
309  $res = $this->isSitewide();
310  break;
311  case 'read':
312  $res = false;
313  break;
314  case 'purge':
315  $res = false;
316  break;
317  }
318  if ( !$res && $blockDisablesLogin ) {
319  // If a block would disable login, then it should
320  // prevent any right that all users cannot do
321  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
322  $anon = new User;
323  $res = $permissionManager->userHasRight( $anon, $right ) ? $res : true;
324  }
325 
326  return $res;
327  }
328 
340  public static function parseTarget( $target ) {
342  ->getBlockUtils()
343  ->parseBlockTarget( $target );
344  }
345 
350  public function getType() {
351  return $this->type;
352  }
353 
364  public function getTargetAndType() {
365  return [ $this->getTarget(), $this->getType() ];
366  }
367 
374  public function getTarget() {
375  return $this->target;
376  }
377 
384  public function getExpiry() {
385  return $this->mExpiry;
386  }
387 
394  public function setExpiry( $expiry ) {
395  $this->mExpiry = $expiry;
396  }
397 
404  public function getTimestamp() {
405  return $this->mTimestamp;
406  }
407 
414  public function setTimestamp( $timestamp ) {
415  $this->mTimestamp = $timestamp;
416  }
417 
422  public function setTarget( $target ) {
423  list( $this->target, $this->type ) = static::parseTarget( $target );
424  }
425 
436  $message = MediaWikiServices::getInstance()
437  ->getBlockErrorFormatter()->getMessage(
438  $this,
439  $context->getUser(),
441  $context->getRequest()->getIP()
442  );
443  return array_merge( [ [ $message->getKey() ], $message->getParams() ] );
444  }
445 
473  public function appliesToUsertalk( Title $usertalk = null ) {
474  if ( !$usertalk ) {
475  if ( $this->target instanceof User ) {
476  $usertalk = $this->target->getTalkPage();
477  } else {
478  throw new InvalidArgumentException(
479  '$usertalk must be provided if block target is not a user/IP'
480  );
481  }
482  }
483 
484  if ( $usertalk->getNamespace() !== NS_USER_TALK ) {
485  throw new InvalidArgumentException(
486  '$usertalk must be a user talk page'
487  );
488  }
489 
490  if ( !$this->isSitewide() ) {
491  if ( $this->appliesToPage( $usertalk->getArticleID() ) ) {
492  return true;
493  }
494  if ( !$this->appliesToNamespace( NS_USER_TALK ) ) {
495  return false;
496  }
497  }
498 
499  // This is a type of block which uses the ipb_allow_usertalk
500  // flag. The flag can still be overridden by global configs.
501  $config = RequestContext::getMain()->getConfig();
502  if ( !$config->get( 'BlockAllowsUTEdit' ) ) {
503  return true;
504  }
505  return !$this->isUsertalkEditAllowed();
506  }
507 
519  public function appliesToTitle( Title $title ) {
520  return $this->isSitewide();
521  }
522 
531  public function appliesToNamespace( $ns ) {
532  return $this->isSitewide();
533  }
534 
548  public function appliesToPage( $pageId ) {
549  return $this->isSitewide();
550  }
551 
558  public function appliesToPasswordReset() {
559  return $this->isCreateAccountBlocked();
560  }
561 
562 }
MediaWiki\Block\AbstractBlock\appliesToNamespace
appliesToNamespace( $ns)
Checks if a block applies to a particular namespace.
Definition: AbstractBlock.php:531
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:60
MediaWiki\Block\AbstractBlock\$target
User string null $target
Definition: AbstractBlock.php:78
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
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:1560
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:1831
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:233
MediaWiki\Block\AbstractBlock\TYPE_AUTO
const TYPE_AUTO
Definition: AbstractBlock.php:94
MediaWiki\Block\AbstractBlock\getId
getId()
Get the block ID.
Definition: AbstractBlock.php:143
$res
$res
Definition: testCompression.php:57
MediaWiki\Block\AbstractBlock\getTargetAndType
getTargetAndType()
Get the target and target type for this particular block.
Definition: AbstractBlock.php:364
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:473
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:227
MediaWiki\Block\AbstractBlock\TYPE_RANGE
const TYPE_RANGE
Definition: AbstractBlock.php:93
MediaWiki\Block\AbstractBlock\appliesToPasswordReset
appliesToPasswordReset()
Check if the block prevents a user from resetting their password.
Definition: AbstractBlock.php:558
MediaWiki\Block\AbstractBlock\$mTimestamp
string $mTimestamp
Definition: AbstractBlock.php:51
MediaWiki\Block\AbstractBlock\TYPE_ID
const TYPE_ID
Definition: AbstractBlock.php:95
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:272
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:246
MediaWiki\Block\AbstractBlock\getReasonComment
getReasonComment()
Get the reason for creating the block.
Definition: AbstractBlock.php:177
MediaWiki\Block\AbstractBlock\TYPE_IP
const TYPE_IP
Definition: AbstractBlock.php:92
$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:40
MediaWiki\Block\AbstractBlock\getType
getType()
Get the type of target for this particular block.
Definition: AbstractBlock.php:350
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:220
MediaWiki\Block\AbstractBlock\$isHardblock
bool $isHardblock
Definition: AbstractBlock.php:75
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:87
MediaWiki\Block\AbstractBlock\setTimestamp
setTimestamp( $timestamp)
Set the timestamp indicating when the block was created.
Definition: AbstractBlock.php:414
MediaWiki\Block\AbstractBlock\parseTarget
static parseTarget( $target)
From an existing block, get the target and the type of target.
Definition: AbstractBlock.php:340
MediaWiki\Block\AbstractBlock\TYPE_USER
const TYPE_USER
Definition: AbstractBlock.php:91
MediaWiki\Block\AbstractBlock\__construct
__construct(array $options=[])
Create a new block with specified parameters on a user, IP or IP range.
Definition: AbstractBlock.php:106
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:435
MediaWiki\Block\AbstractBlock\appliesToTitle
appliesToTitle(Title $title)
Checks if a block applies to a particular title.
Definition: AbstractBlock.php:519
MediaWiki\Block\AbstractBlock\$mReason
$mReason
Definition: AbstractBlock.php:42
MediaWiki\Block\AbstractBlock\setExpiry
setExpiry( $expiry)
Set the block expiry time.
Definition: AbstractBlock.php:394
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:384
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:476
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:57
MediaWiki\Block\AbstractBlock\getTimestamp
getTimestamp()
Get the timestamp indicating when the block was created.
Definition: AbstractBlock.php:404
Title
Represents a title within MediaWiki.
Definition: Title.php:46
MediaWiki\Block\AbstractBlock\appliesToRight
appliesToRight( $right)
Determine whether the block prevents a given right.
Definition: AbstractBlock.php:291
MediaWiki\Block\AbstractBlock\setReason
setReason( $reason)
Set the reason for creating the block.
Definition: AbstractBlock.php:187
MediaWiki\Block\AbstractBlock\appliesToPage
appliesToPage( $pageId)
Checks if a block applies to a particular page.
Definition: AbstractBlock.php:548
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
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:66
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:197
MediaWiki\Block\AbstractBlock\$type
int null $type
AbstractBlock::TYPE_ constant.
Definition: AbstractBlock.php:84
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:422
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:207
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:63
MediaWiki\Block\AbstractBlock\getTarget
getTarget()
Get the target for this particular block.
Definition: AbstractBlock.php:374
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:259
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:63
MediaWiki\Block\AbstractBlock\getReason
getReason()
Get the reason given for creating the block, as a string.
Definition: AbstractBlock.php:166