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