Add the other existing $skin.css/.js to the message files too to be consistent
[lhc/web/wiklou.git] / includes / 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 * @static
100 */
101 function &getInstance() {
102 global $wgMemc;
103 static $dateFormatter = false;
104 if ( !$dateFormatter ) {
105 $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
106 if ( !$dateFormatter ) {
107 $dateFormatter = new DateFormatter;
108 $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
109 }
110 }
111 return $dateFormatter;
112 }
113
114 /**
115 * @param string $preference User preference
116 * @param string $text Text to reformat
117 */
118 function reformat( $preference, $text ) {
119 if ( isset( $this->preferences[$preference] ) ) {
120 $preference = $this->preferences[$preference];
121 } else {
122 $preference = self::NONE;
123 }
124 for ( $i=1; $i<=self::LAST; $i++ ) {
125 $this->mSource = $i;
126 if ( isset ( $this->rules[$preference][$i] ) ) {
127 # Specific rules
128 $this->mTarget = $this->rules[$preference][$i];
129 } elseif ( isset ( $this->rules[self::ALL][$i] ) ) {
130 # General rules
131 $this->mTarget = $this->rules[self::ALL][$i];
132 } elseif ( $preference ) {
133 # User preference
134 $this->mTarget = $preference;
135 } else {
136 # Default
137 $this->mTarget = $i;
138 }
139 $text = preg_replace_callback( $this->regexes[$i], array( &$this, 'replace' ), $text );
140 }
141 return $text;
142 }
143
144 /**
145 * @param $matches
146 */
147 function replace( $matches ) {
148 # Extract information from $matches
149 $bits = array();
150 $key = $this->keys[$this->mSource];
151 for ( $p=0; $p < strlen($key); $p++ ) {
152 if ( $key{$p} != ' ' ) {
153 $bits[$key{$p}] = $matches[$p+1];
154 }
155 }
156
157 $format = $this->targets[$this->mTarget];
158
159 # Construct new date
160 $text = '';
161 $fail = false;
162
163 for ( $p=0; $p < strlen( $format ); $p++ ) {
164 $char = $format{$p};
165 switch ( $char ) {
166 case 'd': # ISO day of month
167 if ( !isset($bits['d']) ) {
168 $text .= sprintf( '%02d', $bits['j'] );
169 } else {
170 $text .= $bits['d'];
171 }
172 break;
173 case 'm': # ISO month
174 if ( !isset($bits['m']) ) {
175 $m = $this->makeIsoMonth( $bits['F'] );
176 if ( !$m || $m == '00' ) {
177 $fail = true;
178 } else {
179 $text .= $m;
180 }
181 } else {
182 $text .= $bits['m'];
183 }
184 break;
185 case 'y': # ISO year
186 if ( !isset( $bits['y'] ) ) {
187 $text .= $this->makeIsoYear( $bits['Y'] );
188 } else {
189 $text .= $bits['y'];
190 }
191 break;
192 case 'j': # ordinary day of month
193 if ( !isset($bits['j']) ) {
194 $text .= intval( $bits['d'] );
195 } else {
196 $text .= $bits['j'];
197 }
198 break;
199 case 'F': # long month
200 if ( !isset( $bits['F'] ) ) {
201 $m = intval($bits['m']);
202 if ( $m > 12 || $m < 1 ) {
203 $fail = true;
204 } else {
205 global $wgContLang;
206 $text .= $wgContLang->getMonthName( $m );
207 }
208 } else {
209 $text .= ucfirst( $bits['F'] );
210 }
211 break;
212 case 'Y': # ordinary (optional BC) year
213 if ( !isset( $bits['Y'] ) ) {
214 $text .= $this->makeNormalYear( $bits['y'] );
215 } else {
216 $text .= $bits['Y'];
217 }
218 break;
219 default:
220 $text .= $char;
221 }
222 }
223 if ( $fail ) {
224 $text = $matches[0];
225 }
226 return $text;
227 }
228
229 /**
230 * @todo document
231 */
232 function getMonthRegex() {
233 global $wgContLang;
234 $names = array();
235 for( $i = 1; $i <= 12; $i++ ) {
236 $names[] = $wgContLang->getMonthName( $i );
237 $names[] = $wgContLang->getMonthAbbreviation( $i );
238 }
239 return implode( '|', $names );
240 }
241
242 /**
243 * Makes an ISO month, e.g. 02, from a month name
244 * @param $monthName String: month name
245 * @return string ISO month name
246 */
247 function makeIsoMonth( $monthName ) {
248 global $wgContLang;
249
250 $n = $this->xMonths[$wgContLang->lc( $monthName )];
251 return sprintf( '%02d', $n );
252 }
253
254 /**
255 * @todo document
256 * @param $year String: Year name
257 * @return string ISO year name
258 */
259 function makeIsoYear( $year ) {
260 # Assumes the year is in a nice format, as enforced by the regex
261 if ( substr( $year, -2 ) == 'BC' ) {
262 $num = intval(substr( $year, 0, -3 )) - 1;
263 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
264 $text = sprintf( '-%04d', $num );
265
266 } else {
267 $text = sprintf( '%04d', $year );
268 }
269 return $text;
270 }
271
272 /**
273 * @todo document
274 */
275 function makeNormalYear( $iso ) {
276 if ( $iso{0} == '-' ) {
277 $text = (intval( substr( $iso, 1 ) ) + 1) . ' BC';
278 } else {
279 $text = intval( $iso );
280 }
281 return $text;
282 }
283 }