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