Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
14 / 17
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReferencePreviewsGadgetsIntegration
82.35% covered (warning)
82.35%
14 / 17
60.00% covered (warning)
60.00%
3 / 5
9.45
0.00% covered (danger)
0.00%
0 / 1
 __construct
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
2.09
 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\MediaWikiServices;
9use MediaWiki\User\User;
10use MediaWiki\User\UserIdentity;
11use Wikimedia\Services\NoSuchServiceException;
12
13/**
14 * Gadgets integration
15 *
16 * @license GPL-2.0-or-later
17 */
18class ReferencePreviewsGadgetsIntegration {
19
20    public const CONFIG_NAVIGATION_POPUPS_NAME = 'CiteReferencePreviewsConflictingNavPopupsGadgetName';
21    public const CONFIG_REFERENCE_TOOLTIPS_NAME = 'CiteReferencePreviewsConflictingRefTooltipsGadgetName';
22
23    private ?GadgetRepo $gadgetRepo;
24
25    private string $navPopupsGadgetName;
26    private string $refTooltipsGadgetName;
27
28    public function __construct( Config $config ) {
29        $this->navPopupsGadgetName = $this->sanitizeGadgetName(
30            $config->get( self::CONFIG_NAVIGATION_POPUPS_NAME ) );
31        $this->refTooltipsGadgetName = $this->sanitizeGadgetName(
32            $config->get( self::CONFIG_REFERENCE_TOOLTIPS_NAME ) );
33
34        try {
35            $this->gadgetRepo = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' );
36        } catch ( NoSuchServiceException $e ) {
37            $this->gadgetRepo = null;
38        }
39    }
40
41    private function sanitizeGadgetName( string $gadgetName ): string {
42        return str_replace( ' ', '_', trim( $gadgetName ) );
43    }
44
45    private function isGadgetEnabled( UserIdentity $user, string $gadgetName ): bool {
46        if ( $this->gadgetRepo ) {
47            if ( in_array( $gadgetName, $this->gadgetRepo->getGadgetIds() ) ) {
48                try {
49                    return $this->gadgetRepo->getGadget( $gadgetName )
50                        ->isEnabled( $user );
51                } catch ( InvalidArgumentException $e ) {
52                    return false;
53                }
54            }
55        }
56        return false;
57    }
58
59    public function isNavPopupsGadgetEnabled( User $user ): bool {
60        return $this->isGadgetEnabled( $user, $this->navPopupsGadgetName );
61    }
62
63    public function isRefTooltipsGadgetEnabled( User $user ): bool {
64        return $this->isGadgetEnabled( $user, $this->refTooltipsGadgetName );
65    }
66
67}