5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Date formatter, recognises dates in plain text and formats them according to user preferences.
26 * @todo preferences, OutputPage
35 protected $monthNames = '';
37 /** @todo Are these unused? */
48 /** @todo Are these unused? */
60 protected $preferences;
81 * @param Language $lang In which language to format the date
83 function __construct( Language
$lang ) {
86 $this->monthNames
= $this->getMonthRegex();
87 for ( $i = 1; $i <= 12; $i++
) {
88 $this->xMonths
[$this->lang
->lc( $this->lang
->getMonthName( $i ) )] = $i;
89 $this->xMonths
[$this->lang
->lc( $this->lang
->getMonthAbbreviation( $i ) )] = $i;
92 $this->regexTrail
= '(?![a-z])/iu';
94 # Partial regular expressions
95 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')\]\]';
96 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})\]\]';
97 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))\]\]';
98 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
99 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
101 # Real regular expressions
102 $this->regexes
[self
::DMY
] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
103 $this->regexes
[self
::YDM
] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
104 $this->regexes
[self
::MDY
] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
105 $this->regexes
[self
::YMD
] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
106 $this->regexes
[self
::DM
] = "/{$this->prxDM}{$this->regexTrail}";
107 $this->regexes
[self
::MD
] = "/{$this->prxMD}{$this->regexTrail}";
108 $this->regexes
[self
::ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
109 $this->regexes
[self
::ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
112 # See the comments in replace() for the meaning of the letters
113 $this->keys
[self
::DMY
] = 'jFY';
114 $this->keys
[self
::YDM
] = 'Y jF';
115 $this->keys
[self
::MDY
] = 'FjY';
116 $this->keys
[self
::YMD
] = 'Y Fj';
117 $this->keys
[self
::DM
] = 'jF';
118 $this->keys
[self
::MD
] = 'Fj';
119 $this->keys
[self
::ISO1
] = 'ymd'; # y means ISO year
120 $this->keys
[self
::ISO2
] = 'ymd';
122 # Target date formats
123 $this->targets
[self
::DMY
] = '[[F j|j F]] [[Y]]';
124 $this->targets
[self
::YDM
] = '[[Y]], [[F j|j F]]';
125 $this->targets
[self
::MDY
] = '[[F j]], [[Y]]';
126 $this->targets
[self
::YMD
] = '[[Y]] [[F j]]';
127 $this->targets
[self
::DM
] = '[[F j|j F]]';
128 $this->targets
[self
::MD
] = '[[F j]]';
129 $this->targets
[self
::ISO1
] = '[[Y|y]]-[[F j|m-d]]';
130 $this->targets
[self
::ISO2
] = '[[y-m-d]]';
134 $this->rules
[self
::DMY
][self
::MD
] = self
::DM
;
135 $this->rules
[self
::ALL
][self
::MD
] = self
::MD
;
136 $this->rules
[self
::MDY
][self
::DM
] = self
::MD
;
137 $this->rules
[self
::ALL
][self
::DM
] = self
::DM
;
138 $this->rules
[self
::NONE
][self
::ISO2
] = self
::ISO1
;
140 $this->preferences
= array(
141 'default' => self
::NONE
,
145 'ISO 8601' => self
::ISO1
,
150 * Get a DateFormatter object
152 * @param Language|string|null $lang In which language to format the date
153 * Defaults to the site content language
154 * @return DateFormatter object
156 public static function &getInstance( $lang = null ) {
157 global $wgMemc, $wgContLang;
158 static $dateFormatter = false;
159 $lang = $lang ?
wfGetLangObj( $lang ) : $wgContLang;
160 $key = wfMemcKey( 'dateformatter', $lang->getCode() );
161 if ( !$dateFormatter ) {
162 $dateFormatter = $wgMemc->get( $key );
163 if ( !$dateFormatter ) {
164 $dateFormatter = new DateFormatter( $lang );
165 $wgMemc->set( $key, $dateFormatter, 3600 );
168 return $dateFormatter;
172 * @param string $preference User preference
173 * @param string $text Text to reformat
174 * @param array $options Array can contain 'linked' and/or 'match-whole'
178 function reformat( $preference, $text, $options = array( 'linked' ) ) {
179 $linked = in_array( 'linked', $options );
180 $match_whole = in_array( 'match-whole', $options );
182 if ( isset( $this->preferences
[$preference] ) ) {
183 $preference = $this->preferences
[$preference];
185 $preference = self
::NONE
;
187 for ( $i = 1; $i <= self
::LAST
; $i++
) {
189 if ( isset( $this->rules
[$preference][$i] ) ) {
191 $this->mTarget
= $this->rules
[$preference][$i];
192 } elseif ( isset( $this->rules
[self
::ALL
][$i] ) ) {
194 $this->mTarget
= $this->rules
[self
::ALL
][$i];
195 } elseif ( $preference ) {
197 $this->mTarget
= $preference;
202 $regex = $this->regexes
[$i];
206 $regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
209 if ( $match_whole ) {
210 // Let's hope this works
211 $regex = preg_replace( '!^/!', '/^', $regex );
212 $regex = str_replace( $this->regexTrail
,
213 '$' . $this->regexTrail
, $regex );
216 // Another horrible hack
217 $this->mLinked
= $linked;
218 $text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
219 unset( $this->mLinked
);
225 * @param array $matches
228 function replace( $matches ) {
229 # Extract information from $matches
231 if ( isset( $this->mLinked
) ) {
232 $linked = $this->mLinked
;
236 $key = $this->keys
[$this->mSource
];
237 $keyLength = strlen( $key );
238 for ( $p = 0; $p < $keyLength; $p++
) {
239 if ( $key[$p] != ' ' ) {
240 $bits[$key[$p]] = $matches[$p +
1];
244 return $this->formatDate( $bits, $linked );
252 function formatDate( $bits, $link = true ) {
253 $format = $this->targets
[$this->mTarget
];
257 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
258 // strip remaining links
259 $format = str_replace( array( '[[', ']]' ), '', $format );
266 // Pre-generate y/Y stuff because we need the year for the <span> title.
267 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) {
268 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
270 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
271 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
274 if ( !isset( $bits['m'] ) ) {
275 $m = $this->makeIsoMonth( $bits['F'] );
276 if ( !$m ||
$m == '00' ) {
283 if ( !isset( $bits['d'] ) ) {
284 $bits['d'] = sprintf( '%02d', $bits['j'] );
287 $formatLength = strlen( $format );
288 for ( $p = 0; $p < $formatLength; $p++
) {
291 case 'd': # ISO day of month
294 case 'm': # ISO month
300 case 'j': # ordinary day of month
301 if ( !isset( $bits['j'] ) ) {
302 $text .= intval( $bits['d'] );
307 case 'F': # long month
308 if ( !isset( $bits['F'] ) ) {
309 $m = intval( $bits['m'] );
310 if ( $m > 12 ||
$m < 1 ) {
313 $text .= $this->lang
->getMonthName( $m );
316 $text .= ucfirst( $bits['F'] );
319 case 'Y': # ordinary (optional BC) year
327 /** @todo FIXME: $matches doesn't exist here, what's expected? */
332 if ( isset( $bits['y'] ) ) {
333 $isoBits[] = $bits['y'];
335 $isoBits[] = $bits['m'];
336 $isoBits[] = $bits['d'];
337 $isoDate = implode( '-', $isoBits );
339 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
340 $text = Html
::rawElement( 'span',
341 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
350 function getMonthRegex() {
352 for ( $i = 1; $i <= 12; $i++
) {
353 $names[] = $this->lang
->getMonthName( $i );
354 $names[] = $this->lang
->getMonthAbbreviation( $i );
356 return implode( '|', $names );
360 * Makes an ISO month, e.g. 02, from a month name
361 * @param string $monthName Month name
362 * @return string ISO month name
364 function makeIsoMonth( $monthName ) {
365 $n = $this->xMonths
[$this->lang
->lc( $monthName )];
366 return sprintf( '%02d', $n );
371 * @param string $year Year name
372 * @return string ISO year name
374 function makeIsoYear( $year ) {
375 # Assumes the year is in a nice format, as enforced by the regex
376 if ( substr( $year, -2 ) == 'BC' ) {
377 $num = intval( substr( $year, 0, -3 ) ) - 1;
378 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
379 $text = sprintf( '-%04d', $num );
382 $text = sprintf( '%04d', $year );
392 function makeNormalYear( $iso ) {
393 if ( $iso[0] == '-' ) {
394 $text = ( intval( substr( $iso, 1 ) ) +
1 ) . ' BC';
396 $text = intval( $iso );