Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
Hooks | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
72 | |
0.00% |
0 / 1 |
onSiteNoticeAfter | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\DismissableSiteNotice; |
4 | |
5 | use MediaWiki\Hook\SiteNoticeAfterHook; |
6 | use MediaWiki\Html\Html; |
7 | use MediaWiki\Parser\Sanitizer; |
8 | use MediaWiki\Skin\Skin; |
9 | |
10 | class Hooks implements SiteNoticeAfterHook { |
11 | |
12 | /** |
13 | * @param string &$notice |
14 | * @param Skin $skin |
15 | * @suppress SecurityCheck-DoubleEscaped |
16 | */ |
17 | public function onSiteNoticeAfter( &$notice, $skin ) { |
18 | global $wgMajorSiteNoticeID, $wgDismissableSiteNoticeForAnons; |
19 | if ( method_exists( $skin, 'getVersion' ) ) { |
20 | // does the skin support versioning and if so does it provide dismissable site notices? |
21 | if ( $skin->getVersion() === 2 ) { |
22 | return; |
23 | } |
24 | } |
25 | |
26 | if ( trim( Sanitizer::removeHTMLcomments( $notice ) ) === '' ) { |
27 | return; |
28 | } |
29 | |
30 | // Dismissal for anons is configurable |
31 | if ( $wgDismissableSiteNoticeForAnons || $skin->getUser()->isRegistered() ) { |
32 | // Cookie value consists of two parts |
33 | $major = (int)$wgMajorSiteNoticeID; |
34 | $minor = (int)$skin->msg( 'sitenotice_id' )->inContentLanguage()->text(); |
35 | |
36 | $out = $skin->getOutput(); |
37 | $out->addModuleStyles( 'ext.dismissableSiteNotice.styles' ); |
38 | $out->addModules( 'ext.dismissableSiteNotice' ); |
39 | $out->addJsConfigVars( 'wgSiteNoticeId', "$major.$minor" ); |
40 | |
41 | $notice = Html::rawElement( 'div', [ 'class' => 'mw-dismissable-notice' ], |
42 | Html::rawElement( 'div', [ 'class' => 'mw-dismissable-notice-close' ], |
43 | $skin->msg( 'sitenotice_close-brackets' ) |
44 | ->rawParams( |
45 | Html::element( |
46 | 'a', |
47 | [ 'tabindex' => '0', 'role' => 'button' ], |
48 | $skin->msg( 'sitenotice_close' )->text() |
49 | ) |
50 | ) |
51 | ->escaped() |
52 | ) . |
53 | Html::rawElement( 'div', [ 'class' => 'mw-dismissable-notice-body' ], $notice ) |
54 | ); |
55 | } |
56 | |
57 | if ( $skin->getUser()->isAnon() ) { |
58 | // If there's no nonce (false), pass null to Html::inlineScript |
59 | $nonce = $skin->getOutput()->getCSP()->getNonce() ?: null; |
60 | |
61 | // Hide the sitenotice from search engines (see bug T11209 comment 4) |
62 | // NOTE: Is this actually effective? |
63 | // NOTE: Avoid document.write (T125323) |
64 | // NOTE: Must be compatible with JavaScript in ancient Grade C browsers. |
65 | $jsWrapped = |
66 | '<div id="mw-dismissablenotice-anonplace"></div>' . |
67 | Html::inlineScript( |
68 | '(function(){' . |
69 | 'var node=document.getElementById("mw-dismissablenotice-anonplace");' . |
70 | 'if(node){' . |
71 | // Replace placeholder with parsed HTML from $notice. |
72 | // Setting outerHTML is supported in all Grade C browsers |
73 | // and gracefully fallsback to just setting a property. |
74 | // It is short for: |
75 | // - Create temporary element or document fragment |
76 | // - Set innerHTML. |
77 | // - Replace node with wrapper's child nodes. |
78 | 'node.outerHTML=' . Html::encodeJsVar( $notice ) . ';' . |
79 | '}' . |
80 | '}());', |
81 | $nonce |
82 | ); |
83 | $notice = $jsWrapped; |
84 | } |
85 | } |
86 | } |