some double quoting replaced by single quoting
[lhc/web/wiklou.git] / includes / DateFormatter.php
1 <?php
2 define("DF_ALL", -1);
3 define("DF_NONE", 0);
4 define("DF_MDY", 1);
5 define("DF_DMY", 2);
6 define("DF_YMD", 3);
7 define("DF_ISO1", 4);
8 define("DF_LASTPREF", 4);
9
10 define("DF_ISO2", 5);
11 define("DF_YDM", 6);
12 define("DF_DM", 7);
13 define("DF_MD", 8);
14 define("DF_LAST", 8);
15
16 # To do: preferences, OutputPage
17
18 class DateFormatter
19 {
20 var $mSource, $mTarget;
21 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
22
23 var $regexes, $pDays, $pMonths, $pYears;
24 var $rules, $xMonths;
25
26 function DateFormatter()
27 {
28 global $wgMonthNamesEn, $wgInputEncoding;
29
30 $this->monthNames = $this->getMonthRegex();
31 for ( $i=1; $i<=12; $i++ ) {
32 $this->xMonths[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
33 }
34
35 # Attempt at UTF-8 support, untested at the moment
36 if ( $wgInputEncoding == 'UTF-8' ) {
37 $this->regexTrail = '(?![a-z])/iu';
38 } else {
39 $this->regexTrail = '(?![a-z])/i';
40 }
41
42 # Partial regular expressions
43 $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')]]';
44 $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})]]';
45 $this->prxY = '\[\[(\d{1,4}([ _]BC|))]]';
46 $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
47 $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
48
49 # Real regular expressions
50 $this->regexes[DF_DMY] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
51 $this->regexes[DF_YDM] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
52 $this->regexes[DF_MDY] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
53 $this->regexes[DF_YMD] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
54 $this->regexes[DF_DM] = "/{$this->prxDM}{$this->regexTrail}";
55 $this->regexes[DF_MD] = "/{$this->prxMD}{$this->regexTrail}";
56 $this->regexes[DF_ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
57 $this->regexes[DF_ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
58
59 # Extraction keys
60 # See the comments in replace() for the meaning of the letters
61 $this->keys[DF_DMY] = 'jFY';
62 $this->keys[DF_YDM] = 'Y jF';
63 $this->keys[DF_MDY] = 'FjY';
64 $this->keys[DF_YMD] = 'Y Fj';
65 $this->keys[DF_DM] = 'jF';
66 $this->keys[DF_MD] = 'Fj';
67 $this->keys[DF_ISO1] = 'ymd'; # y means ISO year
68 $this->keys[DF_ISO2] = 'ymd';
69
70 # Target date formats
71 $this->targets[DF_DMY] = '[[F j|j F]] [[Y]]';
72 $this->targets[DF_YDM] = '[[Y]], [[F j|j F]]';
73 $this->targets[DF_MDY] = '[[F j]], [[Y]]';
74 $this->targets[DF_YMD] = '[[Y]] [[F j]]';
75 $this->targets[DF_DM] = '[[F j|j F]]';
76 $this->targets[DF_MD] = '[[F j]]';
77 $this->targets[DF_ISO1] = '[[Y|y]]-[[F j|m-d]]';
78 $this->targets[DF_ISO2] = '[[y-m-d]]';
79
80 # Rules
81 # pref source target
82 $this->rules[DF_DMY][DF_MD] = DF_DM;
83 $this->rules[DF_ALL][DF_MD] = DF_MD;
84 $this->rules[DF_MDY][DF_DM] = DF_MD;
85 $this->rules[DF_ALL][DF_DM] = DF_DM;
86 $this->rules[DF_NONE][DF_ISO2] = DF_ISO1;
87 }
88
89 function reformat( $preference, $text )
90 {
91 for ( $i=1; $i<=DF_LAST; $i++ ) {
92 $this->mSource = $i;
93 if ( @$this->rules[$preference][$i] ) {
94 # Specific rules
95 $this->mTarget = $this->rules[$preference][$i];
96 } elseif ( @$this->rules[DF_ALL][$i] ) {
97 # General rules
98 $this->mTarget = $this->rules[DF_ALL][$i];
99 } elseif ( $preference ) {
100 # User preference
101 $this->mTarget = $preference;
102 } else {
103 # Default
104 $this->mTarget = $i;
105 }
106 $text = preg_replace_callback( $this->regexes[$i], 'wfMainDateReplace', $text );
107 }
108 return $text;
109 }
110
111 function replace( $matches )
112 {
113 global $wgMonthNamesEn;
114 # Extract information from $matches
115 $bits = array();
116 $key = $this->keys[$this->mSource];
117 for ( $p=0; $p < strlen($key); $p++ ) {
118 if ( $key{$p} != ' ' ) {
119 $bits[$key{$p}] = $matches[$p+1];
120 }
121 }
122
123 $format = $this->targets[$this->mTarget];
124
125 # Construct new date
126 $text = '';
127 $fail = false;
128
129 for ( $p=0; $p < strlen( $format ); $p++ ) {
130 $char = $format{$p};
131 switch ( $char ) {
132 case 'd': # ISO day of month
133 if ( is_null($bits['d']) ) {
134 $text .= sprintf( '%02d', $bits['j'] );
135 } else {
136 $text .= $bits['d'];
137 }
138 break;
139 case 'm': # ISO month
140 if ( is_null($bits['m']) ) {
141 $m = $this->makeIsoMonth( $bits['F'] );
142 if ( !$m || $m == '00' ) {
143 $fail = true;
144 } else {
145 $text .= $m;
146 }
147 } else {
148 $text .= $bits['m'];
149 }
150 break;
151 case 'y': # ISO year
152 if ( is_null( $bits['y'] ) ) {
153 $text .= $this->makeIsoYear( $bits['Y'] );
154 } else {
155 $text .= $bits['y'];
156 }
157 break;
158 case 'j': # ordinary day of month
159 if ( is_null($bits['j']) ) {
160 $text .= IntVal( $bits['d'] );
161 } else {
162 $text .= $bits['j'];
163 }
164 break;
165 case 'F': # long month
166 if ( is_null( $bits['F'] ) ) {
167 $m = IntVal($bits['m']);
168 if ( $m > 12 || $m < 1 ) {
169 $fail = true;
170 } else {
171 $text .= $wgMonthNamesEn[$m-1];
172 }
173 } else {
174 $text .= ucfirst( $bits['F'] );
175 }
176 break;
177 case 'Y': # ordinary (optional BC) year
178 if ( is_null( $bits['Y'] ) ) {
179 $text .= $this->makeNormalYear( $bits['y'] );
180 } else {
181 $text .= $bits['Y'];
182 }
183 break;
184 default:
185 $text .= $char;
186 }
187 }
188 if ( $fail ) {
189 $text = $matches[0];
190 }
191 return $text;
192 }
193
194 function getMonthRegex()
195 {
196 global $wgMonthNamesEn;
197 return implode( '|', $wgMonthNamesEn );
198 }
199
200 # Makes an ISO month, e.g. 02, from a month name
201 function makeIsoMonth( $monthName )
202 {
203 $n = $this->xMonths[strtolower( $monthName )];
204 return sprintf( '%02d', $n );
205 }
206
207 function makeIsoYear( $year )
208 {
209 # Assumes the year is in a nice format, as enforced by the regex
210 if ( substr( $year, -2 ) == 'BC' ) {
211 $num = IntVal(substr( $year, 0, -3 )) - 1;
212 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
213 $text = sprintf( '-%04d', $num );
214
215 } else {
216 $text = sprintf( '%04d', $year );
217 }
218 return $text;
219 }
220
221 function makeNormalYear( $iso )
222 {
223 if ( $iso{0} == '-' ) {
224 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
225 } else {
226 $text = IntVal( $iso );
227 }
228 return $text;
229 }
230 }
231
232 function wfMainDateReplace( $matches )
233 {
234 global $wgDateFormatter;
235 return $wgDateFormatter->replace( $matches );
236 }
237 ?>