Merge "Implement mediawiki.cookie module"
[lhc/web/wiklou.git] / includes / parser / DateFormatter.php
1 <?php
2 /**
3 * Date formatter
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * Date formatter, recognises dates in plain text and formats them according to user preferences.
26 * @todo preferences, OutputPage
27 * @ingroup Parser
28 */
29 class DateFormatter {
30 protected $mSource;
31
32 protected $mTarget;
33
34 /** @var string */
35 protected $monthNames = '';
36
37 /** @todo Are these unused? */
38 private $rxDM;
39 private $rxMD;
40 private $rxDMY;
41 private $rxYDM;
42 private $rxMDY;
43 private $rxYMD;
44
45 /** @var array */
46 protected $regexes;
47
48 /** @todo Are these unused? */
49 private $pDays;
50 private $pMonths;
51 private $pYears;
52
53 /** @var array */
54 protected $rules;
55
56 /** @var array */
57 protected $xMonths;
58
59 /** @var array */
60 protected $preferences;
61
62 /** @var bool */
63 protected $mLinked;
64
65 protected $lang;
66
67 const ALL = -1;
68 const NONE = 0;
69 const MDY = 1;
70 const DMY = 2;
71 const YMD = 3;
72 const ISO1 = 4;
73 const LASTPREF = 4;
74 const ISO2 = 5;
75 const YDM = 6;
76 const DM = 7;
77 const MD = 8;
78 const LAST = 8;
79
80 /**
81 * @param Language $lang In which language to format the date
82 */
83 function __construct( Language $lang ) {
84 $this->lang = $lang;
85
86 $this->monthNames = $this->getMonthRegex();
87 for ( $i = 1; $i <= 12; $i++ ) {
88 $this->xMonths[$this->lang->lc( $this->lang->getMonthName( $i ) )] = $i;
89 $this->xMonths[$this->lang->lc( $this->lang->getMonthAbbreviation( $i ) )] = $i;
90 }
91
92 $this->regexTrail = '(?![a-z])/iu';
93
94 # Partial regular expressions
95 $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')\]\]';
96 $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})\]\]';
97 $this->prxY = '\[\[(\d{1,4}([ _]BC|))\]\]';
98 $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
99 $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
100
101 # Real regular expressions
102 $this->regexes[self::DMY] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
103 $this->regexes[self::YDM] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
104 $this->regexes[self::MDY] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
105 $this->regexes[self::YMD] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
106 $this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
107 $this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
108 $this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
109 $this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
110
111 # Extraction keys
112 # See the comments in replace() for the meaning of the letters
113 $this->keys[self::DMY] = 'jFY';
114 $this->keys[self::YDM] = 'Y jF';
115 $this->keys[self::MDY] = 'FjY';
116 $this->keys[self::YMD] = 'Y Fj';
117 $this->keys[self::DM] = 'jF';
118 $this->keys[self::MD] = 'Fj';
119 $this->keys[self::ISO1] = 'ymd'; # y means ISO year
120 $this->keys[self::ISO2] = 'ymd';
121
122 # Target date formats
123 $this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
124 $this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
125 $this->targets[self::MDY] = '[[F j]], [[Y]]';
126 $this->targets[self::YMD] = '[[Y]] [[F j]]';
127 $this->targets[self::DM] = '[[F j|j F]]';
128 $this->targets[self::MD] = '[[F j]]';
129 $this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
130 $this->targets[self::ISO2] = '[[y-m-d]]';
131
132 # Rules
133 # pref source target
134 $this->rules[self::DMY][self::MD] = self::DM;
135 $this->rules[self::ALL][self::MD] = self::MD;
136 $this->rules[self::MDY][self::DM] = self::MD;
137 $this->rules[self::ALL][self::DM] = self::DM;
138 $this->rules[self::NONE][self::ISO2] = self::ISO1;
139
140 $this->preferences = array(
141 'default' => self::NONE,
142 'dmy' => self::DMY,
143 'mdy' => self::MDY,
144 'ymd' => self::YMD,
145 'ISO 8601' => self::ISO1,
146 );
147 }
148
149 /**
150 * Get a DateFormatter object
151 *
152 * @param Language|string|null $lang In which language to format the date
153 * Defaults to the site content language
154 * @return DateFormatter object
155 */
156 public static function &getInstance( $lang = null ) {
157 global $wgMemc, $wgContLang;
158 static $dateFormatter = false;
159 $lang = $lang ? wfGetLangObj( $lang ) : $wgContLang;
160 $key = wfMemcKey( 'dateformatter', $lang->getCode() );
161 if ( !$dateFormatter ) {
162 $dateFormatter = $wgMemc->get( $key );
163 if ( !$dateFormatter ) {
164 $dateFormatter = new DateFormatter( $lang );
165 $wgMemc->set( $key, $dateFormatter, 3600 );
166 }
167 }
168 return $dateFormatter;
169 }
170
171 /**
172 * @param string $preference User preference
173 * @param string $text Text to reformat
174 * @param array $options Array can contain 'linked' and/or 'match-whole'
175 *
176 * @return string
177 */
178 function reformat( $preference, $text, $options = array( 'linked' ) ) {
179 $linked = in_array( 'linked', $options );
180 $match_whole = in_array( 'match-whole', $options );
181
182 if ( isset( $this->preferences[$preference] ) ) {
183 $preference = $this->preferences[$preference];
184 } else {
185 $preference = self::NONE;
186 }
187 for ( $i = 1; $i <= self::LAST; $i++ ) {
188 $this->mSource = $i;
189 if ( isset( $this->rules[$preference][$i] ) ) {
190 # Specific rules
191 $this->mTarget = $this->rules[$preference][$i];
192 } elseif ( isset( $this->rules[self::ALL][$i] ) ) {
193 # General rules
194 $this->mTarget = $this->rules[self::ALL][$i];
195 } elseif ( $preference ) {
196 # User preference
197 $this->mTarget = $preference;
198 } else {
199 # Default
200 $this->mTarget = $i;
201 }
202 $regex = $this->regexes[$i];
203
204 // Horrible hack
205 if ( !$linked ) {
206 $regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
207 }
208
209 if ( $match_whole ) {
210 // Let's hope this works
211 $regex = preg_replace( '!^/!', '/^', $regex );
212 $regex = str_replace( $this->regexTrail,
213 '$' . $this->regexTrail, $regex );
214 }
215
216 // Another horrible hack
217 $this->mLinked = $linked;
218 $text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
219 unset( $this->mLinked );
220 }
221 return $text;
222 }
223
224 /**
225 * @param array $matches
226 * @return string
227 */
228 function replace( $matches ) {
229 # Extract information from $matches
230 $linked = true;
231 if ( isset( $this->mLinked ) ) {
232 $linked = $this->mLinked;
233 }
234
235 $bits = array();
236 $key = $this->keys[$this->mSource];
237 $keyLength = strlen( $key );
238 for ( $p = 0; $p < $keyLength; $p++ ) {
239 if ( $key[$p] != ' ' ) {
240 $bits[$key[$p]] = $matches[$p + 1];
241 }
242 }
243
244 return $this->formatDate( $bits, $linked );
245 }
246
247 /**
248 * @param array $bits
249 * @param bool $link
250 * @return string
251 */
252 function formatDate( $bits, $link = true ) {
253 $format = $this->targets[$this->mTarget];
254
255 if ( !$link ) {
256 // strip piped links
257 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
258 // strip remaining links
259 $format = str_replace( array( '[[', ']]' ), '', $format );
260 }
261
262 # Construct new date
263 $text = '';
264 $fail = false;
265
266 // Pre-generate y/Y stuff because we need the year for the <span> title.
267 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) {
268 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
269 }
270 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
271 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
272 }
273
274 if ( !isset( $bits['m'] ) ) {
275 $m = $this->makeIsoMonth( $bits['F'] );
276 if ( !$m || $m == '00' ) {
277 $fail = true;
278 } else {
279 $bits['m'] = $m;
280 }
281 }
282
283 if ( !isset( $bits['d'] ) ) {
284 $bits['d'] = sprintf( '%02d', $bits['j'] );
285 }
286
287 $formatLength = strlen( $format );
288 for ( $p = 0; $p < $formatLength; $p++ ) {
289 $char = $format[$p];
290 switch ( $char ) {
291 case 'd': # ISO day of month
292 $text .= $bits['d'];
293 break;
294 case 'm': # ISO month
295 $text .= $bits['m'];
296 break;
297 case 'y': # ISO year
298 $text .= $bits['y'];
299 break;
300 case 'j': # ordinary day of month
301 if ( !isset( $bits['j'] ) ) {
302 $text .= intval( $bits['d'] );
303 } else {
304 $text .= $bits['j'];
305 }
306 break;
307 case 'F': # long month
308 if ( !isset( $bits['F'] ) ) {
309 $m = intval( $bits['m'] );
310 if ( $m > 12 || $m < 1 ) {
311 $fail = true;
312 } else {
313 $text .= $this->lang->getMonthName( $m );
314 }
315 } else {
316 $text .= ucfirst( $bits['F'] );
317 }
318 break;
319 case 'Y': # ordinary (optional BC) year
320 $text .= $bits['Y'];
321 break;
322 default:
323 $text .= $char;
324 }
325 }
326 if ( $fail ) {
327 /** @todo FIXME: $matches doesn't exist here, what's expected? */
328 $text = $matches[0];
329 }
330
331 $isoBits = array();
332 if ( isset( $bits['y'] ) ) {
333 $isoBits[] = $bits['y'];
334 }
335 $isoBits[] = $bits['m'];
336 $isoBits[] = $bits['d'];
337 $isoDate = implode( '-', $isoBits );
338
339 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
340 $text = Html::rawElement( 'span',
341 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
342
343 return $text;
344 }
345
346 /**
347 * @todo document
348 * @return string
349 */
350 function getMonthRegex() {
351 $names = array();
352 for ( $i = 1; $i <= 12; $i++ ) {
353 $names[] = $this->lang->getMonthName( $i );
354 $names[] = $this->lang->getMonthAbbreviation( $i );
355 }
356 return implode( '|', $names );
357 }
358
359 /**
360 * Makes an ISO month, e.g. 02, from a month name
361 * @param string $monthName Month name
362 * @return string ISO month name
363 */
364 function makeIsoMonth( $monthName ) {
365 $n = $this->xMonths[$this->lang->lc( $monthName )];
366 return sprintf( '%02d', $n );
367 }
368
369 /**
370 * @todo document
371 * @param string $year Year name
372 * @return string ISO year name
373 */
374 function makeIsoYear( $year ) {
375 # Assumes the year is in a nice format, as enforced by the regex
376 if ( substr( $year, -2 ) == 'BC' ) {
377 $num = intval( substr( $year, 0, -3 ) ) - 1;
378 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
379 $text = sprintf( '-%04d', $num );
380
381 } else {
382 $text = sprintf( '%04d', $year );
383 }
384 return $text;
385 }
386
387 /**
388 * @todo document
389 * @param string $iso
390 * @return int|string
391 */
392 function makeNormalYear( $iso ) {
393 if ( $iso[0] == '-' ) {
394 $text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC';
395 } else {
396 $text = intval( $iso );
397 }
398 return $text;
399 }
400 }