More return documentation
[lhc/web/wiklou.git] / includes / parser / DateFormatter.php
1 <?php
2 /**
3 * Date formatter
4 *
5 * @file
6 */
7
8 /**
9 * Date formatter, recognises dates in plain text and formats them accoding to user preferences.
10 * @todo preferences, OutputPage
11 * @ingroup Parser
12 */
13 class DateFormatter
14 {
15 var $mSource, $mTarget;
16 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
17
18 var $regexes, $pDays, $pMonths, $pYears;
19 var $rules, $xMonths, $preferences;
20
21 const ALL = -1;
22 const NONE = 0;
23 const MDY = 1;
24 const DMY = 2;
25 const YMD = 3;
26 const ISO1 = 4;
27 const LASTPREF = 4;
28 const ISO2 = 5;
29 const YDM = 6;
30 const DM = 7;
31 const MD = 8;
32 const LAST = 8;
33
34 /**
35 * @todo document
36 */
37 function __construct() {
38 global $wgContLang;
39
40 $this->monthNames = $this->getMonthRegex();
41 for ( $i=1; $i<=12; $i++ ) {
42 $this->xMonths[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
43 $this->xMonths[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
44 }
45
46 $this->regexTrail = '(?![a-z])/iu';
47
48 # Partial regular expressions
49 $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')\]\]';
50 $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})\]\]';
51 $this->prxY = '\[\[(\d{1,4}([ _]BC|))\]\]';
52 $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
53 $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
54
55 # Real regular expressions
56 $this->regexes[self::DMY] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
57 $this->regexes[self::YDM] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
58 $this->regexes[self::MDY] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
59 $this->regexes[self::YMD] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
60 $this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
61 $this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
62 $this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
63 $this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
64
65 # Extraction keys
66 # See the comments in replace() for the meaning of the letters
67 $this->keys[self::DMY] = 'jFY';
68 $this->keys[self::YDM] = 'Y jF';
69 $this->keys[self::MDY] = 'FjY';
70 $this->keys[self::YMD] = 'Y Fj';
71 $this->keys[self::DM] = 'jF';
72 $this->keys[self::MD] = 'Fj';
73 $this->keys[self::ISO1] = 'ymd'; # y means ISO year
74 $this->keys[self::ISO2] = 'ymd';
75
76 # Target date formats
77 $this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
78 $this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
79 $this->targets[self::MDY] = '[[F j]], [[Y]]';
80 $this->targets[self::YMD] = '[[Y]] [[F j]]';
81 $this->targets[self::DM] = '[[F j|j F]]';
82 $this->targets[self::MD] = '[[F j]]';
83 $this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
84 $this->targets[self::ISO2] = '[[y-m-d]]';
85
86 # Rules
87 # pref source target
88 $this->rules[self::DMY][self::MD] = self::DM;
89 $this->rules[self::ALL][self::MD] = self::MD;
90 $this->rules[self::MDY][self::DM] = self::MD;
91 $this->rules[self::ALL][self::DM] = self::DM;
92 $this->rules[self::NONE][self::ISO2] = self::ISO1;
93
94 $this->preferences = array(
95 'default' => self::NONE,
96 'dmy' => self::DMY,
97 'mdy' => self::MDY,
98 'ymd' => self::YMD,
99 'ISO 8601' => self::ISO1,
100 );
101 }
102
103 /**
104 * Get a DateFormatter object
105 *
106 * @return DateFormatter object
107 */
108 public static function &getInstance() {
109 global $wgMemc;
110 static $dateFormatter = false;
111 if ( !$dateFormatter ) {
112 $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
113 if ( !$dateFormatter ) {
114 $dateFormatter = new DateFormatter;
115 $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
116 }
117 }
118 return $dateFormatter;
119 }
120
121 /**
122 * @param $preference String: User preference
123 * @param $text String: Text to reformat
124 * @param $options Array: can contain 'linked' and/or 'match-whole'
125 * @return mixed|String
126 */
127 function reformat( $preference, $text, $options = array('linked') ) {
128
129 $linked = in_array( 'linked', $options );
130 $match_whole = in_array( 'match-whole', $options );
131
132 if ( isset( $this->preferences[$preference] ) ) {
133 $preference = $this->preferences[$preference];
134 } else {
135 $preference = self::NONE;
136 }
137 for ( $i=1; $i<=self::LAST; $i++ ) {
138 $this->mSource = $i;
139 if ( isset ( $this->rules[$preference][$i] ) ) {
140 # Specific rules
141 $this->mTarget = $this->rules[$preference][$i];
142 } elseif ( isset ( $this->rules[self::ALL][$i] ) ) {
143 # General rules
144 $this->mTarget = $this->rules[self::ALL][$i];
145 } elseif ( $preference ) {
146 # User preference
147 $this->mTarget = $preference;
148 } else {
149 # Default
150 $this->mTarget = $i;
151 }
152 $regex = $this->regexes[$i];
153
154 // Horrible hack
155 if (!$linked) {
156 $regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
157 }
158
159 if ($match_whole) {
160 // Let's hope this works
161 $regex = preg_replace( '!^/!', '/^', $regex );
162 $regex = str_replace( $this->regexTrail,
163 '$'.$this->regexTrail, $regex );
164 }
165
166 // Another horrible hack
167 $this->mLinked = $linked;
168 $text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
169 unset($this->mLinked);
170 }
171 return $text;
172 }
173
174 /**
175 * @param $matches
176 * @return string
177 */
178 function replace( $matches ) {
179 # Extract information from $matches
180 $linked = true;
181 if ( isset( $this->mLinked ) )
182 $linked = $this->mLinked;
183
184 $bits = array();
185 $key = $this->keys[$this->mSource];
186 for ( $p=0; $p < strlen($key); $p++ ) {
187 if ( $key[$p] != ' ' ) {
188 $bits[$key[$p]] = $matches[$p+1];
189 }
190 }
191
192 return $this->formatDate( $bits, $linked );
193 }
194
195 function formatDate( $bits, $link = true ) {
196 $format = $this->targets[$this->mTarget];
197
198 if (!$link) {
199 // strip piped links
200 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
201 // strip remaining links
202 $format = str_replace( array( '[[', ']]' ), '', $format );
203 }
204
205 # Construct new date
206 $text = '';
207 $fail = false;
208
209 // Pre-generate y/Y stuff because we need the year for the <span> title.
210 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) )
211 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
212 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) )
213 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
214
215 if ( !isset( $bits['m'] ) ) {
216 $m = $this->makeIsoMonth( $bits['F'] );
217 if ( !$m || $m == '00' ) {
218 $fail = true;
219 } else {
220 $bits['m'] = $m;
221 }
222 }
223
224 if ( !isset($bits['d']) ) {
225 $bits['d'] = sprintf( '%02d', $bits['j'] );
226 }
227
228 for ( $p=0; $p < strlen( $format ); $p++ ) {
229 $char = $format[$p];
230 switch ( $char ) {
231 case 'd': # ISO day of month
232 $text .= $bits['d'];
233 break;
234 case 'm': # ISO month
235 $text .= $bits['m'];
236 break;
237 case 'y': # ISO year
238 $text .= $bits['y'];
239 break;
240 case 'j': # ordinary day of month
241 if ( !isset($bits['j']) ) {
242 $text .= intval( $bits['d'] );
243 } else {
244 $text .= $bits['j'];
245 }
246 break;
247 case 'F': # long month
248 if ( !isset( $bits['F'] ) ) {
249 $m = intval($bits['m']);
250 if ( $m > 12 || $m < 1 ) {
251 $fail = true;
252 } else {
253 global $wgContLang;
254 $text .= $wgContLang->getMonthName( $m );
255 }
256 } else {
257 $text .= ucfirst( $bits['F'] );
258 }
259 break;
260 case 'Y': # ordinary (optional BC) year
261 $text .= $bits['Y'];
262 break;
263 default:
264 $text .= $char;
265 }
266 }
267 if ( $fail ) {
268 $text = $matches[0];
269 }
270
271 $isoBits = array();
272 if ( isset($bits['y']) )
273 $isoBits[] = $bits['y'];
274 $isoBits[] = $bits['m'];
275 $isoBits[] = $bits['d'];
276 $isoDate = implode( '-', $isoBits );
277
278 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
279 $text = Html::rawElement( 'span',
280 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
281
282 return $text;
283 }
284
285 /**
286 * @todo document
287 * @return string
288 */
289 function getMonthRegex() {
290 global $wgContLang;
291 $names = array();
292 for( $i = 1; $i <= 12; $i++ ) {
293 $names[] = $wgContLang->getMonthName( $i );
294 $names[] = $wgContLang->getMonthAbbreviation( $i );
295 }
296 return implode( '|', $names );
297 }
298
299 /**
300 * Makes an ISO month, e.g. 02, from a month name
301 * @param $monthName String: month name
302 * @return string ISO month name
303 */
304 function makeIsoMonth( $monthName ) {
305 global $wgContLang;
306
307 $n = $this->xMonths[$wgContLang->lc( $monthName )];
308 return sprintf( '%02d', $n );
309 }
310
311 /**
312 * @todo document
313 * @param $year String: Year name
314 * @return string ISO year name
315 */
316 function makeIsoYear( $year ) {
317 # Assumes the year is in a nice format, as enforced by the regex
318 if ( substr( $year, -2 ) == 'BC' ) {
319 $num = intval(substr( $year, 0, -3 )) - 1;
320 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
321 $text = sprintf( '-%04d', $num );
322
323 } else {
324 $text = sprintf( '%04d', $year );
325 }
326 return $text;
327 }
328
329 /**
330 * @todo document
331 * @return int|string
332 */
333 function makeNormalYear( $iso ) {
334 if ( $iso[0] == '-' ) {
335 $text = (intval( substr( $iso, 1 ) ) + 1) . ' BC';
336 } else {
337 $text = intval( $iso );
338 }
339 return $text;
340 }
341 }