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