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