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