MediaWiki  master
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 = array_key_exists( 'type', $params )
37  ? $params['type']
38  : '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 ( Exception $ex ) {
141  return false;
142  }
143  }
144 
145  protected function formatDate( $value ) {
146  switch ( $this->mType ) {
147  case 'date':
148  return gmdate( 'Y-m-d', $value );
149 
150  case 'time':
151  return gmdate( 'H:i:s', $value );
152 
153  case 'datetime':
154  return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
155  }
156  }
157 
158  public function getInputOOUI( $value ) {
159  $params = [
160  'type' => $this->mType,
161  'value' => $value,
162  'name' => $this->mName,
163  'id' => $this->mID,
164  ];
165 
166  if ( isset( $this->mParams['min'] ) ) {
167  $min = $this->parseDate( $this->mParams['min'] );
168  if ( $min ) {
169  $params['min'] = $this->formatDate( $min );
170  }
171  }
172  if ( isset( $this->mParams['max'] ) ) {
173  $max = $this->parseDate( $this->mParams['max'] );
174  if ( $max ) {
175  $params['max'] = $this->formatDate( $max );
176  }
177  }
178 
179  if ( $this->mType === 'date' ) {
180  $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
181  return new MediaWiki\Widget\DateInputWidget( $params );
182  } else {
183  return new MediaWiki\Widget\DateTimeInputWidget( $params );
184  }
185  }
186 
187  protected function getOOUIModules() {
188  if ( $this->mType === 'date' ) {
189  return [ 'mediawiki.widgets.DateInputWidget' ];
190  } else {
191  return [ 'mediawiki.widgets.datetime' ];
192  }
193  }
194 
195  protected function shouldInfuseOOUI() {
196  return true;
197  }
198 
199 }
HTMLDateTimeField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLDateTimeField.php:195
HTMLDateTimeField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
Definition: HTMLDateTimeField.php:158
HTMLTextField
<input> field.
Definition: HTMLTextField.php:15
MediaWiki\Widget\DateInputWidget
Date input widget.
Definition: DateInputWidget.php:14
HTMLFormField\$mName
$mName
Definition: HTMLFormField.php:15
HTMLDateTimeField
A field that will contain a date and/or time.
Definition: HTMLDateTimeField.php:20
HTMLDateTimeField\parseDate
parseDate( $value)
Definition: HTMLDateTimeField.php:124
HTMLFormField\$mID
$mID
Definition: HTMLFormField.php:18
HTMLDateTimeField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLDateTimeField.php:87
HTMLDateTimeField\$mType
$mType
Definition: HTMLDateTimeField.php:27
HTMLFormField\getDefault
getDefault()
Stable to override.
Definition: HTMLFormField.php:999
HTMLDateTimeField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLDateTimeField.php:187
MediaWiki\Widget\DateTimeInputWidget
Date-time input widget.
Definition: DateTimeInputWidget.php:13
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:87
HTMLDateTimeField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters Stable to override.
Definition: HTMLDateTimeField.php:52
HTMLDateTimeField\formatDate
formatDate( $value)
Definition: HTMLDateTimeField.php:145
HTMLDateTimeField\$patterns
static $patterns
Definition: HTMLDateTimeField.php:21
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:77
HTMLDateTimeField\__construct
__construct( $params)
Stable to call Stable to call.type: HTML textfield typesize: field size in characters (defaults to 45...
Definition: HTMLDateTimeField.php:33