MediaWiki master
MediaWiki\Block\BlockManager Class Reference

A service class for checking blocks. More...

Public Member Functions

 __construct (ServiceOptions $options, UserFactory $userFactory, UserIdentityUtils $userIdentityUtils, LoggerInterface $logger, HookContainer $hookContainer, DatabaseBlockStore $blockStore, BlockTargetFactory $blockTargetFactory, ProxyLookup $proxyLookup)
 clearUserCache (UserIdentity $user)
 Clear the cache of any blocks that refer to the specified user.
 filter (?Block $block, $callback)
 Remove elements of a block which fail a callback test.
 getBlock (UserIdentity $user, ?WebRequest $request, $fromReplica=true)
 Get the blocks that apply to a user.
 getBlocksForIPList (array $ipChain, bool $applySoftBlocks, bool $fromPrimary)
 Get all blocks that match any IP from an array of IP addresses.
 getCreateAccountBlock (UserIdentity $user, ?WebRequest $request, $fromReplica)
 Get the block which applies to a create account action, if there is any.
 getIpBlock (string $ip, bool $fromReplica)
 Get the blocks that apply to an IP address.
 getIpRangeBlock (string $ipRange, bool $fromReplica=true)
 Given an IP range, check if a block applies to that range specifically.
 isDnsBlacklisted ( $ip, $checkAllowed=false)
 Whether the given IP is in a DNS blacklist.
 trackBlockWithCookie (User $user, WebResponse $response)
 Set the 'BlockID' cookie depending on block type and user authentication status.

Static Public Member Functions

static clearBlockCookie (WebResponse $response)
 Unset the 'BlockID' cookie.

Public Attributes

const CONSTRUCTOR_OPTIONS

Protected Member Functions

 checkHost ( $hostname)
 Wrapper for mocking in tests.

Detailed Description

A service class for checking blocks.

To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().

Since
1.34 Refactored from User and Block.

Definition at line 35 of file BlockManager.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Block\BlockManager::__construct ( ServiceOptions $options,
UserFactory $userFactory,
UserIdentityUtils $userIdentityUtils,
LoggerInterface $logger,
HookContainer $hookContainer,
DatabaseBlockStore $blockStore,
BlockTargetFactory $blockTargetFactory,
ProxyLookup $proxyLookup )

Definition at line 63 of file BlockManager.php.

Member Function Documentation

◆ checkHost()

MediaWiki\Block\BlockManager::checkHost ( $hostname)
protected

Wrapper for mocking in tests.

Parameters
string$hostnameDNSBL query
Returns
string[]|false IPv4 array, or false if the IP is not blacklisted

Definition at line 667 of file BlockManager.php.

◆ clearBlockCookie()

MediaWiki\Block\BlockManager::clearBlockCookie ( WebResponse $response)
static

Unset the 'BlockID' cookie.

Since
1.34
Parameters
WebResponse$response

Definition at line 787 of file BlockManager.php.

References MediaWiki\Request\WebResponse\clearCookie().

Referenced by trackBlockWithCookie().

◆ clearUserCache()

MediaWiki\Block\BlockManager::clearUserCache ( UserIdentity $user)

Clear the cache of any blocks that refer to the specified user.

Definition at line 163 of file BlockManager.php.

◆ filter()

MediaWiki\Block\BlockManager::filter ( ?Block $block,
$callback )

Remove elements of a block which fail a callback test.

Since
1.42
Parameters
Block | null$blockThe block, or null to pass in zero blocks.
callable$callbackThe callback, which will be called once for each non-composite component of the block. The only parameter is the non-composite Block. It should return true, to keep that component, or false, to remove that component.
Returns
Block|null
  • If there are zero remaining elements, null will be returned.
  • If there is one remaining element, a DatabaseBlock or some other non-composite block will be returned.
  • If there is more than one remaining element, a CompositeBlock will be returned.

Definition at line 239 of file BlockManager.php.

◆ getBlock()

MediaWiki\Block\BlockManager::getBlock ( UserIdentity $user,
?WebRequest $request,
$fromReplica = true )

Get the blocks that apply to a user.

If there is only one, return that, otherwise return a composite block that combines the strictest features of the applicable blocks.

If the user is exempt from IP blocks, the request should be null.

Since
1.42
Parameters
UserIdentity$userThe user performing the action
WebRequest | null$requestThe request to use for IP and cookie blocks, or null to skip checking for such blocks. If the user has the ipblock-exempt right, the request should be null.
bool$fromReplicaWhether to check the replica DB first. To improve performance, non-critical checks are done against replica DBs. Check when actually saving should be done against primary.
Returns
AbstractBlock|null

Definition at line 104 of file BlockManager.php.

References $request.

Referenced by getCreateAccountBlock().

◆ getBlocksForIPList()

MediaWiki\Block\BlockManager::getBlocksForIPList ( array $ipChain,
bool $applySoftBlocks,
bool $fromPrimary )

Get all blocks that match any IP from an array of IP addresses.

Access: internal
Public to support deprecated method in DatabaseBlock
Parameters
array$ipChainList of IPs (strings), usually retrieved from the X-Forwarded-For header of the request
bool$applySoftBlocksInclude soft blocks (anonymous-only blocks). These should only block anonymous and temporary users.
bool$fromPrimaryWhether to query the primary or replica DB
Returns
DatabaseBlock[]

Definition at line 447 of file BlockManager.php.

◆ getCreateAccountBlock()

MediaWiki\Block\BlockManager::getCreateAccountBlock ( UserIdentity $user,
?WebRequest $request,
$fromReplica )

Get the block which applies to a create account action, if there is any.

Since
1.42
Parameters
UserIdentity$user
WebRequest | null$requestThe request, or null to omit IP address and cookie blocks. If the user has the ipblock-exempt right, null should be passed.
bool$fromReplica
Returns
AbstractBlock|null

Definition at line 179 of file BlockManager.php.

References $request, and getBlock().

◆ getIpBlock()

MediaWiki\Block\BlockManager::getIpBlock ( string $ip,
bool $fromReplica )

Get the blocks that apply to an IP address.

If there is only one, return that, otherwise return a composite block that combines the strictest features of the applicable blocks.

Since
1.38
Parameters
string$ip
bool$fromReplica
Returns
AbstractBlock|null

Definition at line 306 of file BlockManager.php.

References if.

◆ getIpRangeBlock()

MediaWiki\Block\BlockManager::getIpRangeBlock ( string $ipRange,
bool $fromReplica = true )

Given an IP range, check if a block applies to that range specifically.

Since
1.46
Parameters
string$ipRange
bool$fromReplica
Returns
DatabaseBlock|null

Definition at line 327 of file BlockManager.php.

References if.

◆ isDnsBlacklisted()

MediaWiki\Block\BlockManager::isDnsBlacklisted ( $ip,
$checkAllowed = false )

Whether the given IP is in a DNS blacklist.

Parameters
string$ipIP to check
bool$checkAllowedWhether to check $wgProxyWhitelist first
Returns
bool True if blacklisted.

Definition at line 598 of file BlockManager.php.

References MediaWiki\MainConfigNames\DnsBlacklistUrls, MediaWiki\MainConfigNames\EnableDnsBlacklist, and MediaWiki\MainConfigNames\ProxyWhitelist.

◆ trackBlockWithCookie()

MediaWiki\Block\BlockManager::trackBlockWithCookie ( User $user,
WebResponse $response )

Set the 'BlockID' cookie depending on block type and user authentication status.

If a block cookie is already set, this will check the block that the cookie references and do the following:

  • If the block is a valid block that should be applied, do nothing and return early. This ensures that the cookie's expiry time is based on the time of the first page load or attempt. (See discussion on T233595.)
  • If the block is invalid (e.g. has expired), clear the cookie and continue to check whether there is another block that should be tracked.
  • If the block is a valid block, but should not be tracked by a cookie, clear the cookie and continue to check whether there is another block that should be tracked.

Must be called after the User object is loaded, and before headers are sent.

Since
1.34
Parameters
User$user
WebResponse$responseThe response on which to set the cookie.

Definition at line 690 of file BlockManager.php.

References $request, clearBlockCookie(), MediaWiki\MainConfigNames\CookieSetOnAutoblock, MediaWiki\MainConfigNames\CookieSetOnIpBlock, MediaWiki\User\User\getBlock(), MediaWiki\User\User\getRequest(), MediaWiki\Request\WebResponse\headersSent(), MediaWiki\User\User\isAnon(), and MediaWiki\User\User\isSafeToLoad().

Member Data Documentation

◆ CONSTRUCTOR_OPTIONS

const MediaWiki\Block\BlockManager::CONSTRUCTOR_OPTIONS
Initial value:
= [
]
const DnsBlacklistUrls
Name constant for the DnsBlacklistUrls setting, for use with Config\get().
const SoftBlockRanges
Name constant for the SoftBlockRanges setting, for use with Config\get().
const CookieSetOnAutoblock
Name constant for the CookieSetOnAutoblock setting, for use with Config\get().
const EnableDnsBlacklist
Name constant for the EnableDnsBlacklist setting, for use with Config\get().
const ApplyIpBlocksToXff
Name constant for the ApplyIpBlocksToXff setting, for use with Config\get().
const ProxyList
Name constant for the ProxyList setting, for use with Config\get().
const ProxyWhitelist
Name constant for the ProxyWhitelist setting, for use with Config\get().
const CookieSetOnIpBlock
Name constant for the CookieSetOnIpBlock setting, for use with Config\get().
const SecretKey
Name constant for the SecretKey setting, for use with Config\get().
Access: internal
For use by ServiceWiring

Definition at line 39 of file BlockManager.php.


The documentation for this class was generated from the following file: