MediaWiki master
HTMLDateTimeField.php
Go to the documentation of this file.
1<?php
2
4
5use DateTime;
6use DateTimeZone;
7use Exception;
8use InvalidArgumentException;
9use Wikimedia\RequestTimeout\TimeoutException;
10
30 protected static $patterns = [
31 'date' => '[0-9]{4}-[01][0-9]-[0-3][0-9]',
32 'time' => '[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
33 'datetime' => '[0-9]{4}-[01][0-9]-[0-3][0-9][T ][0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?Z?',
34 ];
35
37 protected $mType = 'datetime';
38
43 public function __construct( $params ) {
44 parent::__construct( $params );
45
46 $this->mType = $params['type'] ?? 'datetime';
47
48 if ( !in_array( $this->mType, [ 'date', 'time', 'datetime' ] ) ) {
49 throw new InvalidArgumentException( "Invalid type '$this->mType'" );
50 }
51
52 if ( $this->mPlaceholder === '' ) {
53 // Messages: htmlform-date-placeholder htmlform-time-placeholder htmlform-datetime-placeholder
54 $this->mPlaceholder = $this->msg( "htmlform-{$this->mType}-placeholder" )->text();
55 }
56
57 $this->mClass .= ' mw-htmlform-datetime-field';
58 }
59
61 public function getAttributes( array $list ) {
62 $parentList = array_diff( $list, [ 'min', 'max' ] );
63 $ret = parent::getAttributes( $parentList );
64
65 if ( in_array( 'min', $list ) && isset( $this->mParams['min'] ) ) {
66 $min = $this->parseDate( $this->mParams['min'] );
67 if ( $min ) {
68 $ret['min'] = $this->formatDate( $min );
69 }
70 }
71 if ( in_array( 'max', $list ) && isset( $this->mParams['max'] ) ) {
72 $max = $this->parseDate( $this->mParams['max'] );
73 if ( $max ) {
74 $ret['max'] = $this->formatDate( $max );
75 }
76 }
77
78 $ret['step'] = 1;
79
80 $ret['type'] = $this->mType;
81 $ret['pattern'] = static::$patterns[$this->mType];
82
83 return $ret;
84 }
85
87 public function loadDataFromRequest( $request ) {
88 if ( !$request->getCheck( $this->mName ) ) {
89 return $this->getDefault();
90 }
91
92 $value = $request->getText( $this->mName );
93 $date = $this->parseDate( $value );
94 return $date ? $this->formatDate( $date ) : $value;
95 }
96
98 public function validate( $value, $alldata ) {
99 $p = parent::validate( $value, $alldata );
100
101 if ( $p !== true ) {
102 return $p;
103 }
104
105 if ( $value === '' ) {
106 // required was already checked by parent::validate
107 return true;
108 }
109
110 $date = $this->parseDate( $value );
111 if ( !$date ) {
112 // Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
113 return $this->msg( "htmlform-{$this->mType}-invalid" );
114 }
115
116 if ( isset( $this->mParams['min'] ) ) {
117 $min = $this->parseDate( $this->mParams['min'] );
118 if ( $min && $date < $min ) {
119 // Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
120 return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) );
121 }
122 }
123
124 if ( isset( $this->mParams['max'] ) ) {
125 $max = $this->parseDate( $this->mParams['max'] );
126 if ( $max && $date > $max ) {
127 // Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
128 return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) );
129 }
130 }
131
132 return true;
133 }
134
139 protected function parseDate( $value ) {
140 $value = trim( $value ?? '' );
141 if ( $value === '' ) {
142 return false;
143 }
144
145 if ( $this->mType === 'date' ) {
146 $value .= ' T00:00:00+0000';
147 }
148 if ( $this->mType === 'time' ) {
149 $value = '1970-01-01 ' . $value . '+0000';
150 }
151
152 try {
153 $date = new DateTime( $value, new DateTimeZone( 'GMT' ) );
154 return $date->getTimestamp();
155 } catch ( TimeoutException $e ) {
156 throw $e;
157 } catch ( Exception ) {
158 return false;
159 }
160 }
161
166 protected function formatDate( $value ) {
167 switch ( $this->mType ) {
168 case 'date':
169 return gmdate( 'Y-m-d', $value );
170
171 case 'time':
172 return gmdate( 'H:i:s', $value );
173
174 case 'datetime':
175 return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
176 }
177 }
178
180 public function getInputOOUI( $value ) {
181 $params = [
182 'type' => $this->mType,
183 'value' => $value,
184 'name' => $this->mName,
185 'id' => $this->mID,
186 ];
187
188 $params += \OOUI\Element::configFromHtmlAttributes(
189 $this->getAttributes( [ 'disabled', 'readonly', 'min', 'max' ] )
190 );
191
192 if ( $this->mType === 'date' ) {
193 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
194 return new \MediaWiki\Widget\DateInputWidget( $params );
195 } else {
196 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.DateTimeInputWidget.styles' );
197 return new \MediaWiki\Widget\DateTimeInputWidget( $params );
198 }
199 }
200
202 protected function getOOUIModules() {
203 if ( $this->mType === 'date' ) {
204 return [ 'mediawiki.widgets.DateInputWidget' ];
205 } else {
206 return [ 'mediawiki.widgets.datetime' ];
207 }
208 }
209
211 protected function shouldInfuseOOUI() {
212 return true;
213 }
214
215}
216
218class_alias( HTMLDateTimeField::class, 'HTMLDateTimeField' );
A field that will contain a date and/or time.
getAttributes(array $list)
Returns the given attributes from the parameters.to overridearray Attributes
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
msg( $key,... $params)
Get a translated interface message.