MediaWiki  master
TimezoneFilter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Preferences;
22 
23 use DateTimeZone;
24 use Exception;
25 
26 class TimezoneFilter implements Filter {
27 
31  public function filterForForm( $value ) {
32  return $value;
33  }
34 
38  public function filterFromForm( $tz ) {
39  $data = explode( '|', $tz, 3 );
40  switch ( $data[0] ) {
41  case 'ZoneInfo':
42  $valid = false;
43 
44  if ( count( $data ) === 3 ) {
45  // Make sure this timezone exists
46  try {
47  // @phan-suppress-next-line PhanNoopNew
48  new DateTimeZone( $data[2] );
49  // If the constructor didn't throw, we know it's valid
50  $valid = true;
51  } catch ( Exception $e ) {
52  // Not a valid timezone
53  }
54  }
55 
56  if ( !$valid ) {
57  // If the supplied timezone doesn't exist, fall back to the encoded offset
58  return 'Offset|' . intval( $tz[1] );
59  }
60  return $tz;
61  case 'System':
62  return $tz;
63  default:
64  $data = explode( ':', $tz, 2 );
65  if ( count( $data ) == 2 ) {
66  $data[0] = intval( $data[0] );
67  $data[1] = intval( $data[1] );
68  $minDiff = abs( $data[0] ) * 60 + $data[1];
69  if ( $data[0] < 0 ) {
70  $minDiff = - $minDiff;
71  }
72  } else {
73  $minDiff = intval( $data[0] ) * 60;
74  }
75 
76  # Max is +14:00 and min is -12:00, see:
77  # https://en.wikipedia.org/wiki/Timezone
78  # 14:00
79  $minDiff = min( $minDiff, 840 );
80  # -12:00
81  $minDiff = max( $minDiff, -720 );
82  return 'Offset|' . $minDiff;
83  }
84  }
85 }
MediaWiki\Preferences
Definition: DefaultPreferencesFactory.php:21
MediaWiki\Preferences\TimezoneFilter\filterFromForm
filterFromForm( $tz)
mixed
Definition: TimezoneFilter.php:38
MediaWiki\Preferences\TimezoneFilter\filterForForm
filterForForm( $value)
mixed
Definition: TimezoneFilter.php:31
MediaWiki\Preferences\TimezoneFilter
Definition: TimezoneFilter.php:26
MediaWiki\Preferences\Filter
Base interface for user preference filters that work as a middleware between storage and interface.
Definition: Filter.php:27