forked from wikimedia/mediawiki-extensions-LanguageSelector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageSelectorHooks.php
299 lines (249 loc) · 9.16 KB
/
LanguageSelectorHooks.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
use MediaWiki\MediaWikiServices;
class LanguageSelectorHooks {
public static function onRegistration() {
global $wgLanguageSelectorDetectLanguage, $wgLanguageSelectorLocation, $wgParserOutputHooks;
define( 'LANGUAGE_SELECTOR_USE_CONTENT_LANG', 0 ); # no detection
define( 'LANGUAGE_SELECTOR_PREFER_CONTENT_LANG', 1 ); # use content language if accepted by the client
define( 'LANGUAGE_SELECTOR_PREFER_CLIENT_LANG', 2 ); # use language most preferred by the client
/**
* Language detection mode for anonymous visitors.
* Possible values:
* * LANGUAGE_SELECTOR_USE_CONTENT_LANG - use the $wgLanguageCode setting (default content language)
* * LANGUAGE_SELECTOR_PREFER_CONTENT_LANG - use the $wgLanguageCode setting, if accepted by the client
* * LANGUAGE_SELECTOR_PREFER_CLIENT_LANG - use the client's preferred language, if in $wgLanguageSelectorLanguages
*/
$wgLanguageSelectorDetectLanguage = LANGUAGE_SELECTOR_PREFER_CLIENT_LANG;
define( 'LANGUAGE_SELECTOR_MANUAL', 0 ); # don't place anywhere
define( 'LANGUAGE_SELECTOR_AT_TOP_OF_TEXT', 1 ); # put at the top of page content
define( 'LANGUAGE_SELECTOR_IN_TOOLBOX', 2 ); # put into toolbox
define( 'LANGUAGE_SELECTOR_AS_PORTLET', 3 ); # as portlet
define( 'LANGUAGE_SELECTOR_INTO_SITENOTICE', 11 ); # put after sitenotice text
define( 'LANGUAGE_SELECTOR_INTO_TITLE', 12 ); # put after title text
define( 'LANGUAGE_SELECTOR_INTO_SUBTITLE', 13 ); # put after subtitle text
define( 'LANGUAGE_SELECTOR_INTO_CATLINKS', 14 ); # put after catlinks text
$wgLanguageSelectorLocation = LANGUAGE_SELECTOR_AT_TOP_OF_TEXT;
$wgParserOutputHooks['languageselector'] = 'LanguageSelectorHooks::addJavascript';
}
public static function extension() {
// We'll probably be beaten to this by the call in onUserGetLanguageObject(),
// but just in case, call this to make sure the global is properly initialised
self::getLanguageSelectorLanguages();
}
/**
* @param Parser $parser
*/
public static function onParserFirstCallInit( Parser $parser ) {
$parser->setHook( 'languageselector', [ __CLASS__, 'languageSelectorTag' ] );
}
/**
* @return array
*/
public static function getLanguageSelectorLanguages() {
global $wgLanguageSelectorLanguages, $wgLanguageSelectorShowAll;
if ( $wgLanguageSelectorLanguages === null ) {
$wgLanguageSelectorLanguages = array_keys( Language::fetchLanguageNames(
null,
$wgLanguageSelectorShowAll === true ? 'mw' : 'mwfile'
) );
sort( $wgLanguageSelectorLanguages );
}
return $wgLanguageSelectorLanguages;
}
/**
* @param User $user
* @param string &$code
*/
public static function onUserGetLanguageObject( $user, &$code ) {
global $wgLanguageSelectorDetectLanguage,
$wgCommandLineMode, $wgRequest;
if ( $wgCommandLineMode ) {
return;
}
$setlang = $wgRequest->getRawVal( 'setlang' );
if ( $setlang && !in_array( $setlang, self::getLanguageSelectorLanguages() ) ) {
$setlang = null; // ignore invalid
}
if ( $setlang ) {
$wgRequest->response()->setcookie( 'LanguageSelectorLanguage', $setlang );
$requestedLanguage = $setlang;
} else {
$requestedLanguage = $wgRequest->getCookie( 'LanguageSelectorLanguage' );
}
if ( $setlang && !$user->isAnon() ) {
$userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
if ( $setlang != $userOptionsManager->getOption( $user, 'language' ) ) {
$userOptionsManager->setOption( $user, 'language', $requestedLanguage );
$userOptionsManager->saveOptions( $user );
$code = $requestedLanguage;
}
}
if ( !$wgRequest->getRawVal( 'uselang' ) && $user->isAnon() ) {
if ( $wgLanguageSelectorDetectLanguage != LANGUAGE_SELECTOR_USE_CONTENT_LANG ) {
if ( $requestedLanguage ) {
$code = $requestedLanguage;
} else {
$languages = $wgRequest->getAcceptLang();
// see if the content language is accepted by the client.
if ( $wgLanguageSelectorDetectLanguage != LANGUAGE_SELECTOR_PREFER_CONTENT_LANG
|| !array_key_exists( MediaWikiServices::getInstance()->getContentLanguage()->getCode(), $languages ) ) {
$supported = self::getLanguageSelectorLanguages();
// look for a language that is acceptable to the client
// and known to the wiki.
foreach ( $languages as $reqCode => $q ) {
if ( in_array( $reqCode, $supported ) ) {
$code = $reqCode;
break;
}
}
// Apparently Safari sends stupid things like "de-de" only.
// Try again with stripped codes.
foreach ( $languages as $reqCode => $q ) {
$stupidPHP = explode( '-', $reqCode, 2 );
$bareCode = array_shift( $stupidPHP );
if ( in_array( $bareCode, $supported ) ) {
$code = $bareCode;
break;
}
}
}
}
}
}
}
/**
* @param OutputPage $out
* @param array &$cookies
*/
public static function onGetCacheVaryCookies( $out, &$cookies ) {
global $wgCookiePrefix;
$cookies[] = $wgCookiePrefix . 'LanguageSelectorLanguage';
}
/**
* @param string $input
* @param array $args
* @param Parser $parser
* @return string
*/
public static function languageSelectorTag( $input, $args, $parser ) {
$style = $args['style'] ?? null;
$class = $args['class'] ?? null;
$selectorstyle = $args['selectorstyle'] ?? null;
$buttonstyle = $args['buttonstyle'] ?? null;
$showcode = $args['showcode'] ?? null;
if ( $style ) {
$style = htmlspecialchars( $style, ENT_QUOTES );
}
if ( $class ) {
$class = htmlspecialchars( $class, ENT_QUOTES );
}
if ( $selectorstyle ) {
$selectorstyle = htmlspecialchars( $selectorstyle, ENT_QUOTES );
}
if ( $buttonstyle ) {
$buttonstyle = htmlspecialchars( $buttonstyle, ENT_QUOTES );
}
if ( $showcode ) {
$showcode = strtolower( $showcode );
if ( $showcode == 'true' || $showcode == 'yes' || $showcode == 'on' ) {
$showcode = true;
} elseif ( $showcode == 'false' || $showcode == 'no' || $showcode == 'off' ) {
$showcode = false;
} else {
$showcode = null;
}
} else {
$showcode = null;
}
# So that this also works with parser cache
$parser->getOutput()->addOutputHook( 'languageselector' );
return self::languageSelectorHTML( $parser->getTitle(), $style, $class, $selectorstyle, $buttonstyle, $showcode );
}
/**
* @param User $user
* @param bool $autocreated
*/
public static function onLocalUserCreated( $user, $autocreated ) {
$context = RequestContext::getMain();
// inherit language;
// if the context user is the created user this means remembering what the user selected
// otherwise, it would mean inheriting the language from the user creating the account.
if ( $context->getUser() === $user ) {
$userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
$userOptionsManager->setOption( $user, 'language', $context->getLanguage()->getCode() );
}
}
/**
* @param OutputPage $outputPage
* @param ParserOutput $parserOutput
* @param mixed $data
*/
public static function addJavascript( $outputPage, $parserOutput, $data ) {
$outputPage->addModules( 'ext.languageSelector' );
}
/**
* @param Title $title
* @param null|string $style
* @param null|string $class
* @param null|string $selectorstyle
* @param null|string $buttonstyle
* @param null|bool $showCode
* @return string
*/
public static function languageSelectorHTML( Title $title, $style = null, $class = null, $selectorstyle = null, $buttonstyle = null, $showCode = null ) {
global $wgLang, $wgScript, $wgLanguageSelectorShowCode;
if ( $showCode === null ) {
$showCode = $wgLanguageSelectorShowCode;
}
static $id = 0;
$id += 1;
$code = $wgLang->getCode();
$html = '';
$html .= Xml::openElement( 'span', [
'id' => 'languageselector-box-' . $id,
'class' => 'languageselector ' . $class,
'style' => $style
] );
$html .= Xml::openElement( 'form', [
'name' => 'languageselector-form-' . $id,
'id' => 'languageselector-form-' . $id,
'method' => 'get',
'action' => $wgScript,
'style' => 'display:inline;'
] );
$html .= Html::hidden( 'title', $title->getPrefixedDBkey() );
$html .= Xml::openElement( 'select', [
'name' => 'setlang',
'id' => 'languageselector-select-' . $id,
'style' => $selectorstyle
] );
foreach ( self::getLanguageSelectorLanguages() as $ln ) {
$name = Language::fetchLanguageName( $ln );
if ( $showCode ) {
$name = LanguageCode::bcp47( $ln ) . ' - ' . $name;
}
$html .= Xml::option( $name, $ln, $ln == $code );
}
$html .= Xml::closeElement( 'select' );
$html .= Xml::submitButton( wfMessage( 'languageselector-setlang' )->text(),
[ 'id' => 'languageselector-commit-' . $id, 'style' => $buttonstyle ] );
$html .= Xml::closeElement( 'form' );
$html .= Xml::closeElement( 'span' );
return $html;
}
public static function onMenuSidebarAfterBuild( Skin $skin, &$bar ) {
$lines = [];
$code = $skin->getLanguage()->getCode();
foreach ( self::getLanguageSelectorLanguages() as $ln ) {
$lines[] = [
$href = $skin->getTitle()->getFullURL( 'setlang=' . $ln ),
'text' => Language::fetchLanguageName( $ln ),
'href' => $href,
'id' => 'n-languageselector',
'active' => ( $code === $ln ),
];
}
$bar['languageselector'] = $lines;
return true;
}
}