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