4 * Date formatter, recognises dates in plain text and formats them accoding to user preferences.
5 * @todo preferences, OutputPage
10 var $mSource, $mTarget;
11 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
13 var $regexes, $pDays, $pMonths, $pYears;
14 var $rules, $xMonths, $preferences;
32 function DateFormatter() {
35 $this->monthNames
= $this->getMonthRegex();
36 for ( $i=1; $i<=12; $i++
) {
37 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
38 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
41 $this->regexTrail
= '(?![a-z])/iu';
43 # Partial regular expressions
44 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
45 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
46 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
47 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
48 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
50 # Real regular expressions
51 $this->regexes
[self
::DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
52 $this->regexes
[self
::YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
53 $this->regexes
[self
::MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
54 $this->regexes
[self
::YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
55 $this->regexes
[self
::DM
] = "/{$this->prxDM}{$this->regexTrail}";
56 $this->regexes
[self
::MD
] = "/{$this->prxMD}{$this->regexTrail}";
57 $this->regexes
[self
::ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
58 $this->regexes
[self
::ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
61 # See the comments in replace() for the meaning of the letters
62 $this->keys
[self
::DMY
] = 'jFY';
63 $this->keys
[self
::YDM
] = 'Y jF';
64 $this->keys
[self
::MDY
] = 'FjY';
65 $this->keys
[self
::YMD
] = 'Y Fj';
66 $this->keys
[self
::DM
] = 'jF';
67 $this->keys
[self
::MD
] = 'Fj';
68 $this->keys
[self
::ISO1
] = 'ymd'; # y means ISO year
69 $this->keys
[self
::ISO2
] = 'ymd';
72 $this->targets
[self
::DMY
] = '[[F j|j F]] [[Y]]';
73 $this->targets
[self
::YDM
] = '[[Y]], [[F j|j F]]';
74 $this->targets
[self
::MDY
] = '[[F j]], [[Y]]';
75 $this->targets
[self
::YMD
] = '[[Y]] [[F j]]';
76 $this->targets
[self
::DM
] = '[[F j|j F]]';
77 $this->targets
[self
::MD
] = '[[F j]]';
78 $this->targets
[self
::ISO1
] = '[[Y|y]]-[[F j|m-d]]';
79 $this->targets
[self
::ISO2
] = '[[y-m-d]]';
83 $this->rules
[self
::DMY
][self
::MD
] = self
::DM
;
84 $this->rules
[self
::ALL
][self
::MD
] = self
::MD
;
85 $this->rules
[self
::MDY
][self
::DM
] = self
::MD
;
86 $this->rules
[self
::ALL
][self
::DM
] = self
::DM
;
87 $this->rules
[self
::NONE
][self
::ISO2
] = self
::ISO1
;
89 $this->preferences
= array(
90 'default' => self
::NONE
,
94 'ISO 8601' => self
::ISO1
,
99 * Get a DateFormatter object
101 * @return DateFormatter object
103 public static function &getInstance() {
105 static $dateFormatter = false;
106 if ( !$dateFormatter ) {
107 $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
108 if ( !$dateFormatter ) {
109 $dateFormatter = new DateFormatter
;
110 $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
113 return $dateFormatter;
117 * @param $preference String: User preference
118 * @param $text String: Text to reformat
120 function reformat( $preference, $text ) {
121 if ( isset( $this->preferences
[$preference] ) ) {
122 $preference = $this->preferences
[$preference];
124 $preference = self
::NONE
;
126 for ( $i=1; $i<=self
::LAST
; $i++
) {
128 if ( isset ( $this->rules
[$preference][$i] ) ) {
130 $this->mTarget
= $this->rules
[$preference][$i];
131 } elseif ( isset ( $this->rules
[self
::ALL
][$i] ) ) {
133 $this->mTarget
= $this->rules
[self
::ALL
][$i];
134 } elseif ( $preference ) {
136 $this->mTarget
= $preference;
141 $text = preg_replace_callback( $this->regexes
[$i], array( &$this, 'replace' ), $text );
149 function replace( $matches ) {
150 # Extract information from $matches
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];
159 return $this->formatDate( $bits );
162 function formatDate( $bits ) {
163 $format = $this->targets
[$this->mTarget
];
169 // Pre-generate y/Y stuff because we need the year for the <span> title.
170 if ( !isset( $bits['y'] ) )
171 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
172 if ( !isset( $bits['Y'] ) )
173 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
175 for ( $p=0; $p < strlen( $format ); $p++
) {
178 case 'd': # ISO day of month
179 if ( !isset($bits['d']) ) {
180 $text .= sprintf( '%02d', $bits['j'] );
185 case 'm': # ISO month
186 if ( !isset($bits['m']) ) {
187 $m = $this->makeIsoMonth( $bits['F'] );
188 if ( !$m ||
$m == '00' ) {
200 case 'j': # ordinary day of month
201 if ( !isset($bits['j']) ) {
202 $text .= intval( $bits['d'] );
207 case 'F': # long month
208 if ( !isset( $bits['F'] ) ) {
209 $m = intval($bits['m']);
210 if ( $m > 12 ||
$m < 1 ) {
214 $text .= $wgContLang->getMonthName( $m );
217 $text .= ucfirst( $bits['F'] );
220 case 'Y': # ordinary (optional BC) year
231 $isoDate = $bits['y'].$bits['m'].$bits['d'];
233 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
234 $text = Xml
::tags( 'span',
235 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
243 function getMonthRegex() {
246 for( $i = 1; $i <= 12; $i++
) {
247 $names[] = $wgContLang->getMonthName( $i );
248 $names[] = $wgContLang->getMonthAbbreviation( $i );
250 return implode( '|', $names );
254 * Makes an ISO month, e.g. 02, from a month name
255 * @param $monthName String: month name
256 * @return string ISO month name
258 function makeIsoMonth( $monthName ) {
261 $n = $this->xMonths
[$wgContLang->lc( $monthName )];
262 return sprintf( '%02d', $n );
267 * @param $year String: Year name
268 * @return string ISO year name
270 function makeIsoYear( $year ) {
271 # Assumes the year is in a nice format, as enforced by the regex
272 if ( substr( $year, -2 ) == 'BC' ) {
273 $num = intval(substr( $year, 0, -3 )) - 1;
274 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
275 $text = sprintf( '-%04d', $num );
278 $text = sprintf( '%04d', $year );
286 function makeNormalYear( $iso ) {
287 if ( $iso{0} == '-' ) {
288 $text = (intval( substr( $iso, 1 ) ) +
1) . ' BC';
290 $text = intval( $iso );