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