MediaWiki master
DateFormatterConfig.php
Go to the documentation of this file.
1<?php
2
4
10
23 public static function getData( Context $context, Config $config ) {
24 $lang = MediaWikiServices::getInstance()->getLanguageFactory()
25 ->getLanguage( $context->getLanguage() );
26 return self::getDataForLang( $lang, $config );
27 }
28
37 public static function getDataForLang( Language $lang, Config $config ) {
38 $locales = [ $lang->getHtmlCode() ];
39 $fallbacks = $lang->getFallbackLanguages();
40 foreach ( $fallbacks as $code ) {
41 $locales[] = LanguageCode::bcp47( $code );
42 }
43
44 // Discover which fields are required
45 $formats = $lang->getJsDateFormats();
46 $haveField = [];
47 foreach ( $formats as $format ) {
48 $pattern = $format['pattern'] ?? '';
49 foreach ( [ 'mwMonth', 'mwMonthGen', 'mwMonthAbbrev' ] as $field ) {
50 if ( str_contains( $pattern, "{$field}" ) ) {
51 $haveField[$field] = true;
52 }
53 }
54 }
55
56 // Include only the required month data
57 if ( $haveField ) {
58 $months = [ [] ];
59 for ( $i = 1; $i <= 12; $i++ ) {
60 $data = [
61 isset( $haveField['mwMonth'] ) ? $lang->getMonthName( $i ) : '',
62 isset( $haveField['mwMonthGen'] ) ? $lang->getMonthNameGen( $i ) : '',
63 isset( $haveField['mwMonthAbbrev'] ) ? $lang->getMonthAbbreviation( $i ) : ''
64 ];
65 // Trim the end of the array
66 while ( end( $data ) === '' ) {
67 unset( $data[ array_key_last( $data ) ] );
68 }
69 $months[] = $data;
70 }
71 } else {
72 $months = [];
73 }
74
75 return [
76 'locales' => $locales,
77 'formats' => $formats,
78 'defaultStyle' => $lang->getDefaultDateFormat(),
80 'localOffset' => (int)$config->get( MainConfigNames::LocalTZoffset ),
81 'months' => $months
82 ];
83 }
84}
Methods for dealing with language codes.
Base class for language-specific code.
Definition Language.php:81
getHtmlCode()
Get the code in BCP 47 format which we can use inside html lang="" tags.
A class containing constants representing the names of configuration variables.
const Localtimezone
Name constant for the Localtimezone setting, for use with Config::get()
const LocalTZoffset
Name constant for the LocalTZoffset setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Context object that contains information about the state of a specific ResourceLoader web request.
Definition Context.php:46
static getDataForLang(Language $lang, Config $config)
Get configuration data for DateFormatter given parameters.
static getData(Context $context, Config $config)
Callback for mediawiki.DateFormatter/config.json.
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition Module.php:47
Interface for configuration instances.
Definition Config.php:32
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".