didn't mean to commit that comment
[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 $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 * @param $preference
116 * @param $text
117 */
118 function reformat( $preference, $text ) {
119 for ( $i=1; $i<=DF_LAST; $i++ ) {
120 $this->mSource = $i;
121 if ( @$this->rules[$preference][$i] ) {
122 # Specific rules
123 $this->mTarget = $this->rules[$preference][$i];
124 } elseif ( @$this->rules[DF_ALL][$i] ) {
125 # General rules
126 $this->mTarget = $this->rules[DF_ALL][$i];
127 } elseif ( $preference ) {
128 # User preference
129 $this->mTarget = $preference;
130 } else {
131 # Default
132 $this->mTarget = $i;
133 }
134 $text = preg_replace_callback( $this->regexes[$i], 'wfMainDateReplace', $text );
135 }
136 return $text;
137 }
138
139 /**
140 * @param $matches
141 */
142 function replace( $matches ) {
143 # Extract information from $matches
144 $bits = array();
145 $key = $this->keys[$this->mSource];
146 for ( $p=0; $p < strlen($key); $p++ ) {
147 if ( $key{$p} != ' ' ) {
148 $bits[$key{$p}] = $matches[$p+1];
149 }
150 }
151
152 $format = $this->targets[$this->mTarget];
153
154 # Construct new date
155 $text = '';
156 $fail = false;
157
158 for ( $p=0; $p < strlen( $format ); $p++ ) {
159 $char = $format{$p};
160 switch ( $char ) {
161 case 'd': # ISO day of month
162 if ( !isset($bits['d']) ) {
163 $text .= sprintf( '%02d', $bits['j'] );
164 } else {
165 $text .= $bits['d'];
166 }
167 break;
168 case 'm': # ISO month
169 if ( !isset($bits['m']) ) {
170 $m = $this->makeIsoMonth( $bits['F'] );
171 if ( !$m || $m == '00' ) {
172 $fail = true;
173 } else {
174 $text .= $m;
175 }
176 } else {
177 $text .= $bits['m'];
178 }
179 break;
180 case 'y': # ISO year
181 if ( !isset( $bits['y'] ) ) {
182 $text .= $this->makeIsoYear( $bits['Y'] );
183 } else {
184 $text .= $bits['y'];
185 }
186 break;
187 case 'j': # ordinary day of month
188 if ( !isset($bits['j']) ) {
189 $text .= IntVal( $bits['d'] );
190 } else {
191 $text .= $bits['j'];
192 }
193 break;
194 case 'F': # long month
195 if ( !isset( $bits['F'] ) ) {
196 $m = IntVal($bits['m']);
197 if ( $m > 12 || $m < 1 ) {
198 $fail = true;
199 } else {
200 global $wgContLang;
201 $text .= $wgContLang->getMonthName( $m );
202 }
203 } else {
204 $text .= ucfirst( $bits['F'] );
205 }
206 break;
207 case 'Y': # ordinary (optional BC) year
208 if ( !isset( $bits['Y'] ) ) {
209 $text .= $this->makeNormalYear( $bits['y'] );
210 } else {
211 $text .= $bits['Y'];
212 }
213 break;
214 default:
215 $text .= $char;
216 }
217 }
218 if ( $fail ) {
219 $text = $matches[0];
220 }
221 return $text;
222 }
223
224 /**
225 * @todo document
226 */
227 function getMonthRegex() {
228 global $wgContLang;
229 $names = array();
230 for( $i = 1; $i <= 12; $i++ ) {
231 $names[] = $wgContLang->getMonthName( $i );
232 $names[] = $wgContLang->getMonthAbbreviation( $i );
233 }
234 return implode( '|', $names );
235 }
236
237 /**
238 * Makes an ISO month, e.g. 02, from a month name
239 * @param string $monthName Month name
240 * @return string ISO month name
241 */
242 function makeIsoMonth( $monthName ) {
243 $n = $this->xMonths[strtolower( $monthName )];
244 return sprintf( '%02d', $n );
245 }
246
247 /**
248 * @todo document
249 * @param string $year Year name
250 * @return string ISO year name
251 */
252 function makeIsoYear( $year ) {
253 # Assumes the year is in a nice format, as enforced by the regex
254 if ( substr( $year, -2 ) == 'BC' ) {
255 $num = IntVal(substr( $year, 0, -3 )) - 1;
256 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
257 $text = sprintf( '-%04d', $num );
258
259 } else {
260 $text = sprintf( '%04d', $year );
261 }
262 return $text;
263 }
264
265 /**
266 * @todo document
267 */
268 function makeNormalYear( $iso ) {
269 if ( $iso{0} == '-' ) {
270 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
271 } else {
272 $text = IntVal( $iso );
273 }
274 return $text;
275 }
276 }
277
278 /**
279 * @todo document
280 */
281 function wfMainDateReplace( $matches ) {
282 global $wgDateFormatter;
283 return $wgDateFormatter->replace( $matches );
284 }
285 ?>