3 namespace MediaWiki\HTMLForm\Field
;
8 use InvalidArgumentException
;
9 use Wikimedia\RequestTimeout\TimeoutException
;
12 * A field that will contain a date and/or time
14 * Currently recognizes only {YYYY}-{MM}-{DD}T{HH}:{MM}:{SS.S*}Z formatted dates.
16 * Besides the parameters recognized by HTMLTextField, additional recognized
17 * parameters in the field descriptor array include:
18 * type - 'date', 'time', or 'datetime'
19 * min - The minimum date to allow, in any recognized format.
20 * max - The maximum date to allow, in any recognized format.
21 * placeholder - The default comes from the htmlform-(date|time|datetime)-placeholder message.
23 * The result is a formatted date.
26 * @note This widget is not likely to work well in non-OOUI forms.
28 class HTMLDateTimeField
extends HTMLTextField
{
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?',
37 protected $mType = 'datetime';
43 public function __construct( $params ) {
44 parent
::__construct( $params );
46 $this->mType
= $params['type'] ??
'datetime';
48 if ( !in_array( $this->mType
, [ 'date', 'time', 'datetime' ] ) ) {
49 throw new InvalidArgumentException( "Invalid type '$this->mType'" );
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();
57 $this->mClass
.= ' mw-htmlform-datetime-field';
60 public function getAttributes( array $list ) {
61 $parentList = array_diff( $list, [ 'min', 'max' ] );
62 $ret = parent
::getAttributes( $parentList );
64 if ( in_array( 'min', $list ) && isset( $this->mParams
['min'] ) ) {
65 $min = $this->parseDate( $this->mParams
['min'] );
67 $ret['min'] = $this->formatDate( $min );
70 if ( in_array( 'max', $list ) && isset( $this->mParams
['max'] ) ) {
71 $max = $this->parseDate( $this->mParams
['max'] );
73 $ret['max'] = $this->formatDate( $max );
79 $ret['type'] = $this->mType
;
80 $ret['pattern'] = static::$patterns[$this->mType
];
85 public function loadDataFromRequest( $request ) {
86 if ( !$request->getCheck( $this->mName
) ) {
87 return $this->getDefault();
90 $value = $request->getText( $this->mName
);
91 $date = $this->parseDate( $value );
92 return $date ?
$this->formatDate( $date ) : $value;
95 public function validate( $value, $alldata ) {
96 $p = parent
::validate( $value, $alldata );
102 if ( $value === '' ) {
103 // required was already checked by parent::validate
107 $date = $this->parseDate( $value );
109 // Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
110 return $this->msg( "htmlform-{$this->mType}-invalid" );
113 if ( isset( $this->mParams
['min'] ) ) {
114 $min = $this->parseDate( $this->mParams
['min'] );
115 if ( $min && $date < $min ) {
116 // Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
117 return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) );
121 if ( isset( $this->mParams
['max'] ) ) {
122 $max = $this->parseDate( $this->mParams
['max'] );
123 if ( $max && $date > $max ) {
124 // Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
125 return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) );
132 protected function parseDate( $value ) {
133 $value = trim( $value ??
'' );
134 if ( $value === '' ) {
138 if ( $this->mType
=== 'date' ) {
139 $value .= ' T00:00:00+0000';
141 if ( $this->mType
=== 'time' ) {
142 $value = '1970-01-01 ' . $value . '+0000';
146 $date = new DateTime( $value, new DateTimeZone( 'GMT' ) );
147 return $date->getTimestamp();
148 } catch ( TimeoutException
$e ) {
150 } catch ( Exception
$ex ) {
155 protected function formatDate( $value ) {
156 switch ( $this->mType
) {
158 return gmdate( 'Y-m-d', $value );
161 return gmdate( 'H:i:s', $value );
164 return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
168 public function getInputOOUI( $value ) {
170 'type' => $this->mType
,
172 'name' => $this->mName
,
176 $params +
= \OOUI\Element
::configFromHtmlAttributes(
177 $this->getAttributes( [ 'disabled', 'readonly', 'min', 'max' ] )
180 if ( $this->mType
=== 'date' ) {
181 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
182 return new \MediaWiki\Widget\
DateInputWidget( $params );
184 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.widgets.DateTimeInputWidget.styles' );
185 return new \MediaWiki\Widget\
DateTimeInputWidget( $params );
189 protected function getOOUIModules() {
190 if ( $this->mType
=== 'date' ) {
191 return [ 'mediawiki.widgets.DateInputWidget' ];
193 return [ 'mediawiki.widgets.datetime' ];
197 protected function shouldInfuseOOUI() {
203 /** @deprecated class alias since 1.42 */
204 class_alias( HTMLDateTimeField
::class, 'HTMLDateTimeField' );