MediaWiki  master
AbstractBlock.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Block;
22 
24 use IContextSource;
25 use InvalidArgumentException;
29 use Message;
30 use RequestContext;
31 use Title;
32 use User;
33 
38 abstract class AbstractBlock implements Block {
40 
42  protected $reason;
43 
48  public $mTimestamp = '';
49 
54  public $mExpiry = '';
55 
57  protected $mBlockEmail = false;
58 
60  protected $allowUsertalk = false;
61 
63  protected $blockCreateAccount = false;
64 
69  public $mHideName = false;
70 
72  protected $isHardblock;
73 
75  protected $target;
76 
81  protected $type;
82 
84  protected $isSitewide = true;
85 
87  protected $wikiId;
88 
99  public function __construct( array $options = [] ) {
100  $defaults = [
101  'address' => '',
102  'wiki' => self::LOCAL,
103  'reason' => '',
104  'timestamp' => '',
105  'hideName' => false,
106  'anonOnly' => false,
107  ];
108 
109  $options += $defaults;
110 
111  $this->wikiId = $options['wiki'];
112  $this->setTarget( $options['address'] );
113  $this->setReason( $options['reason'] );
114  $this->setTimestamp( wfTimestamp( TS_MW, $options['timestamp'] ) );
115  $this->setHideName( (bool)$options['hideName'] );
116  $this->isHardblock( !$options['anonOnly'] );
117  }
118 
125  abstract public function getBy( $wikiId = self::LOCAL ): int;
126 
132  abstract public function getByName();
133 
137  public function getId( $wikiId = self::LOCAL ): ?int {
138  // TODO: Enable deprecation warnings once cross-wiki accesses have been removed, see T274817
139  // $this->deprecateInvalidCrossWiki( $wikiId, '1.38' );
140  return null;
141  }
142 
153  public function getReason() {
154  $language = RequestContext::getMain()->getLanguage();
155  return $this->reason->message->inLanguage( $language )->plain();
156  }
157 
165  return $this->reason;
166  }
167 
174  public function setReason( $reason ) {
176  }
177 
184  public function getHideName() {
185  return $this->mHideName;
186  }
187 
194  public function setHideName( $hideName ) {
195  $this->mHideName = $hideName;
196  }
197 
207  public function isSitewide( $x = null ): bool {
208  return wfSetVar( $this->isSitewide, $x );
209  }
210 
220  public function isCreateAccountBlocked( $x = null ): bool {
221  return wfSetVar( $this->blockCreateAccount, $x );
222  }
223 
233  public function isEmailBlocked( $x = null ) {
234  return wfSetVar( $this->mBlockEmail, $x );
235  }
236 
246  public function isUsertalkEditAllowed( $x = null ) {
247  return wfSetVar( $this->allowUsertalk, $x );
248  }
249 
259  public function isHardblock( $x = null ): bool {
260  wfSetVar( $this->isHardblock, $x );
261 
262  return $this->getType() == self::TYPE_USER
263  ? true
265  }
266 
278  public function appliesToRight( $right ) {
279  $config = RequestContext::getMain()->getConfig();
280  $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
281 
282  $res = null;
283  switch ( $right ) {
284  case 'createaccount':
285  $res = $this->isCreateAccountBlocked();
286  break;
287  case 'sendemail':
288  $res = $this->isEmailBlocked();
289  break;
290  case 'upload':
291  // Sitewide blocks always block upload. This may be overridden in a subclass.
292  $res = $this->isSitewide();
293  break;
294  case 'read':
295  $res = false;
296  break;
297  case 'purge':
298  $res = false;
299  break;
300  }
301  if ( !$res && $blockDisablesLogin ) {
302  // If a block would disable login, then it should
303  // prevent any right that all users cannot do
304  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
305  $anon = new User;
306  $res = $permissionManager->userHasRight( $anon, $right ) ? $res : true;
307  }
308 
309  return $res;
310  }
311 
316  public function getType(): ?int {
317  return $this->type;
318  }
319 
324  public function getTargetUserIdentity(): ?UserIdentity {
325  return $this->target instanceof UserIdentity ? $this->target : null;
326  }
327 
332  public function getTargetName(): string {
333  return $this->target instanceof UserIdentity
334  ? $this->target->getName()
335  : (string)$this->target;
336  }
337 
344  public function isBlocking( $target ): bool {
345  $targetName = $target instanceof UserIdentity
346  ? $target->getName()
347  : (string)$target;
348 
349  return $targetName === $this->getTargetName();
350  }
351 
358  public function getExpiry(): string {
359  return $this->mExpiry;
360  }
361 
368  public function setExpiry( $expiry ) {
369  // Force string so getExpiry() return typehint doesn't break things
370  $this->mExpiry = (string)$expiry;
371  }
372 
379  public function getTimestamp(): string {
380  return $this->mTimestamp;
381  }
382 
389  public function setTimestamp( $timestamp ) {
390  // Force string so getExpiry() return typehint doesn't break things
391  $this->mTimestamp = (string)$timestamp;
392  }
393 
398  public function setTarget( $target ) {
399  // Small optimization to make this code testable, this is what would happen anyway
400  if ( $target === '' ) {
401  $this->target = null;
402  $this->type = null;
403  } else {
404  list( $parsedTarget, $this->type ) = MediaWikiServices::getInstance()
405  ->getBlockUtils()
406  ->parseBlockTarget( $target );
407  if ( $parsedTarget !== null ) {
408  $this->assertWiki( is_string( $parsedTarget ) ? self::LOCAL : $parsedTarget->getWikiId() );
409  }
410  $this->target = $parsedTarget;
411  }
412  }
413 
418  public function getWikiId() {
419  return $this->wikiId;
420  }
421 
432  $message = MediaWikiServices::getInstance()
433  ->getBlockErrorFormatter()->getMessage(
434  $this,
435  $context->getUser(),
437  $context->getRequest()->getIP()
438  );
439  return array_merge( [ [ $message->getKey() ], $message->getParams() ] );
440  }
441 
469  public function appliesToUsertalk( Title $usertalk = null ) {
470  if ( !$usertalk ) {
471  if ( $this->target instanceof UserIdentity ) {
472  $usertalk = Title::makeTitle(
473  NS_USER_TALK,
474  $this->target->getName()
475  );
476  } else {
477  throw new InvalidArgumentException(
478  '$usertalk must be provided if block target is not a user/IP'
479  );
480  }
481  }
482 
483  if ( $usertalk->getNamespace() !== NS_USER_TALK ) {
484  throw new InvalidArgumentException(
485  '$usertalk must be a user talk page'
486  );
487  }
488 
489  if ( !$this->isSitewide() ) {
490  if ( $this->appliesToPage( $usertalk->getArticleID() ) ) {
491  return true;
492  }
493  if ( !$this->appliesToNamespace( NS_USER_TALK ) ) {
494  return false;
495  }
496  }
497 
498  // This is a type of block which uses the ipb_allow_usertalk
499  // flag. The flag can still be overridden by global configs.
500  $config = RequestContext::getMain()->getConfig();
501  if ( !$config->get( 'BlockAllowsUTEdit' ) ) {
502  return true;
503  }
504  return !$this->isUsertalkEditAllowed();
505  }
506 
518  public function appliesToTitle( Title $title ) {
519  return $this->isSitewide();
520  }
521 
530  public function appliesToNamespace( $ns ) {
531  return $this->isSitewide();
532  }
533 
547  public function appliesToPage( $pageId ) {
548  return $this->isSitewide();
549  }
550 
557  public function appliesToPasswordReset() {
558  return $this->isCreateAccountBlocked();
559  }
560 
561 }
MediaWiki\Block\AbstractBlock\appliesToNamespace
appliesToNamespace( $ns)
Checks if a block applies to a particular namespace.
Definition: AbstractBlock.php:530
MediaWiki\DAO\WikiAwareEntityTrait
trait WikiAwareEntityTrait
Definition: WikiAwareEntityTrait.php:32
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:57
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
MediaWiki\Block\AbstractBlock\getTargetName
getTargetName()
Definition: AbstractBlock.php:332
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:1496
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:1649
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:220
MediaWiki\Block\AbstractBlock\$wikiId
string false $wikiId
Definition: AbstractBlock.php:87
MediaWiki\Block\AbstractBlock\getWikiId
getWikiId()
Definition: AbstractBlock.php:418
$res
$res
Definition: testCompression.php:57
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:469
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:264
MediaWiki\Block\AbstractBlock\appliesToPasswordReset
appliesToPasswordReset()
Check if the block prevents a user from resetting their password.
Definition: AbstractBlock.php:557
MediaWiki\Block\AbstractBlock\$mTimestamp
string $mTimestamp
Definition: AbstractBlock.php:48
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:259
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:233
MediaWiki\Block\AbstractBlock\getReasonComment
getReasonComment()
Get the reason for creating the block.
Definition: AbstractBlock.php:164
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:648
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:41
MediaWiki\Block\AbstractBlock\getType
getType()
Get the type of target for this particular block.
Definition: AbstractBlock.php:316
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:207
MediaWiki\Block\AbstractBlock\$isHardblock
bool $isHardblock
Definition: AbstractBlock.php:72
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:389
MediaWiki\Block\Block
Represents a block that may prevent users from performing specific operations.
Definition: Block.php:40
MediaWiki\Block\Block\isHardblock
isHardblock()
Returns whether the block is a hardblock (affects logged-in users on a given IP/range)
MediaWiki\DAO\WikiAwareEntity\assertWiki
assertWiki( $wikiId)
Throws if $wikiId is different from the return value of getWikiId().
MediaWiki\Block\AbstractBlock\__construct
__construct(array $options=[])
Create a new block with specified parameters on a user, IP or IP range.
Definition: AbstractBlock.php:99
MediaWiki\Block\AbstractBlock\$blockCreateAccount
bool $blockCreateAccount
Definition: AbstractBlock.php:63
MediaWiki\Block\AbstractBlock\getBy
getBy( $wikiId=self::LOCAL)
Get the user id of the blocking sysop.
MediaWiki\Block\AbstractBlock\getPermissionsError
getPermissionsError(IContextSource $context)
Get the key and parameters for the corresponding error message.
Definition: AbstractBlock.php:431
MediaWiki\Block\AbstractBlock\appliesToTitle
appliesToTitle(Title $title)
Checks if a block applies to a particular title.
Definition: AbstractBlock.php:518
MediaWiki\Block\AbstractBlock\$target
UserIdentity string null $target
Definition: AbstractBlock.php:75
MediaWiki\Block\AbstractBlock\setExpiry
setExpiry( $expiry)
Set the block expiry time.
Definition: AbstractBlock.php:368
MediaWiki\Block\AbstractBlock\$mExpiry
string $mExpiry
Definition: AbstractBlock.php:54
IContextSource\getUser
getUser()
MediaWiki\Block\AbstractBlock\getExpiry
getExpiry()
Get the block expiry time.
Definition: AbstractBlock.php:358
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:379
Title
Represents a title within MediaWiki.
Definition: Title.php:47
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:278
MediaWiki\Block\AbstractBlock\setReason
setReason( $reason)
Set the reason for creating the block.
Definition: AbstractBlock.php:174
MediaWiki\Block\AbstractBlock\isBlocking
isBlocking( $target)
Definition: AbstractBlock.php:344
MediaWiki\Block\AbstractBlock\appliesToPage
appliesToPage( $pageId)
Checks if a block applies to a particular page.
Definition: AbstractBlock.php:547
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:139
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
MediaWiki\Block\AbstractBlock\$reason
CommentStoreComment $reason
Definition: AbstractBlock.php:42
IContextSource\getRequest
getRequest()
MediaWiki\Block\AbstractBlock\getTargetUserIdentity
getTargetUserIdentity()
Definition: AbstractBlock.php:324
MediaWiki\Block\AbstractBlock\getHideName
getHideName()
Get whether the block hides the target's username.
Definition: AbstractBlock.php:184
MediaWiki\Block\AbstractBlock\$type
int null $type
AbstractBlock::TYPE_ constant.
Definition: AbstractBlock.php:81
MediaWiki\Block\AbstractBlock\$mHideName
bool $mHideName
Definition: AbstractBlock.php:69
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:398
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:194
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:67
MediaWiki\Block\AbstractBlock\getId
getId( $wikiId=self::LOCAL)
Get the block ID.(since 1.38) ?int
Definition: AbstractBlock.php:137
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:246
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:60
MediaWiki\Block\AbstractBlock\getReason
getReason()
Get the reason given for creating the block, as a string.
Definition: AbstractBlock.php:153