MediaWiki REL1_37
HTMLDateTimeField.php
Go to the documentation of this file.
1<?php
2
21 protected static $patterns = [
22 'date' => '[0-9]{4}-[01][0-9]-[0-3][0-9]',
23 'time' => '[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
24 '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?',
25 ];
26
27 protected $mType = 'datetime';
28
33 public function __construct( $params ) {
34 parent::__construct( $params );
35
36 $this->mType = $params['type'] ?? 'datetime';
37
38 if ( !in_array( $this->mType, [ 'date', 'time', 'datetime' ] ) ) {
39 throw new InvalidArgumentException( "Invalid type '$this->mType'" );
40 }
41
42 if ( $this->mPlaceholder === '' ) {
43 // Messages: htmlform-date-placeholder htmlform-time-placeholder htmlform-datetime-placeholder
44 $this->mPlaceholder = $this->msg( "htmlform-{$this->mType}-placeholder" )->text();
45 }
46
47 $this->mClass .= ' mw-htmlform-datetime-field';
48 }
49
50 public function getAttributes( array $list ) {
51 $parentList = array_diff( $list, [ 'min', 'max' ] );
52 $ret = parent::getAttributes( $parentList );
53
54 if ( in_array( 'min', $list ) && isset( $this->mParams['min'] ) ) {
55 $min = $this->parseDate( $this->mParams['min'] );
56 if ( $min ) {
57 $ret['min'] = $this->formatDate( $min );
58 }
59 }
60 if ( in_array( 'max', $list ) && isset( $this->mParams['max'] ) ) {
61 $max = $this->parseDate( $this->mParams['max'] );
62 if ( $max ) {
63 $ret['max'] = $this->formatDate( $max );
64 }
65 }
66
67 $ret['step'] = 1;
68
69 $ret['type'] = $this->mType;
70 $ret['pattern'] = static::$patterns[$this->mType];
71
72 return $ret;
73 }
74
75 public function loadDataFromRequest( $request ) {
76 if ( !$request->getCheck( $this->mName ) ) {
77 return $this->getDefault();
78 }
79
80 $value = $request->getText( $this->mName );
81 $date = $this->parseDate( $value );
82 return $date ? $this->formatDate( $date ) : $value;
83 }
84
85 public function validate( $value, $alldata ) {
86 $p = parent::validate( $value, $alldata );
87
88 if ( $p !== true ) {
89 return $p;
90 }
91
92 if ( $value === '' ) {
93 // required was already checked by parent::validate
94 return true;
95 }
96
97 $date = $this->parseDate( $value );
98 if ( !$date ) {
99 // Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
100 return $this->msg( "htmlform-{$this->mType}-invalid" );
101 }
102
103 if ( isset( $this->mParams['min'] ) ) {
104 $min = $this->parseDate( $this->mParams['min'] );
105 if ( $min && $date < $min ) {
106 // Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
107 return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) );
108 }
109 }
110
111 if ( isset( $this->mParams['max'] ) ) {
112 $max = $this->parseDate( $this->mParams['max'] );
113 if ( $max && $date > $max ) {
114 // Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
115 return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) );
116 }
117 }
118
119 return true;
120 }
121
122 protected function parseDate( $value ) {
123 $value = trim( $value ?? '' );
124 if ( $value === '' ) {
125 return false;
126 }
127
128 if ( $this->mType === 'date' ) {
129 $value .= ' T00:00:00+0000';
130 }
131 if ( $this->mType === 'time' ) {
132 $value = '1970-01-01 ' . $value . '+0000';
133 }
134
135 try {
136 $date = new DateTime( $value, new DateTimeZone( 'GMT' ) );
137 return $date->getTimestamp();
138 } catch ( Exception $ex ) {
139 return false;
140 }
141 }
142
143 protected function formatDate( $value ) {
144 switch ( $this->mType ) {
145 case 'date':
146 return gmdate( 'Y-m-d', $value );
147
148 case 'time':
149 return gmdate( 'H:i:s', $value );
150
151 case 'datetime':
152 return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
153 }
154 }
155
156 public function getInputOOUI( $value ) {
157 $params = [
158 'type' => $this->mType,
159 'value' => $value,
160 'name' => $this->mName,
161 'id' => $this->mID,
162 ];
163
164 $params += OOUI\Element::configFromHtmlAttributes(
165 $this->getAttributes( [ 'disabled', 'readonly', 'min', 'max' ] )
166 );
167
168 if ( $this->mType === 'date' ) {
169 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
170 return new MediaWiki\Widget\DateInputWidget( $params );
171 } else {
172 return new MediaWiki\Widget\DateTimeInputWidget( $params );
173 }
174 }
175
176 protected function getOOUIModules() {
177 if ( $this->mType === 'date' ) {
178 return [ 'mediawiki.widgets.DateInputWidget' ];
179 } else {
180 return [ 'mediawiki.widgets.datetime' ];
181 }
182 }
183
184 protected function shouldInfuseOOUI() {
185 return true;
186 }
187
188}
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.
msg( $key,... $params)
Get a translated interface message.
<input> field.