Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
8 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReferencePreviewsContext
72.73% covered (warning)
72.73%
8 / 11
50.00% covered (danger)
50.00%
1 / 2
7.99
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 isReferencePreviewsEnabled
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3namespace Cite\ReferencePreviews;
4
5use MediaWiki\Config\Config;
6use MediaWiki\User\Options\UserOptionsLookup;
7use MediaWiki\User\User;
8use Skin;
9
10/**
11 * @license GPL-2.0-or-later
12 */
13class ReferencePreviewsContext {
14
15    private Config $config;
16    private ReferencePreviewsGadgetsIntegration $gadgetsIntegration;
17    private UserOptionsLookup $userOptionsLookup;
18
19    public function __construct(
20        Config $config,
21        ReferencePreviewsGadgetsIntegration $gadgetsIntegration,
22        UserOptionsLookup $userOptionsLookup
23    ) {
24        $this->config = $config;
25        $this->gadgetsIntegration = $gadgetsIntegration;
26        $this->userOptionsLookup = $userOptionsLookup;
27    }
28
29    /**
30     * User preference key to enable/disable Reference Previews. Named
31     * "mwe-popups-referencePreviews-enabled" in localStorage for anonymous users.
32     */
33    public const REFERENCE_PREVIEWS_PREFERENCE_NAME = 'popups-reference-previews';
34
35    /**
36     * If the client-side code for Reference Previews should continue loading
37     * (see isReferencePreviewsEnabled.js), incorporating decisions we can only make after the
38     * ResourceLoader module was registered via {@see CiteHooks::onResourceLoaderRegisterModules}.
39     */
40    public function isReferencePreviewsEnabled( User $user, Skin $skin ): bool {
41        if (
42            // T243822: Temporarily disabled in the mobile skin
43            $skin->getSkinName() === 'minerva' ||
44            // The feature flag is also checked in the ResourceLoaderRegisterModules hook handler
45            // and technically redundant here, but it's cheap; better safe than sorry
46            !$this->config->get( 'CiteReferencePreviews' ) ||
47            $this->gadgetsIntegration->isRefToolTipsGadgetEnabled( $user ) ||
48            $this->gadgetsIntegration->isNavPopupsGadgetEnabled( $user )
49        ) {
50            return false;
51        }
52
53        // Anonymous users can (de)activate the feature via a cookie at runtime, hence it must load
54        return !$user->isNamed() || $this->userOptionsLookup->getBoolOption(
55            $user, self::REFERENCE_PREVIEWS_PREFERENCE_NAME
56        );
57    }
58}