Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AutoPromoteGroupsHandler
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onGetAutoPromoteGroups
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers;
4
5use MediaWiki\Extension\AbuseFilter\BlockAutopromoteStore;
6use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry;
7use MediaWiki\User\Hook\GetAutoPromoteGroupsHook;
8use MediaWiki\User\UserIdentity;
9use Wikimedia\ObjectCache\BagOStuff;
10use Wikimedia\ObjectCache\HashBagOStuff;
11
12class 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}