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