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