4 * A field that will contain a date and/or time
6 * Currently recognizes only {YYYY}-{MM}-{DD}T{HH}:{MM}:{SS.S*}Z formatted dates.
8 * Besides the parameters recognized by HTMLTextField, additional recognized
9 * parameters in the field descriptor array include:
10 * type - 'date', 'time', or 'datetime'
11 * min - The minimum date to allow, in any recognized format.
12 * max - The maximum date to allow, in any recognized format.
13 * placeholder - The default comes from the htmlform-(date|time|datetime)-placeholder message.
15 * The result is a formatted date.
17 * @note This widget is not likely to work well in non-OOUI forms.
19 class HTMLDateTimeField
extends HTMLTextField
{
20 protected static $patterns = [
21 'date' => '[0-9]{4}-[01][0-9]-[0-3][0-9]',
22 'time' => '[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
23 '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?',
26 protected $mType = 'datetime';
28 public function __construct( $params ) {
29 parent
::__construct( $params );
31 $this->mType
= array_key_exists( 'type', $params )
35 if ( !in_array( $this->mType
, [ 'date', 'time', 'datetime' ] ) ) {
36 throw new InvalidArgumentException( "Invalid type '$this->mType'" );
39 if ( $this->mPlaceholder
=== '' ) {
40 // Messages: htmlform-date-placeholder htmlform-time-placeholder htmlform-datetime-placeholder
41 $this->mPlaceholder
= $this->msg( "htmlform-{$this->mType}-placeholder" )->text();
44 $this->mClass
.= ' mw-htmlform-datetime-field';
47 public function getAttributes( array $list ) {
48 $parentList = array_diff( $list, [ 'min', 'max' ] );
49 $ret = parent
::getAttributes( $parentList );
51 if ( in_array( 'min', $list ) && isset( $this->mParams
['min'] ) ) {
52 $min = $this->parseDate( $this->mParams
['min'] );
54 $ret['min'] = $this->formatDate( $min );
55 // Because Html::expandAttributes filters it out
56 $ret['data-min'] = $ret['min'];
59 if ( in_array( 'max', $list ) && isset( $this->mParams
['max'] ) ) {
60 $max = $this->parseDate( $this->mParams
['max'] );
62 $ret['max'] = $this->formatDate( $max );
63 // Because Html::expandAttributes filters it out
64 $ret['data-max'] = $ret['max'];
69 // Because Html::expandAttributes filters it out
70 $ret['data-step'] = 1;
72 $ret['type'] = $this->mType
;
73 $ret['pattern'] = static::$patterns[$this->mType
];
78 public function loadDataFromRequest( $request ) {
79 if ( !$request->getCheck( $this->mName
) ) {
80 return $this->getDefault();
83 $value = $request->getText( $this->mName
);
84 $date = $this->parseDate( $value );
85 return $date ?
$this->formatDate( $date ) : $value;
88 public function validate( $value, $alldata ) {
89 $p = parent
::validate( $value, $alldata );
95 if ( $value === '' ) {
96 // required was already checked by parent::validate
100 $date = $this->parseDate( $value );
102 // Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
103 return $this->msg( "htmlform-{$this->mType}-invalid" );
106 if ( isset( $this->mParams
['min'] ) ) {
107 $min = $this->parseDate( $this->mParams
['min'] );
108 if ( $min && $date < $min ) {
109 // Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
110 return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) );
114 if ( isset( $this->mParams
['max'] ) ) {
115 $max = $this->parseDate( $this->mParams
['max'] );
116 if ( $max && $date > $max ) {
117 // Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
118 return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) );
125 protected function parseDate( $value ) {
126 $value = trim( $value );
127 if ( $value === '' ) {
131 if ( $this->mType
=== 'date' ) {
132 $value .= ' T00:00:00+0000';
134 if ( $this->mType
=== 'time' ) {
135 $value = '1970-01-01 ' . $value . '+0000';
139 $date = new DateTime( $value, new DateTimeZone( 'GMT' ) );
140 return $date->getTimestamp();
141 } catch ( Exception
$ex ) {
146 protected function formatDate( $value ) {
147 switch ( $this->mType
) {
149 return gmdate( 'Y-m-d', $value );
152 return gmdate( 'H:i:s', $value );
155 return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
159 public function getInputOOUI( $value ) {
161 'type' => $this->mType
,
163 'name' => $this->mName
,
167 if ( isset( $this->mParams
['min'] ) ) {
168 $min = $this->parseDate( $this->mParams
['min'] );
170 $params['min'] = $this->formatDate( $min );
173 if ( isset( $this->mParams
['max'] ) ) {
174 $max = $this->parseDate( $this->mParams
['max'] );
176 $params['max'] = $this->formatDate( $max );
180 return new MediaWiki\Widget\
DateTimeInputWidget( $params );
183 protected function getOOUIModules() {
184 return [ 'mediawiki.widgets.datetime' ];
187 protected function shouldInfuseOOUI() {