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