Warn if stateful ParserOutput transforms are used
[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 private $mSource, $mTarget;
31 private $monthNames = '';
32
33 private $regexes;
34 private $rules, $xMonths, $preferences;
35
36 private $lang, $mLinked;
37
38 /** @var string[] */
39 private $keys;
40
41 /** @var string[] */
42 private $targets;
43
44 const ALL = -1;
45 const NONE = 0;
46 const MDY = 1;
47 const DMY = 2;
48 const YMD = 3;
49 const ISO1 = 4;
50 const LASTPREF = 4;
51 const ISO2 = 5;
52 const YDM = 6;
53 const DM = 7;
54 const MD = 8;
55 const LAST = 8;
56
57 /**
58 * @param Language $lang In which language to format the date
59 */
60 public function __construct( Language $lang ) {
61 $this->lang = $lang;
62
63 $this->monthNames = $this->getMonthRegex();
64 for ( $i = 1; $i <= 12; $i++ ) {
65 $this->xMonths[$this->lang->lc( $this->lang->getMonthName( $i ) )] = $i;
66 $this->xMonths[$this->lang->lc( $this->lang->getMonthAbbreviation( $i ) )] = $i;
67 }
68
69 $this->regexTrail = '(?![a-z])/iu';
70
71 # Partial regular expressions
72 $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')\]\]';
73 $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})\]\]';
74 $this->prxY = '\[\[(\d{1,4}([ _]BC|))\]\]';
75 $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
76 $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
77
78 # Real regular expressions
79 $this->regexes[self::DMY] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
80 $this->regexes[self::YDM] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
81 $this->regexes[self::MDY] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
82 $this->regexes[self::YMD] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
83 $this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
84 $this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
85 $this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
86 $this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
87
88 # Extraction keys
89 # See the comments in replace() for the meaning of the letters
90 $this->keys[self::DMY] = 'jFY';
91 $this->keys[self::YDM] = 'Y jF';
92 $this->keys[self::MDY] = 'FjY';
93 $this->keys[self::YMD] = 'Y Fj';
94 $this->keys[self::DM] = 'jF';
95 $this->keys[self::MD] = 'Fj';
96 $this->keys[self::ISO1] = 'ymd'; # y means ISO year
97 $this->keys[self::ISO2] = 'ymd';
98
99 # Target date formats
100 $this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
101 $this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
102 $this->targets[self::MDY] = '[[F j]], [[Y]]';
103 $this->targets[self::YMD] = '[[Y]] [[F j]]';
104 $this->targets[self::DM] = '[[F j|j F]]';
105 $this->targets[self::MD] = '[[F j]]';
106 $this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
107 $this->targets[self::ISO2] = '[[y-m-d]]';
108
109 # Rules
110 # pref source target
111 $this->rules[self::DMY][self::MD] = self::DM;
112 $this->rules[self::ALL][self::MD] = self::MD;
113 $this->rules[self::MDY][self::DM] = self::MD;
114 $this->rules[self::ALL][self::DM] = self::DM;
115 $this->rules[self::NONE][self::ISO2] = self::ISO1;
116
117 $this->preferences = [
118 'default' => self::NONE,
119 'dmy' => self::DMY,
120 'mdy' => self::MDY,
121 'ymd' => self::YMD,
122 'ISO 8601' => self::ISO1,
123 ];
124 }
125
126 /**
127 * Get a DateFormatter object
128 *
129 * @param Language|string|null $lang In which language to format the date
130 * Defaults to the site content language
131 * @return DateFormatter
132 */
133 public static function getInstance( $lang = null ) {
134 global $wgContLang, $wgMainCacheType;
135
136 $lang = $lang ? wfGetLangObj( $lang ) : $wgContLang;
137 $cache = ObjectCache::getLocalServerInstance( $wgMainCacheType );
138
139 static $dateFormatter = false;
140 if ( !$dateFormatter ) {
141 $dateFormatter = $cache->getWithSetCallback(
142 $cache->makeKey( 'dateformatter', $lang->getCode() ),
143 $cache::TTL_HOUR,
144 function () use ( $lang ) {
145 return new DateFormatter( $lang );
146 }
147 );
148 }
149
150 return $dateFormatter;
151 }
152
153 /**
154 * @param string $preference User preference
155 * @param string $text Text to reformat
156 * @param array $options Array can contain 'linked' and/or 'match-whole'
157 *
158 * @return string
159 */
160 public function reformat( $preference, $text, $options = [ 'linked' ] ) {
161 $linked = in_array( 'linked', $options );
162 $match_whole = in_array( 'match-whole', $options );
163
164 if ( isset( $this->preferences[$preference] ) ) {
165 $preference = $this->preferences[$preference];
166 } else {
167 $preference = self::NONE;
168 }
169 for ( $i = 1; $i <= self::LAST; $i++ ) {
170 $this->mSource = $i;
171 if ( isset( $this->rules[$preference][$i] ) ) {
172 # Specific rules
173 $this->mTarget = $this->rules[$preference][$i];
174 } elseif ( isset( $this->rules[self::ALL][$i] ) ) {
175 # General rules
176 $this->mTarget = $this->rules[self::ALL][$i];
177 } elseif ( $preference ) {
178 # User preference
179 $this->mTarget = $preference;
180 } else {
181 # Default
182 $this->mTarget = $i;
183 }
184 $regex = $this->regexes[$i];
185
186 // Horrible hack
187 if ( !$linked ) {
188 $regex = str_replace( [ '\[\[', '\]\]' ], '', $regex );
189 }
190
191 if ( $match_whole ) {
192 // Let's hope this works
193 $regex = preg_replace( '!^/!', '/^', $regex );
194 $regex = str_replace( $this->regexTrail,
195 '$' . $this->regexTrail, $regex );
196 }
197
198 // Another horrible hack
199 $this->mLinked = $linked;
200 $text = preg_replace_callback( $regex, [ $this, 'replace' ], $text );
201 unset( $this->mLinked );
202 }
203 return $text;
204 }
205
206 /**
207 * Regexp replacement callback
208 *
209 * @param array $matches
210 * @return string
211 */
212 private function replace( $matches ) {
213 # Extract information from $matches
214 $linked = true;
215 if ( isset( $this->mLinked ) ) {
216 $linked = $this->mLinked;
217 }
218
219 $bits = [];
220 $key = $this->keys[$this->mSource];
221 $keyLength = strlen( $key );
222 for ( $p = 0; $p < $keyLength; $p++ ) {
223 if ( $key[$p] != ' ' ) {
224 $bits[$key[$p]] = $matches[$p + 1];
225 }
226 }
227
228 return $this->formatDate( $bits, $matches[0], $linked );
229 }
230
231 /**
232 * @param array $bits
233 * @param string $orig Original input string, to be returned
234 * on formatting failure.
235 * @param bool $link
236 * @return string
237 */
238 private function formatDate( $bits, $orig, $link = true ) {
239 $format = $this->targets[$this->mTarget];
240
241 if ( !$link ) {
242 // strip piped links
243 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
244 // strip remaining links
245 $format = str_replace( [ '[[', ']]' ], '', $format );
246 }
247
248 # Construct new date
249 $text = '';
250 $fail = false;
251
252 // Pre-generate y/Y stuff because we need the year for the <span> title.
253 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) {
254 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
255 }
256 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
257 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
258 }
259
260 if ( !isset( $bits['m'] ) ) {
261 $m = $this->makeIsoMonth( $bits['F'] );
262 if ( !$m || $m == '00' ) {
263 $fail = true;
264 } else {
265 $bits['m'] = $m;
266 }
267 }
268
269 if ( !isset( $bits['d'] ) ) {
270 $bits['d'] = sprintf( '%02d', $bits['j'] );
271 }
272
273 $formatLength = strlen( $format );
274 for ( $p = 0; $p < $formatLength; $p++ ) {
275 $char = $format[$p];
276 switch ( $char ) {
277 case 'd': # ISO day of month
278 $text .= $bits['d'];
279 break;
280 case 'm': # ISO month
281 $text .= $bits['m'];
282 break;
283 case 'y': # ISO year
284 $text .= $bits['y'];
285 break;
286 case 'j': # ordinary day of month
287 if ( !isset( $bits['j'] ) ) {
288 $text .= intval( $bits['d'] );
289 } else {
290 $text .= $bits['j'];
291 }
292 break;
293 case 'F': # long month
294 if ( !isset( $bits['F'] ) ) {
295 $m = intval( $bits['m'] );
296 if ( $m > 12 || $m < 1 ) {
297 $fail = true;
298 } else {
299 $text .= $this->lang->getMonthName( $m );
300 }
301 } else {
302 $text .= ucfirst( $bits['F'] );
303 }
304 break;
305 case 'Y': # ordinary (optional BC) year
306 $text .= $bits['Y'];
307 break;
308 default:
309 $text .= $char;
310 }
311 }
312 if ( $fail ) {
313 // This occurs when parsing a date with day or month outside the bounds
314 // of possibilities.
315 $text = $orig;
316 }
317
318 $isoBits = [];
319 if ( isset( $bits['y'] ) ) {
320 $isoBits[] = $bits['y'];
321 }
322 $isoBits[] = $bits['m'];
323 $isoBits[] = $bits['d'];
324 $isoDate = implode( '-', $isoBits );
325
326 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
327 $text = Html::rawElement( 'span',
328 [ 'class' => 'mw-formatted-date', 'title' => $isoDate ], $text );
329
330 return $text;
331 }
332
333 /**
334 * Return a regex that can be used to find month names in string
335 * @return string regex to find the months with
336 */
337 private function getMonthRegex() {
338 $names = [];
339 for ( $i = 1; $i <= 12; $i++ ) {
340 $names[] = $this->lang->getMonthName( $i );
341 $names[] = $this->lang->getMonthAbbreviation( $i );
342 }
343 return implode( '|', $names );
344 }
345
346 /**
347 * Makes an ISO month, e.g. 02, from a month name
348 * @param string $monthName Month name
349 * @return string ISO month name
350 */
351 private function makeIsoMonth( $monthName ) {
352 $n = $this->xMonths[$this->lang->lc( $monthName )];
353 return sprintf( '%02d', $n );
354 }
355
356 /**
357 * Make an ISO year from a year name, for instance: '-1199' from '1200 BC'
358 * @param string $year Year name
359 * @return string ISO year name
360 */
361 private function makeIsoYear( $year ) {
362 # Assumes the year is in a nice format, as enforced by the regex
363 if ( substr( $year, -2 ) == 'BC' ) {
364 $num = intval( substr( $year, 0, -3 ) ) - 1;
365 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
366 $text = sprintf( '-%04d', $num );
367
368 } else {
369 $text = sprintf( '%04d', $year );
370 }
371 return $text;
372 }
373
374 /**
375 * Make a year one from an ISO year, for instance: '400 BC' from '-0399'.
376 * @param string $iso ISO year
377 * @return int|string int representing year number in case of AD dates, or string containing
378 * year number and 'BC' at the end otherwise.
379 */
380 private function makeNormalYear( $iso ) {
381 if ( $iso[0] == '-' ) {
382 $text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC';
383 } else {
384 $text = intval( $iso );
385 }
386 return $text;
387 }
388 }