Declare visibility on class properties of includes/parser/
[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 for ( $p = 0; $p < strlen( $key ); $p++ ) {
238 if ( $key[$p] != ' ' ) {
239 $bits[$key[$p]] = $matches[$p + 1];
240 }
241 }
242
243 return $this->formatDate( $bits, $linked );
244 }
245
246 /**
247 * @param array $bits
248 * @param bool $link
249 * @return string
250 */
251 function formatDate( $bits, $link = true ) {
252 $format = $this->targets[$this->mTarget];
253
254 if ( !$link ) {
255 // strip piped links
256 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
257 // strip remaining links
258 $format = str_replace( array( '[[', ']]' ), '', $format );
259 }
260
261 # Construct new date
262 $text = '';
263 $fail = false;
264
265 // Pre-generate y/Y stuff because we need the year for the <span> title.
266 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) {
267 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
268 }
269 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
270 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
271 }
272
273 if ( !isset( $bits['m'] ) ) {
274 $m = $this->makeIsoMonth( $bits['F'] );
275 if ( !$m || $m == '00' ) {
276 $fail = true;
277 } else {
278 $bits['m'] = $m;
279 }
280 }
281
282 if ( !isset( $bits['d'] ) ) {
283 $bits['d'] = sprintf( '%02d', $bits['j'] );
284 }
285
286 for ( $p = 0; $p < strlen( $format ); $p++ ) {
287 $char = $format[$p];
288 switch ( $char ) {
289 case 'd': # ISO day of month
290 $text .= $bits['d'];
291 break;
292 case 'm': # ISO month
293 $text .= $bits['m'];
294 break;
295 case 'y': # ISO year
296 $text .= $bits['y'];
297 break;
298 case 'j': # ordinary day of month
299 if ( !isset( $bits['j'] ) ) {
300 $text .= intval( $bits['d'] );
301 } else {
302 $text .= $bits['j'];
303 }
304 break;
305 case 'F': # long month
306 if ( !isset( $bits['F'] ) ) {
307 $m = intval( $bits['m'] );
308 if ( $m > 12 || $m < 1 ) {
309 $fail = true;
310 } else {
311 $text .= $this->lang->getMonthName( $m );
312 }
313 } else {
314 $text .= ucfirst( $bits['F'] );
315 }
316 break;
317 case 'Y': # ordinary (optional BC) year
318 $text .= $bits['Y'];
319 break;
320 default:
321 $text .= $char;
322 }
323 }
324 if ( $fail ) {
325 $text = $matches[0];
326 }
327
328 $isoBits = array();
329 if ( isset( $bits['y'] ) ) {
330 $isoBits[] = $bits['y'];
331 }
332 $isoBits[] = $bits['m'];
333 $isoBits[] = $bits['d'];
334 $isoDate = implode( '-', $isoBits );
335
336 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
337 $text = Html::rawElement( 'span',
338 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
339
340 return $text;
341 }
342
343 /**
344 * @todo document
345 * @return string
346 */
347 function getMonthRegex() {
348 $names = array();
349 for ( $i = 1; $i <= 12; $i++ ) {
350 $names[] = $this->lang->getMonthName( $i );
351 $names[] = $this->lang->getMonthAbbreviation( $i );
352 }
353 return implode( '|', $names );
354 }
355
356 /**
357 * Makes an ISO month, e.g. 02, from a month name
358 * @param string $monthName Month name
359 * @return string ISO month name
360 */
361 function makeIsoMonth( $monthName ) {
362 $n = $this->xMonths[$this->lang->lc( $monthName )];
363 return sprintf( '%02d', $n );
364 }
365
366 /**
367 * @todo document
368 * @param string $year Year name
369 * @return string ISO year name
370 */
371 function makeIsoYear( $year ) {
372 # Assumes the year is in a nice format, as enforced by the regex
373 if ( substr( $year, -2 ) == 'BC' ) {
374 $num = intval( substr( $year, 0, -3 ) ) - 1;
375 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
376 $text = sprintf( '-%04d', $num );
377
378 } else {
379 $text = sprintf( '%04d', $year );
380 }
381 return $text;
382 }
383
384 /**
385 * @todo document
386 * @return int|string
387 */
388 function makeNormalYear( $iso ) {
389 if ( $iso[0] == '-' ) {
390 $text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC';
391 } else {
392 $text = intval( $iso );
393 }
394 return $text;
395 }
396 }