16 private const FIELD_CLASS =
'mw-htmlform-timezone-field';
19 private $msgFormatter;
27 if ( isset( $params[
'options'] ) ) {
28 throw new InvalidArgumentException(
"Options should not be provided to " . __CLASS__ );
30 $params[
'placeholder-message'] ??=
'timezone-useoffset-placeholder';
31 $params[
'options'] = [];
32 parent::__construct( $params );
34 $langCode = $lang->getCode();
35 $this->msgFormatter = MediaWikiServices::getInstance()->getMessageFormatterFactory()
36 ->getTextFormatter( $langCode );
37 $this->mOptions = $this->getTimezoneOptions();
43 private function getTimezoneOptions(): array {
46 $localTZoffset = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LocalTZoffset );
47 $timeZoneList = $this->getTimeZoneList();
49 $timestamp = MWTimestamp::getLocalInstance();
51 if ( $localTZoffset === (
int)$timestamp->format(
'Z' ) / 60 ) {
52 $timezoneName = $timestamp->getTimezone()->getName();
54 if ( isset( $timeZoneList[$timezoneName] ) ) {
55 $timezoneName = $timeZoneList[$timezoneName][
'name'];
57 $server_tz_msg = $this->msgFormatter->format(
58 MessageValue::new(
'timezoneuseserverdefault', [ $timezoneName ] )
61 $tzstring = UserTimeCorrection::formatTimezoneOffset( $localTZoffset );
62 $server_tz_msg = $this->msgFormatter->format(
63 MessageValue::new(
'timezoneuseserverdefault', [ $tzstring ] )
66 $opt[$server_tz_msg] =
"System|$localTZoffset";
67 $opt[$this->msgFormatter->format( MessageValue::new(
'timezoneuseoffset' ) )] =
'other';
68 $opt[$this->msgFormatter->format( MessageValue::new(
'guesstimezone' ) )] =
'guess';
70 foreach ( $timeZoneList as $timeZoneInfo ) {
71 $region = $timeZoneInfo[
'region'];
72 if ( !isset( $opt[$region] ) ) {
75 $opt[$region][$timeZoneInfo[
'name']] = $timeZoneInfo[
'timecorrection'];
86 private function getTimeZoneList(): array {
87 $identifiers = DateTimeZone::listIdentifiers();
88 '@phan-var array|false $identifiers';
89 if ( $identifiers ===
false ) {
95 'Africa' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-africa' ) ),
96 'America' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-america' ) ),
97 'Antarctica' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-antarctica' ) ),
98 'Arctic' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-arctic' ) ),
99 'Asia' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-asia' ) ),
100 'Atlantic' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-atlantic' ) ),
101 'Australia' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-australia' ) ),
102 'Europe' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-europe' ) ),
103 'Indian' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-indian' ) ),
104 'Pacific' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-pacific' ) ),
110 $now =
new DateTime();
112 foreach ( $identifiers as $identifier ) {
113 $parts = explode(
'/', $identifier, 2 );
118 if ( count( $parts ) !== 2 || !array_key_exists( $parts[0], $tzRegions ) ) {
123 $parts[0] = $tzRegions[$parts[0]];
125 $dateTimeZone =
new DateTimeZone( $identifier );
126 $minDiff = floor( $dateTimeZone->getOffset( $now ) / 60 );
128 $display = str_replace(
'_',
' ', $parts[0] .
'/' . $parts[1] );
129 $value =
"ZoneInfo|$minDiff|$identifier";
131 $timeZoneList[$identifier] = [
133 'timecorrection' => $value,
134 'region' => $parts[0],
138 return $timeZoneList;
145 $p = parent::validate( $value, $alldata );
151 return $this->mParent->msg(
'timezone-invalid' )->escaped();
161 $classes = parent::getFieldClasses();
162 $classes[] = self::FIELD_CLASS;
Select dropdown field, with an additional "other" textbox.
Dropdown widget that allows the user to select a timezone, either by choosing a geographic zone,...
getFieldClasses()
Returns a list of classes that should be applied to the widget itself.Unfortunately,...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
A class containing constants representing the names of configuration variables.
static getMain()
Get the RequestContext object associated with the main request.