8use InvalidArgumentException;
9use Wikimedia\RequestTimeout\TimeoutException;
30 'date' =>
'[0-9]{4}-[01][0-9]-[0-3][0-9]',
31 'time' =>
'[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
32 '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 $this->mType =
$params[
'type'] ??
'datetime';
46 if ( !in_array( $this->mType, [
'date',
'time',
'datetime' ] ) ) {
47 throw new InvalidArgumentException(
"Invalid type '$this->mType'" );
50 if ( $this->mPlaceholder ===
'' ) {
52 $this->mPlaceholder = $this->
msg(
"htmlform-{$this->mType}-placeholder" )->text();
55 $this->mClass .=
' mw-htmlform-datetime-field';
59 $parentList = array_diff( $list, [
'min',
'max' ] );
60 $ret = parent::getAttributes( $parentList );
62 if ( in_array(
'min', $list ) && isset( $this->mParams[
'min'] ) ) {
63 $min = $this->
parseDate( $this->mParams[
'min'] );
68 if ( in_array(
'max', $list ) && isset( $this->mParams[
'max'] ) ) {
69 $max = $this->
parseDate( $this->mParams[
'max'] );
84 if ( !$request->getCheck( $this->mName ) ) {
88 $value = $request->getText( $this->mName );
90 return $date ? $this->
formatDate( $date ) : $value;
94 $p = parent::validate( $value, $alldata );
100 if ( $value ===
'' ) {
108 return $this->
msg(
"htmlform-{$this->mType}-invalid" );
111 if ( isset( $this->mParams[
'min'] ) ) {
112 $min = $this->
parseDate( $this->mParams[
'min'] );
113 if ( $min && $date < $min ) {
115 return $this->
msg(
"htmlform-{$this->mType}-toolow", $this->
formatDate( $min ) );
119 if ( isset( $this->mParams[
'max'] ) ) {
120 $max = $this->
parseDate( $this->mParams[
'max'] );
121 if ( $max && $date > $max ) {
123 return $this->
msg(
"htmlform-{$this->mType}-toohigh", $this->
formatDate( $max ) );
131 $value = trim( $value ??
'' );
132 if ( $value ===
'' ) {
136 if ( $this->mType ===
'date' ) {
137 $value .=
' T00:00:00+0000';
139 if ( $this->mType ===
'time' ) {
140 $value =
'1970-01-01 ' . $value .
'+0000';
144 $date =
new DateTime( $value,
new DateTimeZone(
'GMT' ) );
145 return $date->getTimestamp();
146 }
catch ( TimeoutException $e ) {
148 }
catch ( Exception $ex ) {
154 switch ( $this->mType ) {
156 return gmdate(
'Y-m-d', $value );
159 return gmdate(
'H:i:s', $value );
162 return gmdate(
'Y-m-d\\TH:i:s\\Z', $value );
174 $params += \OOUI\Element::configFromHtmlAttributes(
175 $this->
getAttributes( [
'disabled',
'readonly',
'min',
'max' ] )
178 if ( $this->mType ===
'date' ) {
179 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.DateInputWidget.styles' );
180 return new \MediaWiki\Widget\DateInputWidget(
$params );
182 return new \MediaWiki\Widget\DateTimeInputWidget(
$params );
187 if ( $this->mType ===
'date' ) {
188 return [
'mediawiki.widgets.DateInputWidget' ];
190 return [
'mediawiki.widgets.datetime' ];
201class_alias( HTMLDateTimeField::class,
'HTMLDateTimeField' );
array $params
The job parameters.