MediaWiki master
HTMLTimezoneField.php
Go to the documentation of this file.
1<?php
2
4
5use DateTime;
6use DateTimeZone;
7use InvalidArgumentException;
15
22 private const FIELD_CLASS = 'mw-htmlform-timezone-field';
23
25 private $msgFormatter;
26
32 public function __construct( $params ) {
33 if ( isset( $params['options'] ) ) {
34 throw new InvalidArgumentException( "Options should not be provided to " . __CLASS__ );
35 }
36 $params['placeholder-message'] ??= 'timezone-useoffset-placeholder';
37 $params['options'] = [];
38 parent::__construct( $params );
39 $lang = $this->mParent ? $this->mParent->getLanguage() : RequestContext::getMain()->getLanguage();
40 $langCode = $lang->getCode();
41 $this->msgFormatter = MediaWikiServices::getInstance()->getMessageFormatterFactory()
42 ->getTextFormatter( $langCode );
43 $this->mOptions = $this->getTimezoneOptions();
44 }
45
49 private function getTimezoneOptions(): array {
50 $opt = [];
51
52 $localTZoffset = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LocalTZoffset );
53 $timeZoneList = $this->getTimeZoneList();
54
55 $timestamp = MWTimestamp::getLocalInstance();
56 // Check that the LocalTZoffset is the same as the local time zone offset
57 if ( $localTZoffset === (int)$timestamp->format( 'Z' ) / 60 ) {
58 $timezoneName = $timestamp->getTimezone()->getName();
59 // Localize timezone
60 if ( isset( $timeZoneList[$timezoneName] ) ) {
61 $timezoneName = $timeZoneList[$timezoneName]['name'];
62 }
63 $server_tz_msg = $this->msgFormatter->format(
64 MessageValue::new( 'timezoneuseserverdefault', [ $timezoneName ] )
65 );
66 } else {
67 $tzstring = UserTimeCorrection::formatTimezoneOffset( $localTZoffset );
68 $server_tz_msg = $this->msgFormatter->format(
69 MessageValue::new( 'timezoneuseserverdefault', [ $tzstring ] )
70 );
71 }
72 $opt[$server_tz_msg] = "System|$localTZoffset";
73 $opt[$this->msgFormatter->format( MessageValue::new( 'timezoneuseoffset' ) )] = 'other';
74 $opt[$this->msgFormatter->format( MessageValue::new( 'guesstimezone' ) )] = 'guess';
75
76 foreach ( $timeZoneList as $timeZoneInfo ) {
77 $region = $timeZoneInfo['region'];
78 if ( !isset( $opt[$region] ) ) {
79 $opt[$region] = [];
80 }
81 $opt[$region][$timeZoneInfo['name']] = $timeZoneInfo['timecorrection'];
82 }
83 return $opt;
84 }
85
92 private function getTimeZoneList(): array {
93 $identifiers = DateTimeZone::listIdentifiers();
94 '@phan-var array|false $identifiers'; // See phan issue #3162
95 if ( $identifiers === false ) {
96 return [];
97 }
98 sort( $identifiers );
99
100 $tzRegions = [
101 'Africa' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-africa' ) ),
102 'America' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-america' ) ),
103 'Antarctica' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-antarctica' ) ),
104 'Arctic' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-arctic' ) ),
105 'Asia' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-asia' ) ),
106 'Atlantic' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-atlantic' ) ),
107 'Australia' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-australia' ) ),
108 'Europe' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-europe' ) ),
109 'Indian' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-indian' ) ),
110 'Pacific' => $this->msgFormatter->format( MessageValue::new( 'timezoneregion-pacific' ) ),
111 ];
112 asort( $tzRegions );
113
114 $timeZoneList = [];
115
116 $now = new DateTime();
117
118 foreach ( $identifiers as $identifier ) {
119 $parts = explode( '/', $identifier, 2 );
120
121 // DateTimeZone::listIdentifiers() returns a number of
122 // backwards-compatibility entries. This filters them out of the
123 // list presented to the user.
124 if ( count( $parts ) !== 2 || !array_key_exists( $parts[0], $tzRegions ) ) {
125 continue;
126 }
127
128 // Localize region
129 $parts[0] = $tzRegions[$parts[0]];
130
131 $dateTimeZone = new DateTimeZone( $identifier );
132 $minDiff = floor( $dateTimeZone->getOffset( $now ) / 60 );
133
134 $display = str_replace( '_', ' ', $parts[0] . '/' . $parts[1] );
135 $value = "ZoneInfo|$minDiff|$identifier";
136
137 $timeZoneList[$identifier] = [
138 'name' => $display,
139 'timecorrection' => $value,
140 'region' => $parts[0],
141 ];
142 }
143
144 return $timeZoneList;
145 }
146
150 public function validate( $value, $alldata ) {
151 $p = parent::validate( $value, $alldata );
152 if ( $p !== true ) {
153 return $p;
154 }
155
156 if ( !( new UserTimeCorrection( $value ) )->isValid() ) {
157 return $this->mParent->msg( 'timezone-invalid' )->escaped();
158 }
159
160 return true;
161 }
162
166 protected function getFieldClasses(): array {
167 $classes = parent::getFieldClasses();
168 $classes[] = self::FIELD_CLASS;
169 return $classes;
170 }
171}
172
174class_alias( HTMLTimezoneField::class, 'HTMLTimezoneField' );
array $params
The job parameters.
Group all the pieces relevant to the context of a request into one instance.
Select dropdown field, with an additional "other" textbox.
Dropdown widget that allows the user to select a timezone, either by choosing a geographic zone,...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getFieldClasses()
Returns a list of classes that should be applied to the widget itself.Unfortunately,...
A class containing constants representing the names of configuration variables.
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.
Utility class to parse the TimeCorrection string value.
static formatTimezoneOffset(int $offset)
Converts a timezone offset in minutes (e.g., "120") to an hh:mm string like "+02:00".
Library for creating and parsing MW-style timestamps.
Value object representing a message for i18n.