Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EarlyLifeCycleHooks
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onMessageCacheFetchOverrides
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 userHasPersonalToolsPrefEnabled
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace GrowthExperiments;
4
5use MediaWiki\Cache\Hook\MessageCacheFetchOverridesHook;
6use MediaWiki\Config\ConfigException;
7use MediaWiki\User\Options\UserOptionsLookup;
8use MediaWiki\User\User;
9use RequestContext;
10
11/**
12 * Hook handler class that contains hooks which are riskier than the average (called often,
13 * or early in the request lifecycle, or both), and so we want to separate them out from
14 * hook handlers with lots of context dependencies (e.g. HomepageHooks), so that there's
15 * less chance of causing dependency loops.
16 */
17class EarlyLifeCycleHooks implements MessageCacheFetchOverridesHook {
18
19    private UserOptionsLookup $userOptionsLookup;
20
21    /**
22     * @param UserOptionsLookup $userOptionsLookup
23     */
24    public function __construct( UserOptionsLookup $userOptionsLookup ) {
25        $this->userOptionsLookup = $userOptionsLookup;
26    }
27
28    /**
29     * Change the tooltip of the userpage link when it point to Special:Homepage
30     *
31     * @param callable[] &$keys message keys to convert
32     * @throws ConfigException
33     */
34    public function onMessageCacheFetchOverrides( array &$keys ): void {
35        $keys['tooltip-pt-userpage'] = function ( string $key ): string {
36            $user = RequestContext::getMain()->getUser();
37
38            if ( $user->isSafeToLoad()
39                && HomepageHooks::isHomepageEnabled( $user )
40                && $this->userHasPersonalToolsPrefEnabled( $user )
41            ) {
42                return 'tooltip-pt-homepage';
43            }
44
45            return $key;
46        };
47    }
48
49    /**
50     * @param User $user
51     * @return bool
52     */
53    private function userHasPersonalToolsPrefEnabled( User $user ): bool {
54        return $user->isNamed()
55            && $this->userOptionsLookup->getBoolOption( $user, HomepageHooks::HOMEPAGE_PREF_PT_LINK );
56    }
57
58}