/
Hooks.php
240 lines (217 loc) · 6.45 KB
/
Hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace CookieWarning;
use Config;
use ConfigException;
use Html;
use MediaWiki;
use MediaWiki\Hook\BeforeInitializeHook;
use MediaWiki\Hook\BeforePageDisplayHook;
use MediaWiki\Hook\SkinAfterContentHook;
use MediaWiki\MediaWikiServices;
use MediaWiki\Preferences\Hook\GetPreferencesHook;
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
use MWException;
use OOUI\ButtonInputWidget;
use OOUI\ButtonWidget;
use OOUI\HorizontalLayout;
use OutputPage;
use Skin;
use Title;
use User;
use WebRequest;
class Hooks implements
SkinAfterContentHook,
GetPreferencesHook,
BeforeInitializeHook,
BeforePageDisplayHook,
ResourceLoaderGetConfigVarsHook
{
/**
* BeforeInitialize hook handler.
*
* If the disablecookiewarning POST data is send, disables the cookiewarning bar with a
* cookie or a user preference, if the user is logged in.
*
* @param Title $title
* @param null $unused
* @param OutputPage $output
* @param User $user
* @param WebRequest $request
* @param MediaWiki $mediawiki
* @throws MWException
*/
public function onBeforeInitialize( $title, $unused, $output, $user, $request, $mediawiki ) {
if ( !$request->wasPosted() || !$request->getVal( 'disablecookiewarning' ) ) {
return;
}
if ( $user->isRegistered() ) {
$userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
$userOptionsManager->setOption( $user, 'cookiewarning_dismissed', 1 );
$userOptionsManager->saveOptions( $user );
} else {
$request->response()->setCookie( 'cookiewarning_dismissed', true );
}
$output->redirect( $request->getRequestURL() );
}
/**
* SkinAfterContent hook handler.
*
* Adds the CookieWarning information bar to the output html.
*
* @param string &$data
* @param Skin $skin
*
* @throws MWException
*/
public function onSkinAfterContent( &$data, $skin ) {
/** @var Decisions $cookieWarningDecisions */
$cookieWarningDecisions = MediaWikiServices::getInstance()
->getService( 'CookieWarning.Decisions' );
if ( !$cookieWarningDecisions->shouldShowCookieWarning( $skin->getContext() ) ) {
return;
}
$data .= self::generateElements( $skin );
}
/**
* Generates the elements for the banner.
*
* @param Skin $skin
* @return string|null The html for cookie notice.
*/
private static function generateElements( Skin $skin ): ?string {
$moreLink = self::getMoreLink();
$buttons = [];
if ( $moreLink ) {
$buttons[] = new ButtonWidget( [
'href' => $moreLink,
'label' => $skin->msg( 'cookiewarning-moreinfo-label' )->text(),
'flags' => [ 'progressive' ]
] );
}
$buttons[] = new ButtonInputWidget( [
'type' => 'submit',
'label' => $skin->msg( 'cookiewarning-ok-label' )->text(),
'name' => 'disablecookiewarning',
'value' => 'OK',
'flags' => [ 'primary', 'progressive' ]
] );
$form = Html::rawElement(
'form',
[ 'method' => 'POST' ],
new HorizontalLayout( [ 'items' => $buttons ] )
);
return Html::openElement(
'div',
[ 'class' => 'mw-cookiewarning-container' ]
) .
Html::openElement(
'div',
[ 'class' => 'mw-cookiewarning-text' ]
) .
Html::element(
'span',
[],
$skin->msg( 'cookiewarning-info' )->text()
) .
Html::closeElement( 'div' ) .
$form .
Html::closeElement( 'div' );
}
/**
* Returns the target for the "More information" link of the cookie warning bar, if one is set.
* The link can be set by either (checked in this order):
* - the configuration variable $wgCookieWarningMoreUrl
* - the interface message MediaWiki:Cookiewarning-more-link
* - the interface message MediaWiki:Cookie-policy-link (bc T145781)
*
* @return string|null The url or null if none set
* @throws ConfigException
*/
private static function getMoreLink(): ?string {
$conf = self::getConfig();
if ( $conf->get( 'CookieWarningMoreUrl' ) ) {
return $conf->get( 'CookieWarningMoreUrl' );
}
$cookieWarningMessage = wfMessage( 'cookiewarning-more-link' );
if ( $cookieWarningMessage->exists() && !$cookieWarningMessage->isDisabled() ) {
return $cookieWarningMessage->text();
}
$cookiePolicyMessage = wfMessage( 'cookie-policy-link' );
if ( $cookiePolicyMessage->exists() && !$cookiePolicyMessage->isDisabled() ) {
return $cookiePolicyMessage->text();
}
return null;
}
/**
* BeforePageDisplay hook handler.
*
* Adds the required style and JS module, if cookiewarning is enabled.
*
* @param OutputPage $out
* @param Skin $skin
* @throws ConfigException
* @throws MWException
*/
public function onBeforePageDisplay( $out, $skin ): void {
/** @var Decisions $cookieWarningDecisions */
$cookieWarningDecisions = MediaWikiServices::getInstance()
->getService( 'CookieWarning.Decisions' );
if ( !$cookieWarningDecisions->shouldShowCookieWarning( $out->getContext() ) ) {
return;
}
$modules = [ 'ext.CookieWarning' ];
$moduleStyles = [ 'ext.CookieWarning.styles' ];
if ( $cookieWarningDecisions->shouldAddResourceLoaderComponents() ) {
$modules[] = 'ext.CookieWarning.geolocation';
$moduleStyles[] = 'ext.CookieWarning.geolocation.styles';
}
$out->addModules( $modules );
$out->addModuleStyles( $moduleStyles );
$out->enableOOUI();
}
/**
* ResourceLoaderGetConfigVars hook handler.
*
* @param array &$vars
* @param string $skin
* @param Config $config
*
* @throws ConfigException
*/
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
/** @var Decisions $cookieWarningDecisions */
$cookieWarningDecisions = MediaWikiServices::getInstance()
->getService( 'CookieWarning.Decisions' );
$conf = self::getConfig();
if ( $cookieWarningDecisions->shouldAddResourceLoaderComponents() ) {
$vars += [
'wgCookieWarningGeoIPServiceURL' => $conf->get( 'CookieWarningGeoIPServiceURL' ),
'wgCookieWarningForCountryCodes' => $conf->get( 'CookieWarningForCountryCodes' ),
];
}
}
/**
* Returns the Config object for the CookieWarning extension.
*
* @return Config
*/
private static function getConfig(): Config {
return MediaWikiServices::getInstance()->getService( 'CookieWarning.Config' );
}
/**
* GetPreferences hook handler
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
*
* @param User $user
* @param array &$defaultPreferences
* @return bool
*/
public function onGetPreferences( $user, &$defaultPreferences ): bool {
$defaultPreferences['cookiewarning_dismissed'] = [
'type' => 'api',
'default' => '0',
];
return true;
}
}