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