AbuseFilterHooks: Provide feature flags for AF custom actions

Why:

- There are issues with the current "showcaptcha" action, and we need to
  disable it until the problems are fixed

What:

- Define a feature flag to enable custom actions. For now, nothing is
  enabled.

Bug: T20110
Change-Id: I3484d66298bc9f49dfbe003a0605e2ac1a092e10
This commit is contained in:
Kosta Harlan 2024-05-15 08:25:56 +02:00
parent 90dd0cfaad
commit 81e9cb99c0
No known key found for this signature in database
GPG key ID: BC3D8915606A5ED9
3 changed files with 24 additions and 5 deletions

View file

@ -96,7 +96,10 @@
] ]
}, },
"AbuseFilterHooks": { "AbuseFilterHooks": {
"class": "MediaWiki\\Extension\\ConfirmEdit\\AbuseFilterHooks" "class": "MediaWiki\\Extension\\ConfirmEdit\\AbuseFilterHooks",
"services": [
"MainConfig"
]
} }
}, },
"Hooks": { "Hooks": {
@ -170,6 +173,10 @@
}, },
"CaptchaRegexes": { "CaptchaRegexes": {
"value": [] "value": []
},
"ConfirmEditEnabledAbuseFilterCustomActions": {
"description": "Feature flag to toggle list of available custom actions to enable in AbuseFilter. See AbuseFilterHooks::onAbuseFilterCustomActions",
"value": []
} }
}, },
"manifest_version": 2 "manifest_version": 2

View file

@ -2,17 +2,27 @@
namespace MediaWiki\Extension\ConfirmEdit; namespace MediaWiki\Extension\ConfirmEdit;
use MediaWiki\Config\Config;
use MediaWiki\Extension\AbuseFilter\Consequences\Parameters; use MediaWiki\Extension\AbuseFilter\Consequences\Parameters;
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterCustomActionsHook; use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterCustomActionsHook;
use MediaWiki\Extension\ConfirmEdit\AbuseFilter\CaptchaConsequence; use MediaWiki\Extension\ConfirmEdit\AbuseFilter\CaptchaConsequence;
class AbuseFilterHooks implements AbuseFilterCustomActionsHook { class AbuseFilterHooks implements AbuseFilterCustomActionsHook {
private Config $config;
public function __construct( Config $config ) {
$this->config = $config;
}
/** @inheritDoc */ /** @inheritDoc */
public function onAbuseFilterCustomActions( array &$actions ): void { public function onAbuseFilterCustomActions( array &$actions ): void {
$actions['showcaptcha'] = static function ( Parameters $params ): CaptchaConsequence { $enabledActions = $this->config->get( 'ConfirmEditEnabledAbuseFilterCustomActions' );
return new CaptchaConsequence( $params ); if ( in_array( 'showcaptcha', $enabledActions ) ) {
}; $actions['showcaptcha'] = static function ( Parameters $params ): CaptchaConsequence {
return new CaptchaConsequence( $params );
};
}
} }
} }

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Extension\ConfirmEdit\Test\Unit\AbuseFilter; namespace MediaWiki\Extension\ConfirmEdit\Test\Unit\AbuseFilter;
use MediaWiki\Config\HashConfig;
use MediaWiki\Extension\ConfirmEdit\AbuseFilterHooks; use MediaWiki\Extension\ConfirmEdit\AbuseFilterHooks;
use MediaWikiUnitTestCase; use MediaWikiUnitTestCase;
@ -11,7 +12,8 @@ use MediaWikiUnitTestCase;
class AbuseFilterHooksTest extends MediaWikiUnitTestCase { class AbuseFilterHooksTest extends MediaWikiUnitTestCase {
public function testOnAbuseFilterCustomActions() { public function testOnAbuseFilterCustomActions() {
$abuseFilterHooks = new AbuseFilterHooks(); $config = new HashConfig( [ 'ConfirmEditEnabledAbuseFilterCustomActions' => [ 'showcaptcha' ] ] );
$abuseFilterHooks = new AbuseFilterHooks( $config );
$actions = []; $actions = [];
$abuseFilterHooks->onAbuseFilterCustomActions( $actions ); $abuseFilterHooks->onAbuseFilterCustomActions( $actions );
$this->assertArrayHasKey( 'showcaptcha', $actions ); $this->assertArrayHasKey( 'showcaptcha', $actions );