Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MfLanguageSearcherEntrypointsRegistrationHandler
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isMobileView
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 isCXEntrypointEnabled
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 onBeforePageDisplay
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2declare( strict_types = 1 );
3
4namespace ContentTranslation\HookHandler;
5
6use ContentTranslation\PreferenceHelper;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Output\Hook\BeforePageDisplayHook;
9use MediaWiki\Registration\ExtensionRegistry;
10use MediaWiki\Title\Title;
11use MediaWiki\User\User;
12
13/**
14 * This class implements a handler for the "BeforePageDisplay" hook, that
15 * registers the "ext.cx.entrypoints.languagesearcher.init" RL module when
16 * the appropriate conditions are met. The following JS variables are also
17 * set when the same conditions are met:
18 * - wgSectionTranslationTargetLanguages
19 * - isLanguageSearcherCXEntrypointEnabled
20 * - mintEntrypointLanguages
21 *
22 * @author Nik Gkountas
23 * @license GPL-2.0-or-later
24 * @since 2024.06
25 */
26class MfLanguageSearcherEntrypointsRegistrationHandler implements BeforePageDisplayHook {
27
28    private PreferenceHelper $preferenceHelper;
29
30    public function __construct( PreferenceHelper $preferenceHelper ) {
31        $this->preferenceHelper = $preferenceHelper;
32    }
33
34    /**
35     * Check whether the current context is in a mobile interface
36     *
37     * @return bool
38     */
39    private static function isMobileView() {
40        $isMobileView = false;
41
42        if ( ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) ) {
43            $mobileContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' );
44            $isMobileView = $mobileContext->shouldDisplayMobileView();
45        }
46        return $isMobileView;
47    }
48
49    /**
50     * @param User $user
51     * @param Title|null $title
52     * @param string[]|null $sectionTranslationTargetLanguages
53     * @return bool
54     */
55    private function isCXEntrypointEnabled(
56        User $user,
57        ?Title $title,
58        ?array $sectionTranslationTargetLanguages
59    ): bool {
60        if ( $this->preferenceHelper->isCXEntrypointDisabled( $user ) ) {
61            return false;
62        }
63
64        // the entrypoint should not be loaded, if the page is not an article (main namespace)
65        if ( !$title || !$title->isContentPage() ) {
66            return false;
67        }
68
69        return (bool)$sectionTranslationTargetLanguages;
70    }
71
72    public function onBeforePageDisplay( $out, $skin ): void {
73        $title = $out->getTitle();
74        $user = $out->getUser();
75
76        if ( !self::isMobileView() ) {
77            return;
78        }
79
80        $sectionTranslationTargetLanguages = $out->getConfig()->get( 'SectionTranslationTargetLanguages' );
81        $isCXEntrypointEnabled = $this->isCXEntrypointEnabled(
82            $user,
83            $title,
84            $sectionTranslationTargetLanguages
85        );
86
87        $mintEntrypointLanguages = $out->getConfig()->get(
88            'AutomaticTranslationLanguageSearcherEntrypointEnabledLanguages'
89        );
90
91        if ( $isCXEntrypointEnabled || $mintEntrypointLanguages ) {
92            $out->addModules( 'ext.cx.entrypoints.languagesearcher.init' );
93            $out->addJsConfigVars( 'wgSectionTranslationTargetLanguages', $sectionTranslationTargetLanguages );
94            $out->addJsConfigVars( 'isLanguageSearcherCXEntrypointEnabled', $isCXEntrypointEnabled );
95            $out->addJsConfigVars( 'mintEntrypointLanguages', $mintEntrypointLanguages );
96        }
97    }
98}