MediaWiki REL1_34
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 new DateTimeZone( $data[2] );
48 // If the constructor didn't throw, we know it's valid
49 $valid = true;
50 } catch ( Exception $e ) {
51 // Not a valid timezone
52 }
53 }
54
55 if ( !$valid ) {
56 // If the supplied timezone doesn't exist, fall back to the encoded offset
57 return 'Offset|' . intval( $tz[1] );
58 }
59 return $tz;
60 case 'System':
61 return $tz;
62 default:
63 $data = explode( ':', $tz, 2 );
64 if ( count( $data ) == 2 ) {
65 $data[0] = intval( $data[0] );
66 $data[1] = intval( $data[1] );
67 $minDiff = abs( $data[0] ) * 60 + $data[1];
68 if ( $data[0] < 0 ) {
69 $minDiff = - $minDiff;
70 }
71 } else {
72 $minDiff = intval( $data[0] ) * 60;
73 }
74
75 # Max is +14:00 and min is -12:00, see:
76 # https://en.wikipedia.org/wiki/Timezone
77 # 14:00
78 $minDiff = min( $minDiff, 840 );
79 # -12:00
80 $minDiff = max( $minDiff, -720 );
81 return 'Offset|' . $minDiff;
82 }
83 }
84}
Base interface for user preference filters that work as a middleware between storage and interface.
Definition Filter.php:27