8b87215705036ba0fbf4fffbbc5707859dc08ff9
3 * Date formatter, recognises dates in plain text and formats them accoding to user preferences.
10 * @todo preferences, OutputPage
16 var $mSource, $mTarget;
17 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
19 var $regexes, $pDays, $pMonths, $pYears;
38 function DateFormatter() {
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;
47 $this->regexTrail
= '(?![a-z])/iu';
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})]]';
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}";
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';
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]]';
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
;
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 );
109 return $dateFormatter;
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++
) {
120 if ( @$this->rules
[$preference][$i] ) {
122 $this->mTarget
= $this->rules
[$preference][$i];
123 } elseif ( @$this->rules
[self
::ALL
][$i] ) {
125 $this->mTarget
= $this->rules
[self
::ALL
][$i];
126 } elseif ( $preference ) {
128 $this->mTarget
= $preference;
133 $text = preg_replace_callback( $this->regexes
[$i], array( &$this, 'replace' ), $text );
141 function replace( $matches ) {
142 # Extract information from $matches
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];
151 $format = $this->targets
[$this->mTarget
];
157 for ( $p=0; $p < strlen( $format ); $p++
) {
160 case 'd': # ISO day of month
161 if ( !isset($bits['d']) ) {
162 $text .= sprintf( '%02d', $bits['j'] );
167 case 'm': # ISO month
168 if ( !isset($bits['m']) ) {
169 $m = $this->makeIsoMonth( $bits['F'] );
170 if ( !$m ||
$m == '00' ) {
180 if ( !isset( $bits['y'] ) ) {
181 $text .= $this->makeIsoYear( $bits['Y'] );
186 case 'j': # ordinary day of month
187 if ( !isset($bits['j']) ) {
188 $text .= intval( $bits['d'] );
193 case 'F': # long month
194 if ( !isset( $bits['F'] ) ) {
195 $m = intval($bits['m']);
196 if ( $m > 12 ||
$m < 1 ) {
200 $text .= $wgContLang->getMonthName( $m );
203 $text .= ucfirst( $bits['F'] );
206 case 'Y': # ordinary (optional BC) year
207 if ( !isset( $bits['Y'] ) ) {
208 $text .= $this->makeNormalYear( $bits['y'] );
226 function getMonthRegex() {
229 for( $i = 1; $i <= 12; $i++
) {
230 $names[] = $wgContLang->getMonthName( $i );
231 $names[] = $wgContLang->getMonthAbbreviation( $i );
233 return implode( '|', $names );
237 * Makes an ISO month, e.g. 02, from a month name
238 * @param $monthName String: month name
239 * @return string ISO month name
241 function makeIsoMonth( $monthName ) {
244 $n = $this->xMonths
[$wgContLang->lc( $monthName )];
245 return sprintf( '%02d', $n );
250 * @param $year String: Year name
251 * @return string ISO year name
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 );
261 $text = sprintf( '%04d', $year );
269 function makeNormalYear( $iso ) {
270 if ( $iso{0} == '-' ) {
271 $text = (intval( substr( $iso, 1 ) ) +
1) . ' BC';
273 $text = intval( $iso );