Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.10% |
27 / 31 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| MakeGlobalVariablesScriptHookHandler | |
87.10% |
27 / 31 |
|
50.00% |
1 / 2 |
13.36 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onMakeGlobalVariablesScript | |
86.67% |
26 / 30 |
|
0.00% |
0 / 1 |
12.34 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace MediaWiki\Extension\ConfirmEdit\Hooks\Handlers; |
| 6 | |
| 7 | use MediaWiki\Config\Config; |
| 8 | use MediaWiki\Extension\ConfirmEdit\Hooks; |
| 9 | use MediaWiki\Extension\VisualEditor\Services\VisualEditorAvailabilityLookup; |
| 10 | use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook; |
| 11 | use MediaWiki\Registration\ExtensionRegistry; |
| 12 | use 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 | */ |
| 22 | class 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 | } |