summaryrefslogtreecommitdiff
path: root/includes/parser/DateFormatter.php
blob: cf5101712ab0f650a1c8f1bc3f04d6607619dbbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
 * Date formatter
 *
 * @file
 */

/**
 * Date formatter, recognises dates in plain text and formats them accoding to user preferences.
 * @todo preferences, OutputPage
 * @ingroup Parser
 */
class DateFormatter
{
	var $mSource, $mTarget;
	var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;

	var $regexes, $pDays, $pMonths, $pYears;
	var $rules, $xMonths, $preferences;

	const ALL = -1;
	const NONE = 0;
	const MDY = 1;
	const DMY = 2;
	const YMD = 3;
	const ISO1 = 4;
	const LASTPREF = 4;
	const ISO2 = 5;
	const YDM = 6;
	const DM = 7;
	const MD = 8;
	const LAST = 8;

	/**
	 * @todo document
	 */
	function __construct() {
		global $wgContLang;

		$this->monthNames = $this->getMonthRegex();
		for ( $i=1; $i<=12; $i++ ) {
			$this->xMonths[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
			$this->xMonths[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
		}

		$this->regexTrail = '(?![a-z])/iu';

		# Partial regular expressions
		$this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')\]\]';
		$this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})\]\]';
		$this->prxY = '\[\[(\d{1,4}([ _]BC|))\]\]';
		$this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
		$this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';

		# Real regular expressions
		$this->regexes[self::DMY] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
		$this->regexes[self::YDM] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
		$this->regexes[self::MDY] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
		$this->regexes[self::YMD] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
		$this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
		$this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
		$this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
		$this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";

		# Extraction keys
		# See the comments in replace() for the meaning of the letters
		$this->keys[self::DMY] = 'jFY';
		$this->keys[self::YDM] = 'Y jF';
		$this->keys[self::MDY] = 'FjY';
		$this->keys[self::YMD] = 'Y Fj';
		$this->keys[self::DM] = 'jF';
		$this->keys[self::MD] = 'Fj';
		$this->keys[self::ISO1] = 'ymd'; # y means ISO year
		$this->keys[self::ISO2] = 'ymd';

		# Target date formats
		$this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
		$this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
		$this->targets[self::MDY] = '[[F j]], [[Y]]';
		$this->targets[self::YMD] = '[[Y]] [[F j]]';
		$this->targets[self::DM] = '[[F j|j F]]';
		$this->targets[self::MD] = '[[F j]]';
		$this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
		$this->targets[self::ISO2] = '[[y-m-d]]';

		# Rules
		#            pref    source 	  target
		$this->rules[self::DMY][self::MD] 	= self::DM;
		$this->rules[self::ALL][self::MD] 	= self::MD;
		$this->rules[self::MDY][self::DM] 	= self::MD;
		$this->rules[self::ALL][self::DM] 	= self::DM;
		$this->rules[self::NONE][self::ISO2] 	= self::ISO1;

		$this->preferences = array(
			'default' => self::NONE,
			'dmy' => self::DMY,
			'mdy' => self::MDY,
			'ymd' => self::YMD,
			'ISO 8601' => self::ISO1,
		);
	}

	/**
	 * Get a DateFormatter object
	 *
	 * @return DateFormatter object
	 */
	public static function &getInstance() {
		global $wgMemc;
		static $dateFormatter = false;
		if ( !$dateFormatter ) {
			$dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
			if ( !$dateFormatter ) {
				$dateFormatter = new DateFormatter;
				$wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
			}
		}
		return $dateFormatter;
	}

	/**
	 * @param $preference String: User preference
	 * @param $text String: Text to reformat
	 * @param $options Array: can contain 'linked' and/or 'match-whole'
	 */
	function reformat( $preference, $text, $options = array('linked') ) {
	
		$linked = in_array( 'linked', $options );
		$match_whole = in_array( 'match-whole', $options );
		
		if ( isset( $this->preferences[$preference] ) ) {
			$preference = $this->preferences[$preference];
		} else {
			$preference = self::NONE;
		}
		for ( $i=1; $i<=self::LAST; $i++ ) {
			$this->mSource = $i;
			if ( isset ( $this->rules[$preference][$i] ) ) {
				# Specific rules
				$this->mTarget = $this->rules[$preference][$i];
			} elseif ( isset ( $this->rules[self::ALL][$i] ) ) {
				# General rules
				$this->mTarget = $this->rules[self::ALL][$i];
			} elseif ( $preference ) {
				# User preference
				$this->mTarget = $preference;
			} else {
				# Default
				$this->mTarget = $i;
			}
			$regex = $this->regexes[$i];
			
			// Horrible hack
			if (!$linked) {
				$regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
			}
			
			if ($match_whole) {
				// Let's hope this works
				$regex = preg_replace( '!^/!', '/^', $regex );
				$regex = str_replace( $this->regexTrail,
					'$'.$this->regexTrail, $regex );
			}
			
			// Another horrible hack
			$this->mLinked = $linked;
			$text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
			unset($this->mLinked);
		}
		return $text;
	}

	/**
	 * @param $matches
	 */
	function replace( $matches ) {
		# Extract information from $matches
		$linked = true;
		if ( isset( $this->mLinked ) )
			$linked = $this->mLinked;
		
		$bits = array();
		$key = $this->keys[$this->mSource];
		for ( $p=0; $p < strlen($key); $p++ ) {
			if ( $key{$p} != ' ' ) {
				$bits[$key{$p}] = $matches[$p+1];
			}
		}
		
		return $this->formatDate( $bits, $linked );
	}
	
	function formatDate( $bits, $link = true ) {
		$format = $this->targets[$this->mTarget];
		
		if (!$link) {
			// strip piped links
			$format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
			// strip remaining links
			$format = str_replace( array( '[[', ']]' ), '', $format );
		}

		# Construct new date
		$text = '';
		$fail = false;
		
		// Pre-generate y/Y stuff because we need the year for the <span> title.
		if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) )
			$bits['y'] = $this->makeIsoYear( $bits['Y'] );
		if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) )
			$bits['Y'] = $this->makeNormalYear( $bits['y'] );
			
		if ( !isset( $bits['m'] ) ) {
			$m = $this->makeIsoMonth( $bits['F'] );
			if ( !$m || $m == '00' ) {
				$fail = true;
			} else {
				$bits['m'] = $m;
			}
		}
		
		if ( !isset($bits['d']) ) {
			$bits['d'] = sprintf( '%02d', $bits['j'] );
		}

		for ( $p=0; $p < strlen( $format ); $p++ ) {
			$char = $format{$p};
			switch ( $char ) {
				case 'd': # ISO day of month
					$text .= $bits['d'];
					break;
				case 'm': # ISO month
					$text .= $bits['m'];
					break;
				case 'y': # ISO year
					$text .= $bits['y'];
					break;
				case 'j': # ordinary day of month
					if ( !isset($bits['j']) ) {
						$text .= intval( $bits['d'] );
					} else {
						$text .= $bits['j'];
					}
					break;
				case 'F': # long month
					if ( !isset( $bits['F'] ) ) {
						$m = intval($bits['m']);
						if ( $m > 12 || $m < 1 ) {
							$fail = true;
						} else {
							global $wgContLang;
							$text .= $wgContLang->getMonthName( $m );
						}
					} else {
						$text .= ucfirst( $bits['F'] );
					}
					break;
				case 'Y': # ordinary (optional BC) year
					$text .= $bits['Y'];
					break;
				default:
					$text .= $char;
			}
		}
		if ( $fail ) {
			$text = $matches[0];
		}
		
		$isoBits = array();
		if ( isset($bits['y']) )
			$isoBits[] = $bits['y'];
		$isoBits[] = $bits['m'];
		$isoBits[] = $bits['d'];
		$isoDate = implode( '-', $isoBits );
		
		// Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
		$text = Html::rawElement( 'span',
					array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
		
		return $text;
	}

	/**
	 * @todo document
	 */
	function getMonthRegex() {
		global $wgContLang;
		$names = array();
		for( $i = 1; $i <= 12; $i++ ) {
			$names[] = $wgContLang->getMonthName( $i );
			$names[] = $wgContLang->getMonthAbbreviation( $i );
		}
		return implode( '|', $names );
	}

	/**
	 * Makes an ISO month, e.g. 02, from a month name
	 * @param $monthName String: month name
	 * @return string ISO month name
	 */
	function makeIsoMonth( $monthName ) {
		global $wgContLang;

		$n = $this->xMonths[$wgContLang->lc( $monthName )];
		return sprintf( '%02d', $n );
	}

	/**
	 * @todo document
	 * @param $year String: Year name
	 * @return string ISO year name
	 */
	function makeIsoYear( $year ) {
		# Assumes the year is in a nice format, as enforced by the regex
		if ( substr( $year, -2 ) == 'BC' ) {
			$num = intval(substr( $year, 0, -3 )) - 1;
			# PHP bug note: sprintf( "%04d", -1 ) fails poorly
			$text = sprintf( '-%04d', $num );

		} else {
			$text = sprintf( '%04d', $year );
		}
		return $text;
	}

	/**
	 * @todo document
	 */
	function makeNormalYear( $iso ) {
		if ( $iso{0} == '-' ) {
			$text = (intval( substr( $iso, 1 ) ) + 1) . ' BC';
		} else {
			$text = intval( $iso );
		}
		return $text;
	}
}