* Not showing " : " in recentchanges if the rcComment is empty
[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 define('DF_NONE', 0);
12 define('DF_MDY', 1);
13 define('DF_DMY', 2);
14 define('DF_YMD', 3);
15 define('DF_ISO1', 4);
16 define('DF_LASTPREF', 4);
17 define('DF_ISO2', 5);
18 define('DF_YDM', 6);
19 define('DF_DM', 7);
20 define('DF_MD', 8);
21 define('DF_LAST', 8);
22
23 /**
24 * @todo preferences, OutputPage
25 * @package MediaWiki
26 * @subpackage Parser
27 */
28 class DateFormatter
29 {
30 var $mSource, $mTarget;
31 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
32
33 var $regexes, $pDays, $pMonths, $pYears;
34 var $rules, $xMonths;
35
36 /**
37 * @todo document
38 */
39 function DateFormatter() {
40 global $wgContLang, $wgInputEncoding;
41
42 $this->monthNames = $this->getMonthRegex();
43 for ( $i=1; $i<=12; $i++ ) {
44 $this->xMonths[strtolower( $wgContLang->getMonthName( $i ) )] = $i;
45 }
46 for ( $i=1; $i<=12; $i++ ) {
47 $this->xMonths[strtolower( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
48 }
49
50 # Attempt at UTF-8 support, untested at the moment
51 if ( $wgInputEncoding == 'UTF-8' ) {
52 $this->regexTrail = '(?![a-z])/iu';
53 } else {
54 $this->regexTrail = '(?![a-z])/i';
55 }
56
57 # Partial regular expressions
58 $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')]]';
59 $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})]]';
60 $this->prxY = '\[\[(\d{1,4}([ _]BC|))]]';
61 $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
62 $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
63
64 # Real regular expressions
65 $this->regexes[DF_DMY] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
66 $this->regexes[DF_YDM] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
67 $this->regexes[DF_MDY] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
68 $this->regexes[DF_YMD] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
69 $this->regexes[DF_DM] = "/{$this->prxDM}{$this->regexTrail}";
70 $this->regexes[DF_MD] = "/{$this->prxMD}{$this->regexTrail}";
71 $this->regexes[DF_ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
72 $this->regexes[DF_ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
73
74 # Extraction keys
75 # See the comments in replace() for the meaning of the letters
76 $this->keys[DF_DMY] = 'jFY';
77 $this->keys[DF_YDM] = 'Y jF';
78 $this->keys[DF_MDY] = 'FjY';
79 $this->keys[DF_YMD] = 'Y Fj';
80 $this->keys[DF_DM] = 'jF';
81 $this->keys[DF_MD] = 'Fj';
82 $this->keys[DF_ISO1] = 'ymd'; # y means ISO year
83 $this->keys[DF_ISO2] = 'ymd';
84
85 # Target date formats
86 $this->targets[DF_DMY] = '[[F j|j F]] [[Y]]';
87 $this->targets[DF_YDM] = '[[Y]], [[F j|j F]]';
88 $this->targets[DF_MDY] = '[[F j]], [[Y]]';
89 $this->targets[DF_YMD] = '[[Y]] [[F j]]';
90 $this->targets[DF_DM] = '[[F j|j F]]';
91 $this->targets[DF_MD] = '[[F j]]';
92 $this->targets[DF_ISO1] = '[[Y|y]]-[[F j|m-d]]';
93 $this->targets[DF_ISO2] = '[[y-m-d]]';
94
95 # Rules
96 # pref source target
97 $this->rules[DF_DMY][DF_MD] = DF_DM;
98 $this->rules[DF_ALL][DF_MD] = DF_MD;
99 $this->rules[DF_MDY][DF_DM] = DF_MD;
100 $this->rules[DF_ALL][DF_DM] = DF_DM;
101 $this->rules[DF_NONE][DF_ISO2] = DF_ISO1;
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 $preference
122 * @param $text
123 */
124 function reformat( $preference, $text ) {
125 if ($preference == 'ISO 8601') $preference = 4; # The ISO 8601 option used to be 4
126 for ( $i=1; $i<=DF_LAST; $i++ ) {
127 $this->mSource = $i;
128 if ( @$this->rules[$preference][$i] ) {
129 # Specific rules
130 $this->mTarget = $this->rules[$preference][$i];
131 } elseif ( @$this->rules[DF_ALL][$i] ) {
132 # General rules
133 $this->mTarget = $this->rules[DF_ALL][$i];
134 } elseif ( $preference ) {
135 # User preference
136 $this->mTarget = $preference;
137 } else {
138 # Default
139 $this->mTarget = $i;
140 }
141 $text = preg_replace_callback( $this->regexes[$i], 'wfMainDateReplace', $text );
142 }
143 return $text;
144 }
145
146 /**
147 * @param $matches
148 */
149 function replace( $matches ) {
150 # Extract information from $matches
151 $bits = array();
152 $key = $this->keys[$this->mSource];
153 for ( $p=0; $p < strlen($key); $p++ ) {
154 if ( $key{$p} != ' ' ) {
155 $bits[$key{$p}] = $matches[$p+1];
156 }
157 }
158
159 $format = $this->targets[$this->mTarget];
160
161 # Construct new date
162 $text = '';
163 $fail = false;
164
165 for ( $p=0; $p < strlen( $format ); $p++ ) {
166 $char = $format{$p};
167 switch ( $char ) {
168 case 'd': # ISO day of month
169 if ( !isset($bits['d']) ) {
170 $text .= sprintf( '%02d', $bits['j'] );
171 } else {
172 $text .= $bits['d'];
173 }
174 break;
175 case 'm': # ISO month
176 if ( !isset($bits['m']) ) {
177 $m = $this->makeIsoMonth( $bits['F'] );
178 if ( !$m || $m == '00' ) {
179 $fail = true;
180 } else {
181 $text .= $m;
182 }
183 } else {
184 $text .= $bits['m'];
185 }
186 break;
187 case 'y': # ISO year
188 if ( !isset( $bits['y'] ) ) {
189 $text .= $this->makeIsoYear( $bits['Y'] );
190 } else {
191 $text .= $bits['y'];
192 }
193 break;
194 case 'j': # ordinary day of month
195 if ( !isset($bits['j']) ) {
196 $text .= IntVal( $bits['d'] );
197 } else {
198 $text .= $bits['j'];
199 }
200 break;
201 case 'F': # long month
202 if ( !isset( $bits['F'] ) ) {
203 $m = IntVal($bits['m']);
204 if ( $m > 12 || $m < 1 ) {
205 $fail = true;
206 } else {
207 global $wgContLang;
208 $text .= $wgContLang->getMonthName( $m );
209 }
210 } else {
211 $text .= ucfirst( $bits['F'] );
212 }
213 break;
214 case 'Y': # ordinary (optional BC) year
215 if ( !isset( $bits['Y'] ) ) {
216 $text .= $this->makeNormalYear( $bits['y'] );
217 } else {
218 $text .= $bits['Y'];
219 }
220 break;
221 default:
222 $text .= $char;
223 }
224 }
225 if ( $fail ) {
226 $text = $matches[0];
227 }
228 return $text;
229 }
230
231 /**
232 * @todo document
233 */
234 function getMonthRegex() {
235 global $wgContLang;
236 $names = array();
237 for( $i = 1; $i <= 12; $i++ ) {
238 $names[] = $wgContLang->getMonthName( $i );
239 $names[] = $wgContLang->getMonthAbbreviation( $i );
240 }
241 return implode( '|', $names );
242 }
243
244 /**
245 * Makes an ISO month, e.g. 02, from a month name
246 * @param string $monthName Month name
247 * @return string ISO month name
248 */
249 function makeIsoMonth( $monthName ) {
250 $n = $this->xMonths[strtolower( $monthName )];
251 return sprintf( '%02d', $n );
252 }
253
254 /**
255 * @todo document
256 * @param string $year 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 }
284
285 /**
286 * @todo document
287 */
288 function wfMainDateReplace( $matches ) {
289 $df =& DateFormatter::getInstance();
290 return $df->replace( $matches );
291 }
292
293 ?>