Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.00% covered (danger)
25.00%
9 / 36
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MobileFrontendSkinHooks
25.00% covered (danger)
25.00%
9 / 36
33.33% covered (danger)
33.33%
1 / 3
21.19
0.00% covered (danger)
0.00%
0 / 1
 getTermsLink
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getDesktopViewLink
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 getMobileViewLink
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3use MediaWiki\Html\Html;
4use MediaWiki\MediaWikiServices;
5
6class MobileFrontendSkinHooks {
7    /**
8     * Returns HTML of terms of use link or null if it shouldn't be displayed
9     * Note: This is called by a hook in the WikimediaMessages extension.
10     *
11     * @param MessageLocalizer $localizer
12     * @return null|string
13     */
14    public static function getTermsLink( MessageLocalizer $localizer ) {
15        $urlMsg = $localizer->msg( 'mobile-frontend-terms-url' )->inContentLanguage();
16        if ( $urlMsg->isDisabled() ) {
17            return null;
18        }
19        $url = $urlMsg->plain();
20
21        return Html::element(
22            'a',
23            [ 'href' => Skin::makeInternalOrExternalUrl( $url ) ],
24            $localizer->msg( 'mobile-frontend-terms-text' )->text()
25        );
26    }
27
28    /**
29     * @param Skin $skin
30     * @param MobileContext $context
31     * @return string representing the desktop link.
32     */
33    public static function getDesktopViewLink( Skin $skin, MobileContext $context ) {
34        $url = $skin->getOutput()->getProperty( 'desktopUrl' );
35        $req = $skin->getRequest();
36        if ( $url ) {
37            $url = wfAppendQuery( $url, 'mobileaction=toggle_view_desktop' );
38        } else {
39            $title = $skin->getTitle() ?? Title::newMainPage();
40            $url = $title->getLocalURL(
41                $req->appendQueryValue( 'mobileaction', 'toggle_view_desktop' )
42            );
43        }
44        $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
45        $desktopUrl = $context->getDesktopUrl( $urlUtils->expand( $url, PROTO_RELATIVE ) ?? '' );
46
47        $desktop = $context->msg( 'mobile-frontend-view-desktop' )->text();
48        return Html::element( 'a',
49            [ 'id' => 'mw-mf-display-toggle', 'href' => $desktopUrl,
50                'data-event-name' => 'switch_to_desktop' ], $desktop );
51    }
52
53    /**
54     * @param Skin $skin
55     * @param MobileContext $context
56     * @return string representing the mobile link.
57     */
58    public static function getMobileViewLink( Skin $skin, MobileContext $context ) {
59        $req = $skin->getRequest();
60        $args = $req->getQueryValues();
61        // avoid title being set twice
62        unset( $args['title'], $args['useformat'] );
63        $args['mobileaction'] = 'toggle_view_mobile';
64        $title = $skin->getTitle();
65        // Title could be null
66        // If no title, there is a problem with context. Possibly inside a test.
67        if ( !$title ) {
68            return '';
69        }
70        $mobileViewUrl = $title->getFullURL( $args );
71        $mobileViewUrl = $context->getMobileUrl( $mobileViewUrl );
72
73        return Html::element( 'a',
74            [ 'href' => $mobileViewUrl, 'class' => 'noprint stopMobileRedirectToggle' ],
75            $context->msg( 'mobile-frontend-view' )->text()
76        );
77    }
78}