Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
36.00% covered (danger)
36.00%
9 / 25
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopupsGadgetsIntegration
36.00% covered (danger)
36.00%
9 / 25
60.00% covered (warning)
60.00%
3 / 5
42.72
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
 isGadgetExtensionEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 conflictsWithNavPopupsGadget
22.22% covered (danger)
22.22%
2 / 9
0.00% covered (danger)
0.00%
0 / 1
11.53
 conflictsWithRefTooltipsGadget
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/** This file is part of the MediaWiki extension Popups.
3 *
4 * Popups is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Popups is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Popups.  If not, see <http://www.gnu.org/licenses/>.
16 *
17 * @file
18 * @ingroup extensions
19 */
20namespace Popups;
21
22use ExtensionRegistry;
23use MediaWiki\Config\Config;
24use MediaWiki\Extension\Gadgets\GadgetRepo;
25use MediaWiki\User\User;
26
27/**
28 * Gadgets integration
29 *
30 * @package Popups
31 */
32class PopupsGadgetsIntegration {
33
34    public const CONFIG_NAVIGATION_POPUPS_NAME = 'PopupsConflictingNavPopupsGadgetName';
35
36    public const CONFIG_REFERENCE_TOOLTIPS_NAME = 'PopupsConflictingRefTooltipsGadgetName';
37
38    /**
39     * @var \ExtensionRegistry
40     */
41    private $extensionRegistry;
42
43    /**
44     * @var string
45     */
46    private $navPopupsGadgetName;
47
48    /**
49     * @var string
50     */
51    private $refTooltipsGadgetName;
52
53    /**
54     * @param Config $config MediaWiki configuration
55     * @param ExtensionRegistry $extensionRegistry MediaWiki extension registry
56     */
57    public function __construct( Config $config, ExtensionRegistry $extensionRegistry ) {
58        $this->extensionRegistry = $extensionRegistry;
59        $this->navPopupsGadgetName = $this->sanitizeGadgetName(
60            $config->get( self::CONFIG_NAVIGATION_POPUPS_NAME ) );
61        $this->refTooltipsGadgetName = $this->sanitizeGadgetName(
62            $config->get( self::CONFIG_REFERENCE_TOOLTIPS_NAME ) );
63    }
64
65    /**
66     * @param string $gadgetName
67     * @return string
68     */
69    private function sanitizeGadgetName( $gadgetName ) {
70        return str_replace( ' ', '_', trim( $gadgetName ) );
71    }
72
73    /**
74     * @return bool
75     */
76    private function isGadgetExtensionEnabled() {
77        return $this->extensionRegistry->isLoaded( 'Gadgets' );
78    }
79
80    /**
81     * Check if Popups conflicts with Nav Popups Gadget
82     * If user enabled Nav Popups, Popups is unavailable
83     *
84     * @param User $user User whose gadget settings are checked
85     * @return bool
86     */
87    public function conflictsWithNavPopupsGadget( User $user ) {
88        if ( $this->isGadgetExtensionEnabled() ) {
89            $gadgetsRepo = GadgetRepo::singleton();
90            $match = array_search( $this->navPopupsGadgetName, $gadgetsRepo->getGadgetIds() );
91            if ( $match !== false ) {
92                try {
93                    return $gadgetsRepo->getGadget( $this->navPopupsGadgetName )
94                        ->isEnabled( $user );
95                } catch ( \InvalidArgumentException $e ) {
96                    return false;
97                }
98            }
99        }
100        return false;
101    }
102
103    /**
104     * Check if Popups conflicts with Ref Tooltips Gadget
105     * If user enabled Ref Tooltip, Popups is unavailable
106     *
107     * @param User $user User whose gadget settings are checked
108     * @return bool
109     */
110    public function conflictsWithRefTooltipsGadget( User $user ) {
111        if ( $this->isGadgetExtensionEnabled() ) {
112            $gadgetsRepo = GadgetRepo::singleton();
113            $match = array_search( $this->refTooltipsGadgetName, $gadgetsRepo->getGadgetIds() );
114            if ( $match !== false ) {
115                try {
116                    return $gadgetsRepo->getGadget( $this->refTooltipsGadgetName )
117                        ->isEnabled( $user );
118                } catch ( \InvalidArgumentException $e ) {
119                    return false;
120                }
121            }
122        }
123        return false;
124    }
125
126}