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