Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReferencePreviewsGadgetsIntegration
93.33% covered (success)
93.33%
14 / 15
80.00% covered (warning)
80.00%
4 / 5
8.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 sanitizeGadgetName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isGadgetEnabled
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 isNavPopupsGadgetEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRefTooltipsGadgetEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Cite\ReferencePreviews;
4
5use InvalidArgumentException;
6use MediaWiki\Config\Config;
7use MediaWiki\Extension\Gadgets\GadgetRepo;
8use MediaWiki\User\UserIdentity;
9
10/**
11 * Gadgets integration
12 *
13 * @license GPL-2.0-or-later
14 */
15class ReferencePreviewsGadgetsIntegration {
16
17    public const CONFIG_NAVIGATION_POPUPS_NAME = 'CiteReferencePreviewsConflictingNavPopupsGadgetName';
18    public const CONFIG_REFERENCE_TOOLTIPS_NAME = 'CiteReferencePreviewsConflictingRefTooltipsGadgetName';
19
20    private ?GadgetRepo $gadgetRepo;
21
22    private string $navPopupsGadgetName;
23    private string $refTooltipsGadgetName;
24
25    public function __construct( Config $config, ?GadgetRepo $gadgetRepo ) {
26        $this->navPopupsGadgetName = $this->sanitizeGadgetName(
27            $config->get( self::CONFIG_NAVIGATION_POPUPS_NAME ) );
28        $this->refTooltipsGadgetName = $this->sanitizeGadgetName(
29            $config->get( self::CONFIG_REFERENCE_TOOLTIPS_NAME ) );
30
31        $this->gadgetRepo = $gadgetRepo;
32    }
33
34    private function sanitizeGadgetName( string $gadgetName ): string {
35        return str_replace( ' ', '_', trim( $gadgetName ) );
36    }
37
38    private function isGadgetEnabled( UserIdentity $user, string $gadgetName ): bool {
39        if ( $this->gadgetRepo ) {
40            if ( in_array( $gadgetName, $this->gadgetRepo->getGadgetIds() ) ) {
41                try {
42                    return $this->gadgetRepo->getGadget( $gadgetName )
43                        ->isEnabled( $user );
44                } catch ( InvalidArgumentException $e ) {
45                    return false;
46                }
47            }
48        }
49        return false;
50    }
51
52    public function isNavPopupsGadgetEnabled( UserIdentity $user ): bool {
53        return $this->isGadgetEnabled( $user, $this->navPopupsGadgetName );
54    }
55
56    public function isRefTooltipsGadgetEnabled( UserIdentity $user ): bool {
57        return $this->isGadgetEnabled( $user, $this->refTooltipsGadgetName );
58    }
59
60}