3use Wikimedia\RequestTimeout\TimeoutException;
24 'date' =>
'[0-9]{4}-[01][0-9]-[0-3][0-9]',
25 'time' =>
'[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
26 '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?',
36 parent::__construct( $params );
38 $this->mType = $params[
'type'] ??
'datetime';
40 if ( !in_array( $this->mType, [
'date',
'time',
'datetime' ] ) ) {
41 throw new InvalidArgumentException(
"Invalid type '$this->mType'" );
44 if ( $this->mPlaceholder ===
'' ) {
46 $this->mPlaceholder = $this->
msg(
"htmlform-{$this->mType}-placeholder" )->text();
49 $this->mClass .=
' mw-htmlform-datetime-field';
53 $parentList = array_diff( $list, [
'min',
'max' ] );
54 $ret = parent::getAttributes( $parentList );
56 if ( in_array(
'min', $list ) && isset( $this->mParams[
'min'] ) ) {
57 $min = $this->
parseDate( $this->mParams[
'min'] );
62 if ( in_array(
'max', $list ) && isset( $this->mParams[
'max'] ) ) {
63 $max = $this->
parseDate( $this->mParams[
'max'] );
78 if ( !$request->getCheck( $this->mName ) ) {
82 $value = $request->getText( $this->mName );
84 return $date ? $this->
formatDate( $date ) : $value;
88 $p = parent::validate( $value, $alldata );
94 if ( $value ===
'' ) {
102 return $this->
msg(
"htmlform-{$this->mType}-invalid" );
105 if ( isset( $this->mParams[
'min'] ) ) {
106 $min = $this->
parseDate( $this->mParams[
'min'] );
107 if ( $min && $date < $min ) {
109 return $this->
msg(
"htmlform-{$this->mType}-toolow", $this->
formatDate( $min ) );
113 if ( isset( $this->mParams[
'max'] ) ) {
114 $max = $this->
parseDate( $this->mParams[
'max'] );
115 if ( $max && $date > $max ) {
117 return $this->
msg(
"htmlform-{$this->mType}-toohigh", $this->
formatDate( $max ) );
125 $value = trim( $value ??
'' );
126 if ( $value ===
'' ) {
130 if ( $this->mType ===
'date' ) {
131 $value .=
' T00:00:00+0000';
133 if ( $this->mType ===
'time' ) {
134 $value =
'1970-01-01 ' . $value .
'+0000';
138 $date =
new DateTime( $value,
new DateTimeZone(
'GMT' ) );
139 return $date->getTimestamp();
140 }
catch ( TimeoutException $e ) {
142 }
catch ( Exception $ex ) {
148 switch ( $this->mType ) {
150 return gmdate(
'Y-m-d', $value );
153 return gmdate(
'H:i:s', $value );
156 return gmdate(
'Y-m-d\\TH:i:s\\Z', $value );
168 $params += OOUI\Element::configFromHtmlAttributes(
169 $this->
getAttributes( [
'disabled',
'readonly',
'min',
'max' ] )
172 if ( $this->mType ===
'date' ) {
173 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.DateInputWidget.styles' );
181 if ( $this->mType ===
'date' ) {
182 return [
'mediawiki.widgets.DateInputWidget' ];
184 return [
'mediawiki.widgets.datetime' ];
A field that will contain a date and/or time.
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
shouldInfuseOOUI()
Whether the field should be automatically infused.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getAttributes(array $list)
Returns the given attributes from the parameters.