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