MediaWiki  1.34.0
AbstractBlock.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Block;
22 
23 use IContextSource;
24 use InvalidArgumentException;
25 use IP;
27 use RequestContext;
28 use Title;
29 use User;
30 
34 abstract class AbstractBlock {
39  public $mReason;
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 $target;
70 
75  protected $type;
76 
78  protected $blocker;
79 
81  protected $isSitewide = true;
82 
83  # TYPE constants
84  const TYPE_USER = 1;
85  const TYPE_IP = 2;
86  const TYPE_RANGE = 3;
87  const TYPE_AUTO = 4;
88  const TYPE_ID = 5;
89 
101  public function __construct( array $options = [] ) {
102  $defaults = [
103  'address' => '',
104  'by' => null,
105  'reason' => '',
106  'timestamp' => '',
107  'byText' => '',
108  'hideName' => false,
109  ];
110 
111  $options += $defaults;
112 
113  $this->setTarget( $options['address'] );
114 
115  if ( $options['by'] ) {
116  # Local user
117  $this->setBlocker( User::newFromId( $options['by'] ) );
118  } else {
119  # Foreign user
120  $this->setBlocker( $options['byText'] );
121  }
122 
123  $this->setReason( $options['reason'] );
124  $this->setTimestamp( wfTimestamp( TS_MW, $options['timestamp'] ) );
125  $this->setHideName( (bool)$options['hideName'] );
126  }
127 
133  public function getBy() {
134  return $this->getBlocker()->getId();
135  }
136 
142  public function getByName() {
143  return $this->getBlocker()->getName();
144  }
145 
150  public function getId() {
151  return null;
152  }
153 
160  public function getReason() {
161  return $this->mReason;
162  }
163 
170  public function setReason( $reason ) {
171  $this->mReason = $reason;
172  }
173 
180  public function getHideName() {
181  return $this->mHideName;
182  }
183 
190  public function setHideName( $hideName ) {
191  $this->mHideName = $hideName;
192  }
193 
203  public function isSitewide( $x = null ) {
204  return wfSetVar( $this->isSitewide, $x );
205  }
206 
216  public function isCreateAccountBlocked( $x = null ) {
217  return wfSetVar( $this->blockCreateAccount, $x );
218  }
219 
229  public function isEmailBlocked( $x = null ) {
230  return wfSetVar( $this->mBlockEmail, $x );
231  }
232 
242  public function isUsertalkEditAllowed( $x = null ) {
243  return wfSetVar( $this->allowUsertalk, $x );
244  }
245 
257  public function appliesToRight( $right ) {
258  $config = RequestContext::getMain()->getConfig();
259  $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
260 
261  $res = null;
262  switch ( $right ) {
263  case 'edit':
264  // TODO: fix this case to return proper value
265  $res = true;
266  break;
267  case 'createaccount':
268  $res = $this->isCreateAccountBlocked();
269  break;
270  case 'sendemail':
271  $res = $this->isEmailBlocked();
272  break;
273  case 'upload':
274  // Until T6995 is completed
275  $res = $this->isSitewide();
276  break;
277  case 'read':
278  $res = false;
279  break;
280  case 'purge':
281  $res = false;
282  break;
283  }
284  if ( !$res && $blockDisablesLogin ) {
285  // If a block would disable login, then it should
286  // prevent any right that all users cannot do
287  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
288  $anon = new User;
289  $res = $permissionManager->userHasRight( $anon, $right ) ? $res : true;
290  }
291 
292  return $res;
293  }
294 
304  public function prevents( $action, $x = null ) {
305  $config = RequestContext::getMain()->getConfig();
306  $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
307  $blockAllowsUTEdit = $config->get( 'BlockAllowsUTEdit' );
308 
309  $res = null;
310  switch ( $action ) {
311  case 'edit':
312  # For now... <evil laugh>
313  $res = true;
314  break;
315  case 'createaccount':
316  $res = wfSetVar( $this->blockCreateAccount, $x );
317  break;
318  case 'sendemail':
319  $res = wfSetVar( $this->mBlockEmail, $x );
320  break;
321  case 'upload':
322  // Until T6995 is completed
323  $res = $this->isSitewide();
324  break;
325  case 'editownusertalk':
326  // NOTE: this check is not reliable on partial blocks
327  // since partially blocked users are always allowed to edit
328  // their own talk page unless a restriction exists on the
329  // page or User_talk: namespace
330  wfSetVar( $this->allowUsertalk, $x === null ? null : !$x );
331  $res = !$this->isUsertalkEditAllowed();
332 
333  // edit own user talk can be disabled by config
334  if ( !$blockAllowsUTEdit ) {
335  $res = true;
336  }
337  break;
338  case 'read':
339  $res = false;
340  break;
341  case 'purge':
342  $res = false;
343  break;
344  }
345  if ( !$res && $blockDisablesLogin ) {
346  // If a block would disable login, then it should
347  // prevent any action that all users cannot do
348  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
349  $anon = new User;
350  $res = $permissionManager->userHasRight( $anon, $action ) ? $res : true;
351  }
352 
353  return $res;
354  }
355 
365  public static function parseTarget( $target ) {
366  # We may have been through this before
367  if ( $target instanceof User ) {
368  if ( IP::isValid( $target->getName() ) ) {
369  return [ $target, self::TYPE_IP ];
370  } else {
371  return [ $target, self::TYPE_USER ];
372  }
373  } elseif ( $target === null ) {
374  return [ null, null ];
375  }
376 
377  $target = trim( $target );
378 
379  if ( IP::isValid( $target ) ) {
380  # We can still create a User if it's an IP address, but we need to turn
381  # off validation checking (which would exclude IP addresses)
382  return [
385  ];
386 
387  } elseif ( IP::isValidRange( $target ) ) {
388  # Can't create a User from an IP range
390  }
391 
392  # Consider the possibility that this is not a username at all
393  # but actually an old subpage (T31797)
394  if ( strpos( $target, '/' ) !== false ) {
395  # An old subpage, drill down to the user behind it
396  $target = explode( '/', $target )[0];
397  }
398 
399  $userObj = User::newFromName( $target );
400  if ( $userObj instanceof User ) {
401  # Note that since numbers are valid usernames, a $target of "12345" will be
402  # considered a User. If you want to pass a block ID, prepend a hash "#12345",
403  # since hash characters are not valid in usernames or titles generally.
404  return [ $userObj, self::TYPE_USER ];
405 
406  } elseif ( preg_match( '/^#\d+$/', $target ) ) {
407  # Autoblock reference in the form "#12345"
408  return [ substr( $target, 1 ), self::TYPE_AUTO ];
409 
410  } else {
411  return [ null, null ];
412  }
413  }
414 
419  public function getType() {
420  return $this->type;
421  }
422 
430  public function getTargetAndType() {
431  return [ $this->getTarget(), $this->getType() ];
432  }
433 
440  public function getTarget() {
441  return $this->target;
442  }
443 
450  public function getExpiry() {
451  return $this->mExpiry;
452  }
453 
460  public function setExpiry( $expiry ) {
461  $this->mExpiry = $expiry;
462  }
463 
470  public function getTimestamp() {
471  return $this->mTimestamp;
472  }
473 
480  public function setTimestamp( $timestamp ) {
481  $this->mTimestamp = $timestamp;
482  }
483 
488  public function setTarget( $target ) {
489  list( $this->target, $this->type ) = static::parseTarget( $target );
490  }
491 
496  public function getBlocker() {
497  return $this->blocker;
498  }
499 
504  public function setBlocker( $user ) {
505  if ( is_string( $user ) ) {
506  $user = User::newFromName( $user, false );
507  }
508 
509  if ( $user->isAnon() && User::isUsableName( $user->getName() ) ) {
510  throw new InvalidArgumentException(
511  'Blocker must be a local user or a name that cannot be a local user'
512  );
513  }
514 
515  $this->blocker = $user;
516  }
517 
525  abstract public function getPermissionsError( IContextSource $context );
526 
536 
537  $blocker = $this->getBlocker();
538  if ( $blocker instanceof User ) { // local user
539  $blockerUserpage = $blocker->getUserPage();
540  $blockerText = $lang->embedBidi( $blockerUserpage->getText() );
541  $link = "[[{$blockerUserpage->getPrefixedText()}|{$blockerText}]]";
542  } else { // foreign user
543  $link = $blocker;
544  }
545 
546  $reason = $this->getReason();
547  if ( $reason == '' ) {
548  $reason = $context->msg( 'blockednoreason' )->text();
549  }
550 
551  /* $ip returns who *is* being blocked, $intended contains who was meant to be blocked.
552  * This could be a username, an IP range, or a single IP. */
553  $intended = (string)$this->getTarget();
554 
555  return [
556  $link,
557  $reason,
558  $context->getRequest()->getIP(),
559  $lang->embedBidi( $this->getByName() ),
560  // TODO: SystemBlock replaces this with the system block type. Clean up
561  // error params so that this is not necessary.
562  $this->getId(),
563  $lang->formatExpiry( $this->getExpiry() ),
564  $lang->embedBidi( $intended ),
565  $lang->userTimeAndDate( $this->getTimestamp(), $context->getUser() ),
566  ];
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.
Definition: AbstractBlock.php:133
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:539
MediaWiki\Block\AbstractBlock\$mBlockEmail
bool $mBlockEmail
Definition: AbstractBlock.php:54
MediaWiki\Block\AbstractBlock\$target
User string $target
Definition: AbstractBlock.php:69
MediaWiki\Block\AbstractBlock\$blocker
User $blocker
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:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
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:1607
MediaWiki\Block\AbstractBlock\$mReason
string $mReason
Definition: AbstractBlock.php:39
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
MediaWiki\Block\AbstractBlock\setBlocker
setBlocker( $user)
Set the user who implemented (or will implement) this block.
Definition: AbstractBlock.php:504
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:216
MediaWiki\Block\AbstractBlock\TYPE_AUTO
const TYPE_AUTO
Definition: AbstractBlock.php:87
IP
A collection of public static functions to play with IP address and IP ranges.
Definition: IP.php:67
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
User\getUserPage
getUserPage()
Get this user's personal page title.
Definition: User.php:4275
MediaWiki\Block\AbstractBlock\getId
getId()
Get the block ID.
Definition: AbstractBlock.php:150
$res
$res
Definition: testCompression.php:52
MediaWiki\Block\AbstractBlock\getTargetAndType
getTargetAndType()
Get the target and target type for this particular block.
Definition: AbstractBlock.php:430
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\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:138
MediaWiki\Block\AbstractBlock\TYPE_RANGE
const TYPE_RANGE
Definition: AbstractBlock.php:86
IP\isValidRange
static isValidRange( $ipRange)
Validate an IP range (valid address with a valid CIDR prefix).
Definition: IP.php:125
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:45
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1044
MessageLocalizer\msg
msg( $key,... $params)
This is the method for getting translated interface messages.
MediaWiki\Block\AbstractBlock\TYPE_ID
const TYPE_ID
Definition: AbstractBlock.php:88
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:229
MediaWiki\Block\AbstractBlock\TYPE_IP
const TYPE_IP
Definition: AbstractBlock.php:85
$title
$title
Definition: testCompression.php:34
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:33
MediaWiki\Block\AbstractBlock\getType
getType()
Get the type of target for this particular block.
Definition: AbstractBlock.php:419
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:203
MediaWiki\Block\AbstractBlock\shouldTrackWithCookie
shouldTrackWithCookie( $isAnon)
Check if the block should be tracked with a cookie.
Definition: AbstractBlock.php:684
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:480
MediaWiki\Block\AbstractBlock\parseTarget
static parseTarget( $target)
From an existing block, get the target and the type of target.
Definition: AbstractBlock.php:365
MediaWiki\Block\AbstractBlock\TYPE_USER
const TYPE_USER
Definition: AbstractBlock.php:84
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:63
MediaWiki\Block\AbstractBlock\getBlocker
getBlocker()
Get the user who implemented this block.
Definition: AbstractBlock.php:496
MediaWiki\Block\AbstractBlock\__construct
__construct(array $options=[])
Create a new block with specified parameters on a user, IP or IP range.
Definition: AbstractBlock.php:101
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.
MediaWiki\Block\AbstractBlock\appliesToTitle
appliesToTitle(Title $title)
Checks if a block applies to a particular title.
Definition: AbstractBlock.php:642
MediaWiki\Block\AbstractBlock\getBlockErrorParams
getBlockErrorParams(IContextSource $context)
Get block information used in different block error messages.
Definition: AbstractBlock.php:534
MediaWiki\Block\AbstractBlock\setExpiry
setExpiry( $expiry)
Set the block expiry time.
Definition: AbstractBlock.php:460
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:450
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:431
IP\isValid
static isValid( $ip)
Validate an IP address.
Definition: IP.php:111
MediaWiki\Block\AbstractBlock\prevents
prevents( $action, $x=null)
Get/set whether the block prevents a given action.
Definition: AbstractBlock.php:304
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:470
MediaWiki\$action
string $action
Cache what action this request is.
Definition: MediaWiki.php:48
IP\sanitizeIP
static sanitizeIP( $ip)
Convert an IP into a verbose, uppercase, normalized form.
Definition: IP.php:139
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:257
MediaWiki\Block\AbstractBlock\setReason
setReason( $reason)
Set the reason for creating the block.
Definition: AbstractBlock.php:170
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:43
IContextSource\getRequest
getRequest()
MediaWiki\Block\AbstractBlock\getHideName
getHideName()
Get whether the block hides the target's username.
Definition: AbstractBlock.php:180
MediaWiki\Block\AbstractBlock\$mHideName
bool $mHideName
Definition: AbstractBlock.php:66
MediaWiki\Block\AbstractBlock
Definition: AbstractBlock.php:34
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:38
User\isUsableName
static isUsableName( $name)
Usernames which fail to pass this function will be blocked from user login and new account registrati...
Definition: User.php:966
MediaWiki\Block\AbstractBlock\setTarget
setTarget( $target)
Set the target for this block, and update $this->type accordingly.
Definition: AbstractBlock.php:488
MediaWiki\Block\AbstractBlock\getByName
getByName()
Get the username of the blocking sysop.
Definition: AbstractBlock.php:142
MediaWiki\Block\AbstractBlock\setHideName
setHideName( $hideName)
Set whether ths block hides the target's username.
Definition: AbstractBlock.php:190
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
MediaWiki\Block\AbstractBlock\getTarget
getTarget()
Get the target for this particular block.
Definition: AbstractBlock.php:440
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2232
IP\sanitizeRange
static sanitizeRange( $range)
Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111....
Definition: IP.php:712
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:242
IContextSource\getLanguage
getLanguage()
MediaWiki\Block\AbstractBlock\$type
int $type
AbstractBlock::TYPE_ constant.
Definition: AbstractBlock.php:75
MediaWiki\Block\AbstractBlock\$allowUsertalk
bool $allowUsertalk
Definition: AbstractBlock.php:57
MediaWiki\Block\AbstractBlock\getReason
getReason()
Get the reason given for creating the block.
Definition: AbstractBlock.php:160