7b59a1d6df3c94ed5172997551b93b57f4697c1c
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 );
57 if ( in_array( 'max', $list ) && isset( $this->mParams
['max'] ) ) {
58 $max = $this->parseDate( $this->mParams
['max'] );
60 $ret['max'] = $this->formatDate( $max );
66 $ret['type'] = $this->mType
;
67 $ret['pattern'] = static::$patterns[$this->mType
];
72 public function loadDataFromRequest( $request ) {
73 if ( !$request->getCheck( $this->mName
) ) {
74 return $this->getDefault();
77 $value = $request->getText( $this->mName
);
78 $date = $this->parseDate( $value );
79 return $date ?
$this->formatDate( $date ) : $value;
82 public function validate( $value, $alldata ) {
83 $p = parent
::validate( $value, $alldata );
89 if ( $value === '' ) {
90 // required was already checked by parent::validate
94 $date = $this->parseDate( $value );
96 // Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
97 return $this->msg( "htmlform-{$this->mType}-invalid" );
100 if ( isset( $this->mParams
['min'] ) ) {
101 $min = $this->parseDate( $this->mParams
['min'] );
102 if ( $min && $date < $min ) {
103 // Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
104 return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) );
108 if ( isset( $this->mParams
['max'] ) ) {
109 $max = $this->parseDate( $this->mParams
['max'] );
110 if ( $max && $date > $max ) {
111 // Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
112 return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) );
119 protected function parseDate( $value ) {
120 $value = trim( $value );
121 if ( $value === '' ) {
125 if ( $this->mType
=== 'date' ) {
126 $value .= ' T00:00:00+0000';
128 if ( $this->mType
=== 'time' ) {
129 $value = '1970-01-01 ' . $value . '+0000';
133 $date = new DateTime( $value, new DateTimeZone( 'GMT' ) );
134 return $date->getTimestamp();
135 } catch ( Exception
$ex ) {
140 protected function formatDate( $value ) {
141 switch ( $this->mType
) {
143 return gmdate( 'Y-m-d', $value );
146 return gmdate( 'H:i:s', $value );
149 return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
153 public function getInputOOUI( $value ) {
155 'type' => $this->mType
,
157 'name' => $this->mName
,
161 if ( isset( $this->mParams
['min'] ) ) {
162 $min = $this->parseDate( $this->mParams
['min'] );
164 $params['min'] = $this->formatDate( $min );
167 if ( isset( $this->mParams
['max'] ) ) {
168 $max = $this->parseDate( $this->mParams
['max'] );
170 $params['max'] = $this->formatDate( $max );
174 return new MediaWiki\Widget\
DateTimeInputWidget( $params );
177 protected function getOOUIModules() {
178 return [ 'mediawiki.widgets.datetime' ];
181 protected function shouldInfuseOOUI() {