8use InvalidArgumentException;
9use Wikimedia\RequestTimeout\TimeoutException;
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?',
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 ===
'' ) {
54 $this->mPlaceholder = $this->
msg(
"htmlform-{$this->mType}-placeholder" )->text();
57 $this->mClass .=
' mw-htmlform-datetime-field';
62 $parentList = array_diff( $list, [
'min',
'max' ] );
63 $ret = parent::getAttributes( $parentList );
65 if ( in_array(
'min', $list ) && isset( $this->mParams[
'min'] ) ) {
66 $min = $this->
parseDate( $this->mParams[
'min'] );
71 if ( in_array(
'max', $list ) && isset( $this->mParams[
'max'] ) ) {
72 $max = $this->
parseDate( $this->mParams[
'max'] );
88 if ( !$request->getCheck( $this->mName ) ) {
92 $value = $request->getText( $this->mName );
94 return $date ? $this->
formatDate( $date ) : $value;
99 $p = parent::validate( $value, $alldata );
105 if ( $value ===
'' ) {
113 return $this->
msg(
"htmlform-{$this->mType}-invalid" );
116 if ( isset( $this->mParams[
'min'] ) ) {
117 $min = $this->
parseDate( $this->mParams[
'min'] );
118 if ( $min && $date < $min ) {
120 return $this->
msg(
"htmlform-{$this->mType}-toolow", $this->
formatDate( $min ) );
124 if ( isset( $this->mParams[
'max'] ) ) {
125 $max = $this->
parseDate( $this->mParams[
'max'] );
126 if ( $max && $date > $max ) {
128 return $this->
msg(
"htmlform-{$this->mType}-toohigh", $this->
formatDate( $max ) );
140 $value = trim( $value ??
'' );
141 if ( $value ===
'' ) {
145 if ( $this->mType ===
'date' ) {
146 $value .=
' T00:00:00+0000';
148 if ( $this->mType ===
'time' ) {
149 $value =
'1970-01-01 ' . $value .
'+0000';
153 $date =
new DateTime( $value,
new DateTimeZone(
'GMT' ) );
154 return $date->getTimestamp();
155 }
catch ( TimeoutException $e ) {
157 }
catch ( Exception ) {
167 switch ( $this->mType ) {
169 return gmdate(
'Y-m-d', $value );
172 return gmdate(
'H:i:s', $value );
175 return gmdate(
'Y-m-d\\TH:i:s\\Z', $value );
188 $params += \OOUI\Element::configFromHtmlAttributes(
189 $this->
getAttributes( [
'disabled',
'readonly',
'min',
'max' ] )
192 if ( $this->mType ===
'date' ) {
193 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.DateInputWidget.styles' );
194 return new \MediaWiki\Widget\DateInputWidget( $params );
196 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.DateTimeInputWidget.styles' );
197 return new \MediaWiki\Widget\DateTimeInputWidget( $params );
203 if ( $this->mType ===
'date' ) {
204 return [
'mediawiki.widgets.DateInputWidget' ];
206 return [
'mediawiki.widgets.datetime' ];
218class_alias( HTMLDateTimeField::class,
'HTMLDateTimeField' );