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?',
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';
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'] );
70 if ( in_array(
'max', $list ) && isset( $this->mParams[
'max'] ) ) {
71 $max = $this->
parseDate( $this->mParams[
'max'] );
86 if ( !$request->getCheck( $this->mName ) ) {
90 $value = $request->getText( $this->mName );
92 return $date ? $this->
formatDate( $date ) : $value;
96 $p = parent::validate( $value, $alldata );
102 if ( $value ===
'' ) {
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 ) {
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 ) {
125 return $this->
msg(
"htmlform-{$this->mType}-toohigh", $this->
formatDate( $max ) );
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 ) {
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 );
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 return new \MediaWiki\Widget\DateTimeInputWidget(
$params );
189 if ( $this->mType ===
'date' ) {
190 return [
'mediawiki.widgets.DateInputWidget' ];
192 return [
'mediawiki.widgets.datetime' ];
203class_alias( HTMLDateTimeField::class,
'HTMLDateTimeField' );
array $params
The job parameters.