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