Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.10% covered (warning)
87.10%
27 / 31
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MakeGlobalVariablesScriptHookHandler
87.10% covered (warning)
87.10%
27 / 31
50.00% covered (danger)
50.00%
1 / 2
13.36
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onMakeGlobalVariablesScript
86.67% covered (warning)
86.67%
26 / 30
0.00% covered (danger)
0.00%
0 / 1
12.34
1<?php
2
3declare( strict_types = 1 );
4
5namespace MediaWiki\Extension\ConfirmEdit\Hooks\Handlers;
6
7use MediaWiki\Config\Config;
8use MediaWiki\Extension\ConfirmEdit\Hooks;
9use MediaWiki\Extension\VisualEditor\Services\VisualEditorAvailabilityLookup;
10use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook;
11use MediaWiki\Registration\ExtensionRegistry;
12use MobileContext;
13
14/**
15 * Adds a JavaScript configuration variable that indicates what kind of captcha is required to be completed for
16 * editing the page that the user is viewing or editing. This is only the generic sense, as the specific content
17 * of the edit may cause a captcha to appear at save time.
18 *
19 * Used by the VisualEditor and MobileFrontend integrations to determine if they need to display hCaptcha to the
20 * user.
21 */
22class MakeGlobalVariablesScriptHookHandler implements MakeGlobalVariablesScriptHook {
23
24    /**
25     * @param ExtensionRegistry $extensionRegistry
26     * @param Config $config
27     * @param VisualEditorAvailabilityLookup|null $visualEditorAvailabilityLookup
28     * @param MobileContext|null $mobileContext
29     */
30    public function __construct(
31        private readonly ExtensionRegistry $extensionRegistry,
32        private readonly Config $config,
33        private $visualEditorAvailabilityLookup = null,
34        private $mobileContext = null
35    ) {
36    }
37
38    /** @inheritDoc */
39    public function onMakeGlobalVariablesScript( &$vars, $out ): void {
40        $mobileFrontendAvailable = $this->extensionRegistry->isLoaded( 'MobileFrontend' );
41        $visualEditorAvailable = $this->extensionRegistry->isLoaded( 'VisualEditor' );
42
43        if ( $visualEditorAvailable && $this->visualEditorAvailabilityLookup !== null ) {
44            $visualEditorAvailable = $this->visualEditorAvailabilityLookup->isAvailable(
45                $out->getTitle(), $out->getRequest(), $out->getUser()
46            );
47        }
48
49        if ( $mobileFrontendAvailable && $this->mobileContext !== null ) {
50            $mobileFrontendAvailable = $this->mobileContext->shouldDisplayMobileView();
51        }
52
53        // No code uses the config variables defined here if VisualEditor and
54        // MobileFrontend are not loaded or their editors cannot be used for the
55        // current request
56        if ( !$visualEditorAvailable && !$mobileFrontendAvailable ) {
57            return;
58        }
59
60        if ( !$out->canUseWikiPage() ) {
61            $vars['wgConfirmEditCaptchaNeededForGenericEdit'] = false;
62            return;
63        }
64
65        $captchaNeededForEdit = false;
66
67        $action = Hooks::getCaptchaTriggerActionFromTitle( $out->getTitle() );
68        $captchaInstance = Hooks::getInstance( $action );
69        if (
70            $captchaInstance->shouldCheck( $out->getWikiPage(), '', '', $out->getContext() )
71        ) {
72            $captchaNeededForEdit = strtolower( $captchaInstance->getName() );
73        }
74
75        $vars['wgConfirmEditCaptchaNeededForGenericEdit'] = $captchaNeededForEdit;
76        if ( $captchaNeededForEdit === 'hcaptcha' ) {
77            $vars['wgConfirmEditHCaptchaVisualEditorOnLoadIntegrationEnabled'] = $visualEditorAvailable &&
78                $this->config->get( 'HCaptchaVisualEditorOnLoadIntegrationEnabled' );
79
80            $vars['wgConfirmEditHCaptchaSiteKey'] = null;
81            if ( $captchaInstance->shouldForceShowCaptcha() ) {
82                $vars['wgConfirmEditHCaptchaSiteKey'] =
83                    $captchaInstance->getConfig()['HCaptchaAlwaysChallengeSiteKey'] ??
84                    $vars['wgConfirmEditHCaptchaSiteKey'];
85            }
86            $vars['wgConfirmEditHCaptchaSiteKey'] ??=
87                $captchaInstance->getConfig()['HCaptchaSiteKey'] ??
88                $out->getConfig()->get( 'HCaptchaSiteKey' );
89        }
90    }
91}