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