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