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