diff --git a/extension.json b/extension.json index 74b982b0d..4797452ce 100644 --- a/extension.json +++ b/extension.json @@ -96,7 +96,10 @@ ] }, "AbuseFilterHooks": { - "class": "MediaWiki\\Extension\\ConfirmEdit\\AbuseFilterHooks" + "class": "MediaWiki\\Extension\\ConfirmEdit\\AbuseFilterHooks", + "services": [ + "MainConfig" + ] } }, "Hooks": { @@ -170,6 +173,10 @@ }, "CaptchaRegexes": { "value": [] + }, + "ConfirmEditEnabledAbuseFilterCustomActions": { + "description": "Feature flag to toggle list of available custom actions to enable in AbuseFilter. See AbuseFilterHooks::onAbuseFilterCustomActions", + "value": [] } }, "manifest_version": 2 diff --git a/includes/AbuseFilterHooks.php b/includes/AbuseFilterHooks.php index 6326d2b4c..4adf60857 100644 --- a/includes/AbuseFilterHooks.php +++ b/includes/AbuseFilterHooks.php @@ -2,17 +2,27 @@ namespace MediaWiki\Extension\ConfirmEdit; +use MediaWiki\Config\Config; use MediaWiki\Extension\AbuseFilter\Consequences\Parameters; use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterCustomActionsHook; use MediaWiki\Extension\ConfirmEdit\AbuseFilter\CaptchaConsequence; class AbuseFilterHooks implements AbuseFilterCustomActionsHook { + private Config $config; + + public function __construct( Config $config ) { + $this->config = $config; + } + /** @inheritDoc */ public function onAbuseFilterCustomActions( array &$actions ): void { - $actions['showcaptcha'] = static function ( Parameters $params ): CaptchaConsequence { - return new CaptchaConsequence( $params ); - }; + $enabledActions = $this->config->get( 'ConfirmEditEnabledAbuseFilterCustomActions' ); + if ( in_array( 'showcaptcha', $enabledActions ) ) { + $actions['showcaptcha'] = static function ( Parameters $params ): CaptchaConsequence { + return new CaptchaConsequence( $params ); + }; + } } } diff --git a/tests/phpunit/unit/AbuseFilterHooksTest.php b/tests/phpunit/unit/AbuseFilterHooksTest.php index 6bd6a68f4..636920d99 100644 --- a/tests/phpunit/unit/AbuseFilterHooksTest.php +++ b/tests/phpunit/unit/AbuseFilterHooksTest.php @@ -2,6 +2,7 @@ namespace MediaWiki\Extension\ConfirmEdit\Test\Unit\AbuseFilter; +use MediaWiki\Config\HashConfig; use MediaWiki\Extension\ConfirmEdit\AbuseFilterHooks; use MediaWikiUnitTestCase; @@ -11,7 +12,8 @@ use MediaWikiUnitTestCase; class AbuseFilterHooksTest extends MediaWikiUnitTestCase { public function testOnAbuseFilterCustomActions() { - $abuseFilterHooks = new AbuseFilterHooks(); + $config = new HashConfig( [ 'ConfirmEditEnabledAbuseFilterCustomActions' => [ 'showcaptcha' ] ] ); + $abuseFilterHooks = new AbuseFilterHooks( $config ); $actions = []; $abuseFilterHooks->onAbuseFilterCustomActions( $actions ); $this->assertArrayHasKey( 'showcaptcha', $actions );