Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
23 / 24
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopupHandler
95.83% covered (success)
95.83%
23 / 24
50.00% covered (danger)
50.00%
1 / 2
13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 onBeforePageDisplay
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\IPInfo\HookHandler;
4
5use MediaWiki\MediaWikiServices;
6use MediaWiki\Output\Hook\BeforePageDisplayHook;
7use MediaWiki\Permissions\PermissionManager;
8use MediaWiki\Registration\ExtensionRegistry;
9use MediaWiki\User\Options\UserOptionsLookup;
10
11class PopupHandler implements BeforePageDisplayHook {
12    private PermissionManager $permissionManager;
13    private UserOptionsLookup $userOptionsLookup;
14
15    public function __construct(
16        PermissionManager $permissionManager,
17        UserOptionsLookup $userOptionsLookup
18    ) {
19        $this->permissionManager = $permissionManager;
20        $this->userOptionsLookup = $userOptionsLookup;
21    }
22
23    /** @inheritDoc */
24    public function onBeforePageDisplay( $out, $skin ): void {
25        // T339861: Don't load on mobile until T268177 is resolved
26        $services = MediaWikiServices::getInstance();
27        $extensionRegistry = ExtensionRegistry::getInstance();
28        if (
29            $extensionRegistry->isLoaded( 'MobileFrontend' ) &&
30            $services->getService( 'MobileFrontend.Context' )->shouldDisplayMobileView()
31        ) {
32            return;
33        }
34
35        if (
36            $out->getRequest()->getRawVal( 'action' ) !== 'history' &&
37            !( $out->getTitle() &&
38                ( $out->getTitle()->isSpecial( 'Log' ) ||
39                     $out->getTitle()->isSpecial( 'Recentchanges' ) ||
40                     $out->getTitle()->isSpecial( 'Watchlist' )
41                )
42            )
43        ) {
44            return;
45        }
46
47        $user = $out->getUser();
48        $isBetaFeaturesLoaded = $extensionRegistry->isLoaded( 'BetaFeatures' );
49
50        if (
51            !$this->permissionManager->userHasRight( $user, 'ipinfo' ) ||
52            !$this->userOptionsLookup->getOption( $user, 'ipinfo-use-agreement' ) ||
53            ( $isBetaFeaturesLoaded &&
54                !$this->userOptionsLookup->getOption( $user, 'ipinfo-beta-feature-enable' )
55            )
56        ) {
57            return;
58        }
59
60        $out->addModules( 'ext.ipInfo' );
61        $out->addModuleStyles( 'ext.ipInfo.styles' );
62    }
63}