Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AutoPromoteGroupsHandler | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onGetAutoPromoteGroups | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers; |
| 4 | |
| 5 | use MediaWiki\Extension\AbuseFilter\BlockAutopromoteStore; |
| 6 | use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry; |
| 7 | use MediaWiki\User\Hook\GetAutoPromoteGroupsHook; |
| 8 | use MediaWiki\User\UserIdentity; |
| 9 | use Wikimedia\ObjectCache\BagOStuff; |
| 10 | use Wikimedia\ObjectCache\HashBagOStuff; |
| 11 | |
| 12 | class AutoPromoteGroupsHandler implements GetAutoPromoteGroupsHook { |
| 13 | |
| 14 | /** @var BagOStuff */ |
| 15 | private $cache; |
| 16 | |
| 17 | public function __construct( |
| 18 | private readonly ConsequencesRegistry $consequencesRegistry, |
| 19 | private readonly BlockAutopromoteStore $blockAutopromoteStore, |
| 20 | ?BagOStuff $cache = null |
| 21 | ) { |
| 22 | $this->cache = $cache ?? new HashBagOStuff(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @param UserIdentity $user |
| 27 | * @param string[] &$promote |
| 28 | */ |
| 29 | public function onGetAutoPromoteGroups( $user, &$promote ): void { |
| 30 | if ( |
| 31 | in_array( 'blockautopromote', $this->consequencesRegistry->getAllEnabledActionNames() ) |
| 32 | && $promote |
| 33 | ) { |
| 34 | // Proxy the blockautopromote data to a faster backend, using an appropriate key |
| 35 | $quickCacheKey = $this->cache->makeKey( |
| 36 | 'abusefilter', |
| 37 | 'blockautopromote', |
| 38 | 'quick', |
| 39 | $user->getId() |
| 40 | ); |
| 41 | $blocked = (bool)$this->cache->getWithSetCallback( |
| 42 | $quickCacheKey, |
| 43 | BagOStuff::TTL_PROC_LONG, |
| 44 | function () use ( $user ) { |
| 45 | return $this->blockAutopromoteStore->getAutoPromoteBlockStatus( $user ); |
| 46 | } |
| 47 | ); |
| 48 | |
| 49 | if ( $blocked ) { |
| 50 | $promote = []; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |