MediaWiki REL1_35
TimezoneFilter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Preferences;
22
23use DateTimeZone;
24use Exception;
25
26class 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}
Base interface for user preference filters that work as a middleware between storage and interface.
Definition Filter.php:27