Add GetAllBlockActions hook

Call onGetAllBlockActions to add to the blocked actions array.
This allows extensions to define blockable actions.

Bug: T279562
Change-Id: Ia72fdabba1dcca0e2f71e4c09400453295f0e05c
This commit is contained in:
STran 2021-04-19 03:18:20 -07:00
parent 9c269ae3b8
commit df89e2fb49
5 changed files with 91 additions and 2 deletions

View file

@ -35,6 +35,7 @@ class HookRunner implements
\MediaWiki\Auth\Hook\SecuritySensitiveOperationStatusHook,
\MediaWiki\Auth\Hook\UserLoggedInHook,
\MediaWiki\Block\Hook\AbortAutoblockHook,
\MediaWiki\Block\Hook\GetAllBlockActionsHook,
\MediaWiki\Block\Hook\GetUserBlockHook,
\MediaWiki\Block\Hook\PerformRetroactiveAutoblockHook,
\MediaWiki\Cache\Hook\BacklinkCacheGetConditionsHook,
@ -1826,6 +1827,14 @@ class HookRunner implements
);
}
public function onGetAllBlockActions( &$actions ) {
return $this->container->run(
'GetAllBlockActions',
[ &$actions ],
[ 'abortable' => false ]
);
}
public function onGetAutoPromoteGroups( $user, &$promote ) {
return $this->container->run(
'GetAutoPromoteGroups',

View file

@ -214,7 +214,7 @@ return [
},
'BlockActionInfo' => static function ( MediaWikiServices $services ) : BlockActionInfo {
return new BlockActionInfo();
return new BlockActionInfo( $services->getHookContainer() );
},
'BlockErrorFormatter' => static function ( MediaWikiServices $services ) : BlockErrorFormatter {

View file

@ -20,6 +20,9 @@
namespace MediaWiki\Block;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
/**
* Defines the actions that can be blocked by a partial block. They are
* always blocked by a sitewide block.
@ -34,6 +37,8 @@ namespace MediaWiki\Block;
* @since 1.37
*/
class BlockActionInfo {
/** @var HookRunner */
private $hookRunner;
/** @var int */
private const ACTION_UPLOAD = 1;
@ -55,11 +60,29 @@ class BlockActionInfo {
'upload' => self::ACTION_UPLOAD,
];
/**
* @param HookContainer $hookContainer
*/
public function __construct( HookContainer $hookContainer ) {
$this->hookRunner = new HookRunner( $hookContainer );
}
/**
* Cache the array of actions
* @var int[]|null
*/
private $allBlockActions = null;
/**
* @return int[]
*/
public function getAllBlockActions() : array {
return self::CORE_BLOCK_ACTIONS;
// Don't run the hook multiple times in the same request
if ( !$this->allBlockActions ) {
$this->allBlockActions = self::CORE_BLOCK_ACTIONS;
$this->hookRunner->onGetAllBlockActions( $this->allBlockActions );
}
return $this->allBlockActions;
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace MediaWiki\Block\Hook;
/**
* This is a hook handler interface, see docs/Hooks.md.
* Use the hook name "GetAllBlockActions" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface GetAllBlockActionsHook {
/**
* Use this hook to add an action to block on
*
* @since 1.37
*
* @param int[] &$actions Array of actions, which may be added to
* @return bool|void True or no return value to continue; callers of this hook should not abort it
*/
public function onGetAllBlockActions( &$actions );
}

View file

@ -0,0 +1,35 @@
<?php
use MediaWiki\Block\BlockActionInfo;
/**
* @group Blocking
* @coversDefaultClass \MediaWiki\Block\BlockActionInfo
*/
class BlockActionInfoTest extends MediaWikiUnitTestCase {
/** @var HookContainer */
private $hookContainer;
protected function setUp(): void {
parent::setUp();
$this->hookContainer = $this->createHookContainer();
}
/**
* @covers ::getAllBlockActions
*/
public function testAddBlockAction() {
$this->hookContainer->register(
'GetAllBlockActions',
static function ( array &$actions ) {
$actions[ 'test' ] = 100;
}
);
$blockActionInfo = new BlockActionInfo( $this->hookContainer );
$blockActions = $blockActionInfo->getAllBlockActions();
// Confirm new action is added
$this->assertContains( 100, $blockActions );
}
}