3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Block
;
24 use InvalidArgumentException
;
26 use MediaWiki\MediaWikiServices
;
32 * @since 1.34 Factored out from DatabaseBlock (previously Block).
34 abstract class AbstractBlock
{
36 * @deprecated since 1.34. Use getReason and setReason instead.
42 * @deprecated since 1.34. Use getTimestamp and setTimestamp instead.
48 * @deprecated since 1.34. Use getExpiry and setExpiry instead.
54 protected $mBlockEmail = false;
57 protected $allowUsertalk = false;
60 protected $blockCreateAccount = false;
63 * @deprecated since 1.34. Use getHideName and setHideName instead.
66 public $mHideName = false;
68 /** @var User|string */
72 * @var int AbstractBlock::TYPE_ constant. After the block has been loaded
73 * from the database, this can only be USER, IP or RANGE.
81 protected $isSitewide = true;
91 * Create a new block with specified parameters on a user, IP or IP range.
93 * @param array $options Parameters of the block, with supported options:
94 * - address: (string|User) Target user name, User object, IP address or IP range
95 * - by: (int) User ID of the blocker
96 * - reason: (string) Reason of the block
97 * - timestamp: (string) The time at which the block comes into effect
98 * - byText: (string) Username of the blocker (for foreign users)
99 * - hideName: (bool) Hide the target user name
101 public function __construct( array $options = [] ) {
111 $options +
= $defaults;
113 $this->setTarget( $options['address'] );
115 if ( $options['by'] ) {
117 $this->setBlocker( User
::newFromId( $options['by'] ) );
120 $this->setBlocker( $options['byText'] );
123 $this->setReason( $options['reason'] );
124 $this->setTimestamp( wfTimestamp( TS_MW
, $options['timestamp'] ) );
125 $this->setHideName( (bool)$options['hideName'] );
129 * Get the user id of the blocking sysop
131 * @return int (0 for foreign users)
133 public function getBy() {
134 return $this->getBlocker()->getId();
138 * Get the username of the blocking sysop
142 public function getByName() {
143 return $this->getBlocker()->getName();
150 public function getId() {
155 * Get the reason given for creating the block
160 public function getReason() {
161 return $this->mReason
;
165 * Set the reason for creating the block
168 * @param string $reason
170 public function setReason( $reason ) {
171 $this->mReason
= $reason;
175 * Get whether the block hides the target's username
178 * @return bool The block hides the username
180 public function getHideName() {
181 return $this->mHideName
;
185 * Set whether ths block hides the target's username
188 * @param bool $hideName The block hides the username
190 public function setHideName( $hideName ) {
191 $this->mHideName
= $hideName;
195 * Indicates that the block is a sitewide block. This means the user is
196 * prohibited from editing any page on the site (other than their own talk
200 * @param null|bool $x
203 public function isSitewide( $x = null ) {
204 return wfSetVar( $this->isSitewide
, $x );
208 * Get or set the flag indicating whether this block blocks the target from
209 * creating an account. (Note that the flag may be overridden depending on
213 * @param null|bool $x Value to set (if null, just get the property value)
214 * @return bool Value of the property
216 public function isCreateAccountBlocked( $x = null ) {
217 return wfSetVar( $this->blockCreateAccount
, $x );
221 * Get or set the flag indicating whether this block blocks the target from
222 * sending emails. (Note that the flag may be overridden depending on
226 * @param null|bool $x Value to set (if null, just get the property value)
227 * @return bool Value of the property
229 public function isEmailBlocked( $x = null ) {
230 return wfSetVar( $this->mBlockEmail
, $x );
234 * Get or set the flag indicating whether this block blocks the target from
235 * editing their own user talk page. (Note that the flag may be overridden
236 * depending on global configs.)
239 * @param null|bool $x Value to set (if null, just get the property value)
240 * @return bool Value of the property
242 public function isUsertalkEditAllowed( $x = null ) {
243 return wfSetVar( $this->allowUsertalk
, $x );
247 * Determine whether the block prevents a given right. A right
248 * may be blacklisted or whitelisted, or determined from a
249 * property on the block object. For certain rights, the property
250 * may be overridden according to global configs.
253 * @param string $right
254 * @return bool|null The block applies to the right, or null if
255 * unsure (e.g. unrecognized right or unset property)
257 public function appliesToRight( $right ) {
258 $config = RequestContext
::getMain()->getConfig();
259 $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
264 // TODO: fix this case to return proper value
267 case 'createaccount':
268 $res = $this->isCreateAccountBlocked();
271 $res = $this->isEmailBlocked();
274 // Until T6995 is completed
275 $res = $this->isSitewide();
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();
289 $res = $permissionManager->userHasRight( $anon, $right ) ?
$res : true;
296 * Get/set whether the block prevents a given action
298 * @deprecated since 1.33, use appliesToRight to determine block
299 * behaviour, and specific methods to get/set properties
300 * @param string $action Action to check
301 * @param bool|null $x Value for set, or null to just get value
302 * @return bool|null Null for unrecognized rights.
304 public function prevents( $action, $x = null ) {
305 $config = RequestContext
::getMain()->getConfig();
306 $blockDisablesLogin = $config->get( 'BlockDisablesLogin' );
307 $blockAllowsUTEdit = $config->get( 'BlockAllowsUTEdit' );
312 # For now... <evil laugh>
315 case 'createaccount':
316 $res = wfSetVar( $this->blockCreateAccount
, $x );
319 $res = wfSetVar( $this->mBlockEmail
, $x );
322 // Until T6995 is completed
323 $res = $this->isSitewide();
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();
333 // edit own user talk can be disabled by config
334 if ( !$blockAllowsUTEdit ) {
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();
350 $res = $permissionManager->userHasRight( $anon, $action ) ?
$res : true;
357 * From an existing block, get the target and the type of target.
358 * Note that, except for null, it is always safe to treat the target
359 * as a string; for User objects this will return User::__toString()
360 * which in turn gives User::getName().
362 * @param string|int|User|null $target
363 * @return array [ User|String|null, AbstractBlock::TYPE_ constant|null ]
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
];
371 return [ $target, self
::TYPE_USER
];
373 } elseif ( $target === null ) {
374 return [ null, null ];
377 $target = trim( $target );
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)
383 User
::newFromName( IP
::sanitizeIP( $target ), false ),
387 } elseif ( IP
::isValidRange( $target ) ) {
388 # Can't create a User from an IP range
389 return [ IP
::sanitizeRange( $target ), self
::TYPE_RANGE
];
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];
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
];
406 } elseif ( preg_match( '/^#\d+$/', $target ) ) {
407 # Autoblock reference in the form "#12345"
408 return [ substr( $target, 1 ), self
::TYPE_AUTO
];
411 return [ null, null ];
416 * Get the type of target for this particular block.
417 * @return int AbstractBlock::TYPE_ constant, will never be TYPE_ID
419 public function getType() {
424 * Get the target and target type for this particular block. Note that for autoblocks,
425 * this returns the unredacted name; frontend functions need to call $block->getRedactedName()
427 * @return array [ User|String, AbstractBlock::TYPE_ constant ]
428 * @todo FIXME: This should be an integral part of the block member variables
430 public function getTargetAndType() {
431 return [ $this->getTarget(), $this->getType() ];
435 * Get the target for this particular block. Note that for autoblocks,
436 * this returns the unredacted name; frontend functions need to call $block->getRedactedName()
438 * @return User|string
440 public function getTarget() {
441 return $this->target
;
445 * Get the block expiry time
450 public function getExpiry() {
451 return $this->mExpiry
;
455 * Set the block expiry time
458 * @param string $expiry
460 public function setExpiry( $expiry ) {
461 $this->mExpiry
= $expiry;
465 * Get the timestamp indicating when the block was created
470 public function getTimestamp() {
471 return $this->mTimestamp
;
475 * Set the timestamp indicating when the block was created
478 * @param string $timestamp
480 public function setTimestamp( $timestamp ) {
481 $this->mTimestamp
= $timestamp;
485 * Set the target for this block, and update $this->type accordingly
486 * @param mixed $target
488 public function setTarget( $target ) {
489 list( $this->target
, $this->type
) = static::parseTarget( $target );
493 * Get the user who implemented this block
494 * @return User User object. May name a foreign user.
496 public function getBlocker() {
497 return $this->blocker
;
501 * Set the user who implemented (or will implement) this block
502 * @param User|string $user Local User object or username string
504 public function setBlocker( $user ) {
505 if ( is_string( $user ) ) {
506 $user = User
::newFromName( $user, false );
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'
515 $this->blocker
= $user;
519 * Get the key and parameters for the corresponding error message.
522 * @param IContextSource $context
525 abstract public function getPermissionsError( IContextSource
$context );
528 * Get block information used in different block error messages
531 * @param IContextSource $context
534 public function getBlockErrorParams( IContextSource
$context ) {
535 $lang = $context->getLanguage();
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
546 $reason = $this->getReason();
547 if ( $reason == '' ) {
548 $reason = $context->msg( 'blockednoreason' )->text();
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();
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.
563 $lang->formatExpiry( $this->getExpiry() ),
564 $lang->embedBidi( $intended ),
565 $lang->userTimeAndDate( $this->getTimestamp(), $context->getUser() ),
570 * Determine whether the block allows the user to edit their own
571 * user talk page. This is done separately from
572 * AbstractBlock::appliesToRight because there is no right for
573 * editing one's own user talk page and because the user's talk
574 * page needs to be passed into the block object, which is unaware
577 * The ipb_allow_usertalk flag (which corresponds to the property
578 * allowUsertalk) is used on sitewide blocks and partial blocks
579 * that contain a namespace restriction on the user talk namespace,
580 * but do not contain a page restriction on the user's talk page.
581 * For all other (i.e. most) partial blocks, the flag is ignored,
582 * and the user can always edit their user talk page unless there
583 * is a page restriction on their user talk page, in which case
584 * they can never edit it. (Ideally the flag would be stored as
585 * null in these cases, but the database field isn't nullable.)
587 * This method does not validate that the passed in talk page belongs to the
588 * block target since the target (an IP) might not be the same as the user's
589 * talk page (if they are logged in).
592 * @param Title|null $usertalk The user's user talk page. If null,
593 * and if the target is a User, the target's userpage is used
594 * @return bool The user can edit their talk page
596 public function appliesToUsertalk( Title
$usertalk = null ) {
598 if ( $this->target
instanceof User
) {
599 $usertalk = $this->target
->getTalkPage();
601 throw new InvalidArgumentException(
602 '$usertalk must be provided if block target is not a user/IP'
607 if ( $usertalk->getNamespace() !== NS_USER_TALK
) {
608 throw new InvalidArgumentException(
609 '$usertalk must be a user talk page'
613 if ( !$this->isSitewide() ) {
614 if ( $this->appliesToPage( $usertalk->getArticleID() ) ) {
617 if ( !$this->appliesToNamespace( NS_USER_TALK
) ) {
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' ) ) {
628 return !$this->isUsertalkEditAllowed();
632 * Checks if a block applies to a particular title
634 * This check does not consider whether `$this->isUsertalkEditAllowed`
635 * returns false, as the identity of the user making the hypothetical edit
636 * isn't known here (particularly in the case of IP hardblocks, range
637 * blocks, and auto-blocks).
639 * @param Title $title
642 public function appliesToTitle( Title
$title ) {
643 return $this->isSitewide();
647 * Checks if a block applies to a particular namespace
654 public function appliesToNamespace( $ns ) {
655 return $this->isSitewide();
659 * Checks if a block applies to a particular page
661 * This check does not consider whether `$this->isUsertalkEditAllowed`
662 * returns false, as the identity of the user making the hypothetical edit
663 * isn't known here (particularly in the case of IP hardblocks, range
664 * blocks, and auto-blocks).
671 public function appliesToPage( $pageId ) {
672 return $this->isSitewide();
676 * Check if the block should be tracked with a cookie.
679 * @deprecated since 1.34 Use BlockManager::trackBlockWithCookie instead
680 * of calling this directly.
681 * @param bool $isAnon The user is logged out
682 * @return bool The block should be tracked with a cookie
684 public function shouldTrackWithCookie( $isAnon ) {
685 wfDeprecated( __METHOD__
, '1.34' );
690 * Check if the block prevents a user from resetting their password
693 * @return bool The block blocks password reset
695 public function appliesToPasswordReset() {
696 return $this->isCreateAccountBlocked();