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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
<?php
/**
* Contains main code.
*
* @file
* @author Robert Leverington
* @author Robin Pepermans
* @author Niklas Laxström
* @author Brian Wolff
* @author Purodha Blissenbach
* @author Sam Reed
* @author Siebrand Mazeland
* @license GPL-2.0-or-later
*/
use MediaWiki\Babel\BabelBox\LanguageBabelBox;
use MediaWiki\Babel\BabelBox\NotBabelBox;
use MediaWiki\Babel\BabelBox\NullBabelBox;
use MediaWiki\MediaWikiServices;
/**
* Main class for the Babel extension.
*/
class Babel {
/**
* @var Title
*/
protected static $title;
/**
* Render the Babel tower.
*
* @param Parser $parser
* @param string ...$parameters
* @return string Babel tower.
*/
public static function Render( Parser $parser, ...$parameters ) {
global $wgBabelUseUserLanguage;
self::$title = $parser->getTitle();
self::mTemplateLinkBatch( $parameters );
$parser->getOutput()->addModuleStyles( 'ext.babel' );
$content = self::mGenerateContentTower( $parser, $parameters );
if ( preg_match( '/^plain\s*=\s*\S/', reset( $parameters ) ) ) {
return $content;
}
if ( $wgBabelUseUserLanguage ) {
$uiLang = $parser->getOptions()->getUserLangObj();
} else {
$uiLang = self::$title->getPageLanguage();
}
$top = wfMessage( 'babel', self::$title->getDBkey() )->inLanguage( $uiLang );
if ( $top->isDisabled() ) {
$top = '';
} else {
$top = $top->text();
$url = wfMessage( 'babel-url' )->inContentLanguage();
if ( !$url->isDisabled() ) {
$top = '[[' . $url->text() . '|' . $top . ']]';
}
$top = '! class="mw-babel-header" | ' . $top;
}
$footer = wfMessage( 'babel-footer', self::$title->getDBkey() )->inLanguage( $uiLang );
$url = wfMessage( 'babel-footer-url' )->inContentLanguage();
$showfooter = '';
if ( !$footer->isDisabled() && !$url->isDisabled() ) {
$showfooter = '! class="mw-babel-footer" | [[' .
$url->text() . '|' . $footer->text() . ']]';
}
$tower = <<<EOT
{|class="mw-babel-wrapper"
$top
|-
| $content
|-
$showfooter
|}
EOT;
return $tower;
}
/**
* @param Parser $parser
* @param string[] $parameters
*
* @return string Wikitext
*/
private static function mGenerateContentTower( Parser $parser, array $parameters ) {
$content = '';
$templateParameters = []; // collects name=value parameters to be passed to wiki templates.
foreach ( $parameters as $name ) {
if ( strpos( $name, '=' ) !== false ) {
$templateParameters[] = $name;
continue;
}
$content .= self::mGenerateContent( $parser, $name, $templateParameters );
}
return $content;
}
private static function setExtensionData( ParserOutput $parserOutput, $code, $level ) {
$data = $parserOutput->getExtensionData( 'babel' ) ?: [];
$data[ BabelLanguageCodes::getCategoryCode( $code ) ] = $level;
$parserOutput->setExtensionData( 'babel', $data );
}
/**
* @param Parser $parser
* @param string $name
* @param string[] $templateParameters
*
* @return string Wikitext
*/
private static function mGenerateContent( Parser $parser, $name, array $templateParameters ) {
$createCategories = !$parser->getOptions()->getIsPreview();
$components = self::mParseParameter( $name );
$template = wfMessage( 'babel-template', $name )->inContentLanguage()->text();
$parserOutput = $parser->getOutput();
if ( $name === '' ) {
$box = new NullBabelBox();
} elseif ( $components !== false ) {
// Valid parameter syntax (with lowercase language code), babel box
$box = new LanguageBabelBox(
self::$title,
$components['code'],
$components['level'],
$createCategories
);
self::setExtensionData( $parserOutput, $components['code'], $components['level'] );
} elseif ( self::mPageExists( $template ) ) {
// Check for an existing template
$templateParameters[0] = $template;
$template = implode( '|', $templateParameters );
$box = new NotBabelBox(
self::$title->getPageLanguage()->getDir(),
$parser->replaceVariables( "{{{$template}}}" )
);
} elseif ( self::mValidTitle( $template ) ) {
// Non-existing page, so try again as a babel box,
// with converting the code to lowercase
$components2 = self::mParseParameter( $name, /* code to lowercase */
true );
if ( $components2 !== false ) {
$box = new LanguageBabelBox(
self::$title,
$components2['code'],
$components2['level'],
$createCategories
);
self::setExtensionData( $parserOutput,
$components2['code'], $components2['level'] );
} else {
// Non-existent page and invalid parameter syntax, red link.
$box = new NotBabelBox(
self::$title->getPageLanguage()->getDir(),
'[[' . $template . ']]'
);
}
} else {
// Invalid title, output raw.
$box = new NotBabelBox(
self::$title->getPageLanguage()->getDir(),
$template
);
}
foreach ( $box->getCategories() as $cat => $sortKey ) {
if ( $sortKey === false ) {
$sortKey = $parser->getDefaultSort();
}
$parserOutput->addCategory( $cat, $sortKey );
}
return $box->render();
}
/**
* Performs a link batch on a series of templates.
*
* @param string[] $parameters Templates to perform the link batch on.
*/
protected static function mTemplateLinkBatch( array $parameters ) {
$titles = [];
foreach ( $parameters as $name ) {
$title = Title::newFromText( wfMessage( 'babel-template', $name )->inContentLanguage()->text() );
if ( is_object( $title ) ) {
$titles[] = $title;
}
}
$batch = new LinkBatch( $titles );
$batch->setCaller( __METHOD__ );
$batch->execute();
}
/**
* Identify whether or not a page exists.
*
* @param string $name Name of the page to check.
* @return bool Indication of whether the page exists.
*/
protected static function mPageExists( $name ) {
$titleObj = Title::newFromText( $name );
return ( is_object( $titleObj ) && $titleObj->exists() );
}
/**
* Identify whether or not the passed string would make a valid page name.
*
* @param string $name Name of page to check.
* @return bool Indication of whether or not the title is valid.
*/
protected static function mValidTitle( $name ) {
$titleObj = Title::newFromText( $name );
return is_object( $titleObj );
}
/**
* Parse a parameter, getting a language code and level.
*
* @param string $parameter Parameter.
* @param bool $strtolower Whether to convert the language code to lowercase
* @return array|bool [ 'code' => xx, 'level' => xx ] false on failure
*/
protected static function mParseParameter( $parameter, $strtolower = false ) {
global $wgBabelDefaultLevel, $wgBabelCategoryNames;
$return = [];
$babelcode = $strtolower ? strtolower( $parameter ) : $parameter;
// Try treating the parameter as a language code (for default level).
$code = BabelLanguageCodes::getCode( $babelcode );
if ( $code !== false ) {
$return['code'] = $code;
$return['level'] = $wgBabelDefaultLevel;
return $return;
}
// Try splitting the parameter in to language and level, split on last hyphen.
$lastSplit = strrpos( $parameter, '-' );
if ( $lastSplit === false ) {
return false;
}
$code = substr( $parameter, 0, $lastSplit );
$level = substr( $parameter, $lastSplit + 1 );
$babelcode = $strtolower ? strtolower( $code ) : $code;
// Validate code.
$return['code'] = BabelLanguageCodes::getCode( $babelcode );
if ( $return['code'] === false ) {
return false;
}
// Validate level.
$level = strtoupper( $level );
if ( !isset( $wgBabelCategoryNames[$level] ) ) {
return false;
}
$return['level'] = $level;
return $return;
}
/**
* Gets the language information a user has set up with Babel.
* This function gets the actual info directly from categories
* or database. For performance, it is recommended to use
* getCachedUserLanguageInfo instead.
*
* @param User $user
* @return string[] [ language code => level ]
*/
public static function getUserLanguageInfo( User $user ) {
$userLanguageInfo = self::getUserLanguagesDB( $user );
ksort( $userLanguageInfo );
return $userLanguageInfo;
}
/**
* Gets the language information a user has set up with Babel,
* from the cache. It's recommended to use this when this will
* be called frequently.
*
* @param User $user
* @return string[] [ language code => level ]
*
* @since Version 1.10.0
*/
public static function getCachedUserLanguageInfo( User $user ) {
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$userId = $user->getId();
$key = $cache->makeKey( 'babel-local-languages', $userId );
$checkKeys = [ $key ];
if ( method_exists( MediaWikiServices::class, 'getCentralIdLookupFactory' ) ) {
// MW1.37+
$centralId = MediaWikiServices::getInstance()->getCentralIdLookupFactory()
->getLookup()->centralIdFromLocalUser( $user );
} else {
$centralId = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
}
if ( $centralId ) {
$checkKeys[] = $cache->makeGlobalKey( 'babel-central-languages', $centralId );
}
$cachedUserLanguageInfo = $cache->getWithSetCallback(
$key,
$cache::TTL_MINUTE * 30,
function ( $oldValue, &$ttl, array &$setOpts ) use ( $userId, $user ) {
wfDebug( "Babel: cache miss for user $userId\n" );
return self::getUserLanguageInfo( $user );
},
[
'checkKeys' => $checkKeys,
]
);
return $cachedUserLanguageInfo;
}
/**
* Gets only the languages codes list out of the user language info.
*
* @param string[] $languageInfo [ language code => level ], the return value of
* getUserLanguageInfo.
* @param string $level Minimal level as given in $wgBabelCategoryNames
* @return string[] List of language codes
*
* @since Version 1.10.0
*/
private static function getLanguages( $languageInfo, $level ) {
if ( !$languageInfo ) {
return [];
}
if ( $level !== null ) {
$level = (string)$level;
// filter down the set, note that this uses a text sort!
$languageInfo = array_filter(
$languageInfo,
static function ( $value ) use ( $level ) {
return ( strcmp( $value, $level ) >= 0 );
}
);
// sort and retain keys
uasort(
$languageInfo,
static function ( $a, $b ) {
return -strcmp( $a, $b );
}
);
}
return array_keys( $languageInfo );
}
/**
* Gets the cached list of languages a user has set up with Babel.
*
* @param User $user
* @param string|null $level Minimal level as given in $wgBabelCategoryNames
* @return string[] List of language codes
*
* @since Version 1.10.0
*/
public static function getCachedUserLanguages( User $user, $level = null ) {
return self::getLanguages( self::getCachedUserLanguageInfo( $user ), $level );
}
/**
* Gets the list of languages a user has set up with Babel.
* For performance it is recommended to use getCachedUserLanguages.
*
* @param User $user
* @param string|null $level Minimal level as given in $wgBabelCategoryNames
* @return string[] List of language codes
*
* @since Version 1.9.0
*/
public static function getUserLanguages( User $user, $level = null ) {
return self::getLanguages( self::getUserLanguageInfo( $user ), $level );
}
private static function getUserLanguagesDB( User $user ) {
global $wgBabelCentralDb;
$babelDB = new MediaWiki\Babel\Database();
$result = $babelDB->getForUser( $user->getId() );
/** If local data or no central source, return */
if ( $result || !$wgBabelCentralDb ) {
return $result;
}
if ( $wgBabelCentralDb === wfWikiID() ) {
// We are the central wiki, so no fallback we can do
return [];
}
if ( method_exists( MediaWikiServices::class, 'getCentralIdLookupFactory' ) ) {
// MW1.37+
$lookup = MediaWikiServices::getInstance()->getCentralIdLookupFactory()->getLookup();
} else {
$lookup = CentralIdLookup::factory();
}
if ( !$lookup->isAttached( $user )
|| !$lookup->isAttached( $user, $wgBabelCentralDb )
) {
return [];
}
return $babelDB->getForRemoteUser( $wgBabelCentralDb, $user->getName() );
}
}
|