MediaWiki master
HTMLTimezoneField.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\HTMLForm\Field;
4
5use DateTime;
6use DateTimeZone;
7use InvalidArgumentException;
8use MediaWiki\MainConfigNames;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\User\UserTimeCorrection;
11use MediaWiki\Utils\MWTimestamp;
12use Wikimedia\Message\ITextFormatter;
13use Wikimedia\Message\MessageValue;
14
21 private const FIELD_CLASS = 'mw-htmlform-timezone-field';
22
24 private $msgFormatter;
25
31 public function __construct( $params ) {
32 if ( isset( $params['options'] ) ) {
33 throw new InvalidArgumentException( "Options should not be provided to " . __CLASS__ );
34 }
35 $params['placeholder-message'] ??= 'timezone-useoffset-placeholder';
36 $params['options'] = [];
37 parent::__construct( $params );
38 $langCode = $this->mParent->getLanguage()->getCode();
39 $this->msgFormatter = MediaWikiServices::getInstance()->getMessageFormatterFactory()
40 ->getTextFormatter( $langCode );
41 $this->mOptions = $this->getTimezoneOptions();
42 }
43
47 private function getTimezoneOptions(): array {
48 $opt = [];
49
50 $localTZoffset = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LocalTZoffset );
51 $timeZoneList = $this->getTimeZoneList();
52
53 $timestamp = MWTimestamp::getLocalInstance();
54 // Check that the LocalTZoffset is the same as the local time zone offset
55 if ( $localTZoffset === (int)$timestamp->format( 'Z' ) / 60 ) {
56 $timezoneName = $timestamp->getTimezone()->getName();
57 // Localize timezone
58 if ( isset( $timeZoneList[$timezoneName] ) ) {
59 $timezoneName = $timeZoneList[$timezoneName]['name'];
60 }
61 $server_tz_msg = $this->msgFormatter->format(
62 MessageValue::new( 'timezoneuseserverdefault', [ $timezoneName ] )
63 );
64 } else {
65 $tzstring = UserTimeCorrection::formatTimezoneOffset( $localTZoffset );
66 $server_tz_msg = $this->msgFormatter->format(
67 MessageValue::new( 'timezoneuseserverdefault', [ $tzstring ] )
68 );
69 }
70 $opt[$server_tz_msg] = "System|$localTZoffset";
71 $opt[$this->msgFormatter->format( MessageValue::new( 'timezoneuseoffset' ) )] = 'other';
72 $opt[$this->msgFormatter->format( MessageValue::new( 'guesstimezone' ) )] = 'guess';
73
74 foreach ( $timeZoneList as $timeZoneInfo ) {
75 $region = $timeZoneInfo['region'];
76 if ( !isset( $opt[$region] ) ) {
77 $opt[$region] = [];
78 }
79 $opt[$region][$timeZoneInfo['name']] = $timeZoneInfo['timecorrection'];
80 }
81 return $opt;
82 }
83
90 private function getTimeZoneList(): array {
91 $identifiers = DateTimeZone::listIdentifiers();
92 sort( $identifiers );
93
94 $tzRegions = [
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' ) ),
105 ];
106 asort( $tzRegions );
107
108 $timeZoneList = [];
109
110 $now = new DateTime();
111
112 foreach ( $identifiers as $identifier ) {
113 $parts = explode( '/', $identifier, 2 );
114
115 // DateTimeZone::listIdentifiers() returns a number of
116 // backwards-compatibility entries. This filters them out of the
117 // list presented to the user.
118 if ( count( $parts ) !== 2 || !array_key_exists( $parts[0], $tzRegions ) ) {
119 continue;
120 }
121
122 // Localize region
123 $parts[0] = $tzRegions[$parts[0]];
124
125 $dateTimeZone = new DateTimeZone( $identifier );
126 $minDiff = floor( $dateTimeZone->getOffset( $now ) / 60 );
127
128 $display = str_replace( '_', ' ', $parts[0] . '/' . $parts[1] );
129 $value = "ZoneInfo|$minDiff|$identifier";
130
131 $timeZoneList[$identifier] = [
132 'name' => $display,
133 'timecorrection' => $value,
134 'region' => $parts[0],
135 ];
136 }
137
138 return $timeZoneList;
139 }
140
144 public function validate( $value, $alldata ) {
145 $p = parent::validate( $value, $alldata );
146 if ( $p !== true ) {
147 return $p;
148 }
149
150 if ( !( new UserTimeCorrection( $value ) )->isValid() ) {
151 return $this->mParent->msg( 'timezone-invalid' )->escaped();
152 }
153
154 return true;
155 }
156
160 protected function getFieldClasses(): array {
161 $classes = parent::getFieldClasses();
162 $classes[] = self::FIELD_CLASS;
163 return $classes;
164 }
165}
166
168class_alias( HTMLTimezoneField::class, 'HTMLTimezoneField' );
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,...
const LocalTZoffset
Name constant for the LocalTZoffset setting, for use with Config\get().
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".
Converts MessageSpecifier objects to localized plain text in a certain language.