MediaWiki  master
AbstractBlock.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Block;
22 
24 use IContextSource;
25 use InvalidArgumentException;
27 use Message;
28 use RequestContext;
29 use Title;
30 use User;
31 use Wikimedia\IPUtils;
32 
36 abstract class AbstractBlock {
41  protected $mReason;
42 
44  protected $reason;
45 
50  public $mTimestamp;
51 
56  public $mExpiry = '';
57 
59  protected $mBlockEmail = false;
60 
62  protected $allowUsertalk = false;
63 
65  protected $blockCreateAccount = false;
66 
71  public $mHideName = false;
72 
74  protected $target;
75 
80  protected $type;
81 
83  protected $isSitewide = true;
84 
85  # TYPE constants
86  const TYPE_USER = 1;
87  const TYPE_IP = 2;
88  const TYPE_RANGE = 3;
89  const TYPE_AUTO = 4;
90  const TYPE_ID = 5;
91 
101  public function __construct( array $options = [] ) {
102  $defaults = [
103  'address' => '',
104  'reason' => '',
105  'timestamp' => '',
106  'hideName' => false,
107  ];
108 
109  $options += $defaults;
110 
111  $this->setTarget( $options['address'] );
112 
113  $this->setReason( $options['reason'] );
114  $this->setTimestamp( wfTimestamp( TS_MW, $options['timestamp'] ) );
115  $this->setHideName( (bool)$options['hideName'] );
116  }
117 
123  abstract public function getBy();
124 
130  abstract public function getByName();
131 
136  public function getId() {
137  return null;
138  }
139 
147  abstract public function getIdentifier();
148 
159  public function getReason() {
160  $language = RequestContext::getMain()->getLanguage();
161  return $this->reason->message->inLanguage( $language )->plain();
162  }
163 
170  public function getReasonComment() {
171  return $this->reason;
172  }
173 
180  public function setReason( $reason ) {
182  }
183 
190  public function getHideName() {
191  return $this->mHideName;
192  }
193 
200  public function setHideName( $hideName ) {
201  $this->mHideName = $hideName;
202  }
203 
213  public function isSitewide( $x = null ) {
214  return wfSetVar( $this->isSitewide, $x );
215  }
216 
226  public function isCreateAccountBlocked( $x = null ) {
227  return wfSetVar( $this->blockCreateAccount, $x );
228  }
229 
239  public function isEmailBlocked( $x = null ) {
240  return wfSetVar( $this->mBlockEmail, $x );
241  }
242 
252  public function isUsertalkEditAllowed( $x = null ) {
253  return wfSetVar( $this->allowUsertalk, $x );
254  }
255 
267  public function appliesToRight( $right ) {
268  $config = RequestContext::getMain()->getConfig();
269  $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
270 
271  $res = null;
272  switch ( $right ) {
273  case 'edit':
274  // TODO: fix this case to return proper value
275  $res = true;
276  break;
277  case 'createaccount':
278  $res = $this->isCreateAccountBlocked();
279  break;
280  case 'sendemail':
281  $res = $this->isEmailBlocked();
282  break;
283  case 'upload':
284  // Until T6995 is completed
285  $res = $this->isSitewide();
286  break;
287  case 'read':
288  $res = false;
289  break;
290  case 'purge':
291  $res = false;
292  break;
293  }
294  if ( !$res && $blockDisablesLogin ) {
295  // If a block would disable login, then it should
296  // prevent any right that all users cannot do
297  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
298  $anon = new User;
299  $res = $permissionManager->userHasRight( $anon, $right ) ? $res : true;
300  }
301 
302  return $res;
303  }
304 
314  public function prevents( $action, $x = null ) {
315  wfDeprecated( __METHOD__, '1.33' );
316  $config = RequestContext::getMain()->getConfig();
317  $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
318  $blockAllowsUTEdit = $config->get( 'BlockAllowsUTEdit' );
319 
320  $res = null;
321  switch ( $action ) {
322  case 'edit':
323  # For now... <evil laugh>
324  $res = true;
325  break;
326  case 'createaccount':
327  $res = wfSetVar( $this->blockCreateAccount, $x );
328  break;
329  case 'sendemail':
330  $res = wfSetVar( $this->mBlockEmail, $x );
331  break;
332  case 'upload':
333  // Until T6995 is completed
334  $res = $this->isSitewide();
335  break;
336  case 'editownusertalk':
337  // NOTE: this check is not reliable on partial blocks
338  // since partially blocked users are always allowed to edit
339  // their own talk page unless a restriction exists on the
340  // page or User_talk: namespace
341  wfSetVar( $this->allowUsertalk, $x === null ? null : !$x );
342  $res = !$this->isUsertalkEditAllowed();
343 
344  // edit own user talk can be disabled by config
345  if ( !$blockAllowsUTEdit ) {
346  $res = true;
347  }
348  break;
349  case 'read':
350  $res = false;
351  break;
352  case 'purge':
353  $res = false;
354  break;
355  }
356  if ( !$res && $blockDisablesLogin ) {
357  // If a block would disable login, then it should
358  // prevent any action that all users cannot do
359  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
360  $anon = new User;
361  $res = $permissionManager->userHasRight( $anon, $action ) ? $res : true;
362  }
363 
364  return $res;
365  }
366 
378  public static function parseTarget( $target ) {
379  # We may have been through this before
380  if ( $target instanceof User ) {
381  if ( IPUtils::isValid( $target->getName() ) ) {
382  return [ $target, self::TYPE_IP ];
383  } else {
384  return [ $target, self::TYPE_USER ];
385  }
386  } elseif ( $target === null ) {
387  return [ null, null ];
388  }
389 
390  $target = trim( $target );
391 
392  if ( IPUtils::isValid( $target ) ) {
393  # We can still create a User if it's an IP address, but we need to turn
394  # off validation checking (which would exclude IP addresses)
395  return [
396  User::newFromName( IPUtils::sanitizeIP( $target ), false ),
398  ];
399 
400  } elseif ( IPUtils::isValidRange( $target ) ) {
401  # Can't create a User from an IP range
402  return [ IPUtils::sanitizeRange( $target ), self::TYPE_RANGE ];
403  }
404 
405  # Consider the possibility that this is not a username at all
406  # but actually an old subpage (T31797)
407  if ( strpos( $target, '/' ) !== false ) {
408  # An old subpage, drill down to the user behind it
409  $target = explode( '/', $target )[0];
410  }
411 
412  $userObj = User::newFromName( $target );
413  if ( $userObj instanceof User ) {
414  # Note that since numbers are valid usernames, a $target of "12345" will be
415  # considered a User. If you want to pass a block ID, prepend a hash "#12345",
416  # since hash characters are not valid in usernames or titles generally.
417  return [ $userObj, self::TYPE_USER ];
418 
419  } elseif ( preg_match( '/^#\d+$/', $target ) ) {
420  # Autoblock reference in the form "#12345"
421  return [ substr( $target, 1 ), self::TYPE_AUTO ];
422 
423  } else {
424  return [ null, null ];
425  }
426  }
427 
432  public function getType() {
433  return $this->type;
434  }
435 
446  public function getTargetAndType() {
447  return [ $this->getTarget(), $this->getType() ];
448  }
449 
456  public function getTarget() {
457  return $this->target;
458  }
459 
466  public function getExpiry() {
467  return $this->mExpiry;
468  }
469 
476  public function setExpiry( $expiry ) {
477  $this->mExpiry = $expiry;
478  }
479 
486  public function getTimestamp() {
487  return $this->mTimestamp;
488  }
489 
496  public function setTimestamp( $timestamp ) {
497  $this->mTimestamp = $timestamp;
498  }
499 
504  public function setTarget( $target ) {
505  list( $this->target, $this->type ) = static::parseTarget( $target );
506  }
507 
513  public function getBlocker() {
514  wfDeprecated( __METHOD__, '1.35' );
515  return null;
516  }
517 
523  public function setBlocker( $user ) {
524  wfDeprecated( __METHOD__, '1.35' );
525  }
526 
537  $message = MediaWikiServices::getInstance()
538  ->getBlockErrorFormatter()->getMessage(
539  $this,
540  $context->getUser(),
542  $context->getRequest()->getIP()
543  );
544  return array_merge( [ [ $message->getKey() ], $message->getParams() ] );
545  }
546 
557  wfDeprecated( __METHOD__, '1.35' );
559  ->getBlockErrorFormatter()->getMessage(
560  $this,
561  $context->getUser(),
563  $context->getRequest()->getIP()
564  )->getParams();
565  }
566 
594  public function appliesToUsertalk( Title $usertalk = null ) {
595  if ( !$usertalk ) {
596  if ( $this->target instanceof User ) {
597  $usertalk = $this->target->getTalkPage();
598  } else {
599  throw new InvalidArgumentException(
600  '$usertalk must be provided if block target is not a user/IP'
601  );
602  }
603  }
604 
605  if ( $usertalk->getNamespace() !== NS_USER_TALK ) {
606  throw new InvalidArgumentException(
607  '$usertalk must be a user talk page'
608  );
609  }
610 
611  if ( !$this->isSitewide() ) {
612  if ( $this->appliesToPage( $usertalk->getArticleID() ) ) {
613  return true;
614  }
615  if ( !$this->appliesToNamespace( NS_USER_TALK ) ) {
616  return false;
617  }
618  }
619 
620  // This is a type of block which uses the ipb_allow_usertalk
621  // flag. The flag can still be overridden by global configs.
622  $config = RequestContext::getMain()->getConfig();
623  if ( !$config->get( 'BlockAllowsUTEdit' ) ) {
624  return true;
625  }
626  return !$this->isUsertalkEditAllowed();
627  }
628 
640  public function appliesToTitle( Title $title ) {
641  return $this->isSitewide();
642  }
643 
652  public function appliesToNamespace( $ns ) {
653  return $this->isSitewide();
654  }
655 
669  public function appliesToPage( $pageId ) {
670  return $this->isSitewide();
671  }
672 
682  public function shouldTrackWithCookie( $isAnon ) {
683  wfDeprecated( __METHOD__, '1.34' );
684  return false;
685  }
686 
693  public function appliesToPasswordReset() {
694  return $this->isCreateAccountBlocked();
695  }
696 
697 }
MediaWiki\Block\AbstractBlock\appliesToNamespace
appliesToNamespace( $ns)
Checks if a block applies to a particular namespace.
Definition: AbstractBlock.php:652
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:59
MediaWiki\Block\AbstractBlock\$target
User string null $target
Definition: AbstractBlock.php:74
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:130
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:1606
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1871
MediaWiki\Block\AbstractBlock\setBlocker
setBlocker( $user)
Set the user who implemented (or will implement) this block.
Definition: AbstractBlock.php:523
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:226
MediaWiki\Block\AbstractBlock\TYPE_AUTO
const TYPE_AUTO
Definition: AbstractBlock.php:89
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:537
MediaWiki\Block\AbstractBlock\getId
getId()
Get the block ID.
Definition: AbstractBlock.php:136
Message
$res
$res
Definition: testCompression.php:54
MediaWiki\Block\AbstractBlock\getTargetAndType
getTargetAndType()
Get the target and target type for this particular block.
Definition: AbstractBlock.php:446
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:594
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:161
MediaWiki\Block\AbstractBlock\TYPE_RANGE
const TYPE_RANGE
Definition: AbstractBlock.php:88
MediaWiki\Block\AbstractBlock\appliesToPasswordReset
appliesToPasswordReset()
Check if the block prevents a user from resetting their password.
Definition: AbstractBlock.php:693
MediaWiki\Block\AbstractBlock\$mTimestamp
string $mTimestamp
Definition: AbstractBlock.php:50
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1044
MediaWiki\Block\AbstractBlock\TYPE_ID
const TYPE_ID
Definition: AbstractBlock.php:90
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:239
MediaWiki\Block\AbstractBlock\getReasonComment
getReasonComment()
Get the reason for creating the block.
Definition: AbstractBlock.php:170
MediaWiki\Block\AbstractBlock\TYPE_IP
const TYPE_IP
Definition: AbstractBlock.php:87
$title
$title
Definition: testCompression.php:36
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:432
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:213
MediaWiki\Block\AbstractBlock\shouldTrackWithCookie
shouldTrackWithCookie( $isAnon)
Check if the block should be tracked with a cookie.
Definition: AbstractBlock.php:682
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:83
MediaWiki\Block\AbstractBlock\setTimestamp
setTimestamp( $timestamp)
Set the timestamp indicating when the block was created.
Definition: AbstractBlock.php:496
MediaWiki\Block\AbstractBlock\parseTarget
static parseTarget( $target)
From an existing block, get the target and the type of target.
Definition: AbstractBlock.php:378
MediaWiki\Block\AbstractBlock\TYPE_USER
const TYPE_USER
Definition: AbstractBlock.php:86
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:513
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:65
MediaWiki\Block\AbstractBlock\getPermissionsError
getPermissionsError(IContextSource $context)
Get the key and parameters for the corresponding error message.
Definition: AbstractBlock.php:536
MediaWiki\Block\AbstractBlock\appliesToTitle
appliesToTitle(Title $title)
Checks if a block applies to a particular title.
Definition: AbstractBlock.php:640
MediaWiki\Block\AbstractBlock\$mReason
$mReason
Definition: AbstractBlock.php:41
MediaWiki\Block\AbstractBlock\getBlockErrorParams
getBlockErrorParams(IContextSource $context)
Get block information used in different block error messages.
Definition: AbstractBlock.php:556
MediaWiki\Block\AbstractBlock\setExpiry
setExpiry( $expiry)
Set the block expiry time.
Definition: AbstractBlock.php:476
MediaWiki\Block\AbstractBlock\$mExpiry
string $mExpiry
Definition: AbstractBlock.php:56
IContextSource\getUser
getUser()
MediaWiki\Block\AbstractBlock\getExpiry
getExpiry()
Get the block expiry time.
Definition: AbstractBlock.php:466
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:447
MediaWiki\Block\AbstractBlock\prevents
prevents( $action, $x=null)
Get/set whether the block prevents a given action.
Definition: AbstractBlock.php:314
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:486
MediaWiki\$action
string $action
Cache what action this request is.
Definition: MediaWiki.php:42
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:267
MediaWiki\Block\AbstractBlock\setReason
setReason( $reason)
Set the reason for creating the block.
Definition: AbstractBlock.php:180
MediaWiki\Block\AbstractBlock\appliesToPage
appliesToPage( $pageId)
Checks if a block applies to a particular page.
Definition: AbstractBlock.php:669
MediaWiki\$config
Config $config
Definition: MediaWiki.php:39
MediaWiki\Block\AbstractBlock\$reason
CommentStoreComment $reason
Definition: AbstractBlock.php:44
IContextSource\getRequest
getRequest()
MediaWiki\Block\AbstractBlock\getHideName
getHideName()
Get whether the block hides the target's username.
Definition: AbstractBlock.php:190
MediaWiki\Block\AbstractBlock\$type
int null $type
AbstractBlock::TYPE_ constant.
Definition: AbstractBlock.php:80
MediaWiki\Block\AbstractBlock\$mHideName
bool $mHideName
Definition: AbstractBlock.php:71
MediaWiki\Block\AbstractBlock
Definition: AbstractBlock.php:36
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:37
MediaWiki\Block\AbstractBlock\setTarget
setTarget( $target)
Set the target for this block, and update $this->type accordingly.
Definition: AbstractBlock.php:504
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:200
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:52
MediaWiki\Block\AbstractBlock\getTarget
getTarget()
Get the target for this particular block.
Definition: AbstractBlock.php:456
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2284
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:252
CommentStoreComment
CommentStoreComment represents a comment stored by CommentStore.
Definition: CommentStoreComment.php:29
IContextSource\getLanguage
getLanguage()
MediaWiki\Block\AbstractBlock\$allowUsertalk
bool $allowUsertalk
Definition: AbstractBlock.php:62
MediaWiki\Block\AbstractBlock\getReason
getReason()
Get the reason given for creating the block, as a string.
Definition: AbstractBlock.php:159