2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2007-03-28 14:16:43 +00:00
|
|
|
|
|
|
|
|
/**
|
2007-04-04 05:22:37 +00:00
|
|
|
* Date formatter, recognises dates in plain text and formats them accoding to user preferences.
|
2004-09-02 23:28:24 +00:00
|
|
|
* @todo preferences, OutputPage
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Parser
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-11-20 13:12:41 +00:00
|
|
|
class DateFormatter
|
|
|
|
|
{
|
|
|
|
|
var $mSource, $mTarget;
|
2004-06-08 23:56:09 +00:00
|
|
|
var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
var $regexes, $pDays, $pMonths, $pYears;
|
2006-07-26 07:15:39 +00:00
|
|
|
var $rules, $xMonths, $preferences;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-07-14 17:02:49 +00:00
|
|
|
const ALL = -1;
|
|
|
|
|
const NONE = 0;
|
|
|
|
|
const MDY = 1;
|
|
|
|
|
const DMY = 2;
|
|
|
|
|
const YMD = 3;
|
|
|
|
|
const ISO1 = 4;
|
|
|
|
|
const LASTPREF = 4;
|
|
|
|
|
const ISO2 = 5;
|
|
|
|
|
const YDM = 6;
|
|
|
|
|
const DM = 7;
|
|
|
|
|
const MD = 8;
|
|
|
|
|
const LAST = 8;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
|
|
|
|
* @todo document
|
|
|
|
|
*/
|
|
|
|
|
function DateFormatter() {
|
2005-12-04 18:27:59 +00:00
|
|
|
global $wgContLang;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
$this->monthNames = $this->getMonthRegex();
|
|
|
|
|
for ( $i=1; $i<=12; $i++ ) {
|
2005-10-29 12:08:48 +00:00
|
|
|
$this->xMonths[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
|
|
|
|
|
$this->xMonths[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2005-10-29 12:08:48 +00:00
|
|
|
$this->regexTrail = '(?![a-z])/iu';
|
|
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
# Partial regular expressions
|
|
|
|
|
$this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')]]';
|
|
|
|
|
$this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})]]';
|
|
|
|
|
$this->prxY = '\[\[(\d{1,4}([ _]BC|))]]';
|
|
|
|
|
$this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
|
|
|
|
|
$this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
# Real regular expressions
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->regexes[self::DMY] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::YDM] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::MDY] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::YMD] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
# Extraction keys
|
|
|
|
|
# See the comments in replace() for the meaning of the letters
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->keys[self::DMY] = 'jFY';
|
|
|
|
|
$this->keys[self::YDM] = 'Y jF';
|
|
|
|
|
$this->keys[self::MDY] = 'FjY';
|
|
|
|
|
$this->keys[self::YMD] = 'Y Fj';
|
|
|
|
|
$this->keys[self::DM] = 'jF';
|
|
|
|
|
$this->keys[self::MD] = 'Fj';
|
|
|
|
|
$this->keys[self::ISO1] = 'ymd'; # y means ISO year
|
|
|
|
|
$this->keys[self::ISO2] = 'ymd';
|
2003-11-20 13:12:41 +00:00
|
|
|
|
|
|
|
|
# Target date formats
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
|
|
|
|
|
$this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
|
|
|
|
|
$this->targets[self::MDY] = '[[F j]], [[Y]]';
|
|
|
|
|
$this->targets[self::YMD] = '[[Y]] [[F j]]';
|
|
|
|
|
$this->targets[self::DM] = '[[F j|j F]]';
|
|
|
|
|
$this->targets[self::MD] = '[[F j]]';
|
|
|
|
|
$this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
|
|
|
|
|
$this->targets[self::ISO2] = '[[y-m-d]]';
|
2003-11-20 13:12:41 +00:00
|
|
|
|
|
|
|
|
# Rules
|
|
|
|
|
# pref source target
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->rules[self::DMY][self::MD] = self::DM;
|
|
|
|
|
$this->rules[self::ALL][self::MD] = self::MD;
|
|
|
|
|
$this->rules[self::MDY][self::DM] = self::MD;
|
|
|
|
|
$this->rules[self::ALL][self::DM] = self::DM;
|
|
|
|
|
$this->rules[self::NONE][self::ISO2] = self::ISO1;
|
2006-07-26 07:15:39 +00:00
|
|
|
|
|
|
|
|
$this->preferences = array(
|
|
|
|
|
'default' => self::NONE,
|
|
|
|
|
'dmy' => self::DMY,
|
|
|
|
|
'mdy' => self::MDY,
|
|
|
|
|
'ymd' => self::YMD,
|
|
|
|
|
'ISO 8601' => self::ISO1,
|
|
|
|
|
);
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-04-20 15:42:08 +00:00
|
|
|
/**
|
|
|
|
|
* @static
|
|
|
|
|
*/
|
|
|
|
|
function &getInstance() {
|
2006-10-04 09:06:18 +00:00
|
|
|
global $wgMemc;
|
2005-04-20 15:42:08 +00:00
|
|
|
static $dateFormatter = false;
|
|
|
|
|
if ( !$dateFormatter ) {
|
2006-10-04 09:06:18 +00:00
|
|
|
$dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
|
2005-04-20 15:42:08 +00:00
|
|
|
if ( !$dateFormatter ) {
|
|
|
|
|
$dateFormatter = new DateFormatter;
|
2006-10-04 09:06:18 +00:00
|
|
|
$wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
|
2005-04-20 15:42:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $dateFormatter;
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
2006-07-26 07:15:39 +00:00
|
|
|
* @param string $preference User preference
|
|
|
|
|
* @param string $text Text to reformat
|
2004-09-03 16:51:45 +00:00
|
|
|
*/
|
|
|
|
|
function reformat( $preference, $text ) {
|
2006-07-26 07:15:39 +00:00
|
|
|
if ( isset( $this->preferences[$preference] ) ) {
|
|
|
|
|
$preference = $this->preferences[$preference];
|
|
|
|
|
} else {
|
|
|
|
|
$preference = self::NONE;
|
|
|
|
|
}
|
2006-07-14 17:02:49 +00:00
|
|
|
for ( $i=1; $i<=self::LAST; $i++ ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
$this->mSource = $i;
|
2007-01-03 05:42:51 +00:00
|
|
|
if ( isset ( $this->rules[$preference][$i] ) ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
# Specific rules
|
|
|
|
|
$this->mTarget = $this->rules[$preference][$i];
|
2007-01-03 05:42:51 +00:00
|
|
|
} elseif ( isset ( $this->rules[self::ALL][$i] ) ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
# General rules
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->mTarget = $this->rules[self::ALL][$i];
|
2003-11-20 13:12:41 +00:00
|
|
|
} elseif ( $preference ) {
|
|
|
|
|
# User preference
|
|
|
|
|
$this->mTarget = $preference;
|
|
|
|
|
} else {
|
|
|
|
|
# Default
|
|
|
|
|
$this->mTarget = $i;
|
|
|
|
|
}
|
2006-07-14 17:02:49 +00:00
|
|
|
$text = preg_replace_callback( $this->regexes[$i], array( &$this, 'replace' ), $text );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
|
|
|
|
* @param $matches
|
|
|
|
|
*/
|
|
|
|
|
function replace( $matches ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
# Extract information from $matches
|
|
|
|
|
$bits = array();
|
|
|
|
|
$key = $this->keys[$this->mSource];
|
|
|
|
|
for ( $p=0; $p < strlen($key); $p++ ) {
|
|
|
|
|
if ( $key{$p} != ' ' ) {
|
|
|
|
|
$bits[$key{$p}] = $matches[$p+1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$format = $this->targets[$this->mTarget];
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
# Construct new date
|
2004-06-08 23:56:09 +00:00
|
|
|
$text = '';
|
2003-11-20 13:12:41 +00:00
|
|
|
$fail = false;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
for ( $p=0; $p < strlen( $format ); $p++ ) {
|
|
|
|
|
$char = $format{$p};
|
|
|
|
|
switch ( $char ) {
|
|
|
|
|
case 'd': # ISO day of month
|
2004-12-29 03:27:16 +00:00
|
|
|
if ( !isset($bits['d']) ) {
|
2004-06-08 23:56:09 +00:00
|
|
|
$text .= sprintf( '%02d', $bits['j'] );
|
2003-11-20 13:12:41 +00:00
|
|
|
} else {
|
|
|
|
|
$text .= $bits['d'];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'm': # ISO month
|
2004-12-29 03:27:16 +00:00
|
|
|
if ( !isset($bits['m']) ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
$m = $this->makeIsoMonth( $bits['F'] );
|
2004-06-08 23:56:09 +00:00
|
|
|
if ( !$m || $m == '00' ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
$fail = true;
|
|
|
|
|
} else {
|
|
|
|
|
$text .= $m;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$text .= $bits['m'];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'y': # ISO year
|
2004-12-29 03:27:16 +00:00
|
|
|
if ( !isset( $bits['y'] ) ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
$text .= $this->makeIsoYear( $bits['Y'] );
|
|
|
|
|
} else {
|
|
|
|
|
$text .= $bits['y'];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'j': # ordinary day of month
|
2004-12-29 03:27:16 +00:00
|
|
|
if ( !isset($bits['j']) ) {
|
2005-08-16 23:36:16 +00:00
|
|
|
$text .= intval( $bits['d'] );
|
2003-11-20 13:12:41 +00:00
|
|
|
} else {
|
|
|
|
|
$text .= $bits['j'];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'F': # long month
|
2004-12-29 03:27:16 +00:00
|
|
|
if ( !isset( $bits['F'] ) ) {
|
2005-08-16 23:36:16 +00:00
|
|
|
$m = intval($bits['m']);
|
2003-11-20 13:12:41 +00:00
|
|
|
if ( $m > 12 || $m < 1 ) {
|
|
|
|
|
$fail = true;
|
|
|
|
|
} else {
|
2004-12-29 03:27:16 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
$text .= $wgContLang->getMonthName( $m );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$text .= ucfirst( $bits['F'] );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'Y': # ordinary (optional BC) year
|
2004-12-29 03:27:16 +00:00
|
|
|
if ( !isset( $bits['Y'] ) ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
$text .= $this->makeNormalYear( $bits['y'] );
|
|
|
|
|
} else {
|
|
|
|
|
$text .= $bits['Y'];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$text .= $char;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $fail ) {
|
|
|
|
|
$text = $matches[0];
|
|
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
|
|
|
|
* @todo document
|
|
|
|
|
*/
|
|
|
|
|
function getMonthRegex() {
|
2004-12-29 03:27:16 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
$names = array();
|
|
|
|
|
for( $i = 1; $i <= 12; $i++ ) {
|
|
|
|
|
$names[] = $wgContLang->getMonthName( $i );
|
|
|
|
|
$names[] = $wgContLang->getMonthAbbreviation( $i );
|
|
|
|
|
}
|
|
|
|
|
return implode( '|', $names );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
|
|
|
|
* Makes an ISO month, e.g. 02, from a month name
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $monthName String: month name
|
2004-09-03 16:51:45 +00:00
|
|
|
* @return string ISO month name
|
|
|
|
|
*/
|
|
|
|
|
function makeIsoMonth( $monthName ) {
|
2005-10-29 12:08:48 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
|
|
|
|
|
$n = $this->xMonths[$wgContLang->lc( $monthName )];
|
2004-06-08 23:56:09 +00:00
|
|
|
return sprintf( '%02d', $n );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
|
|
|
|
* @todo document
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $year String: Year name
|
2004-09-03 16:51:45 +00:00
|
|
|
* @return string ISO year name
|
|
|
|
|
*/
|
|
|
|
|
function makeIsoYear( $year ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
# Assumes the year is in a nice format, as enforced by the regex
|
|
|
|
|
if ( substr( $year, -2 ) == 'BC' ) {
|
2005-08-16 23:36:16 +00:00
|
|
|
$num = intval(substr( $year, 0, -3 )) - 1;
|
2003-11-20 13:12:41 +00:00
|
|
|
# PHP bug note: sprintf( "%04d", -1 ) fails poorly
|
2004-06-08 23:56:09 +00:00
|
|
|
$text = sprintf( '-%04d', $num );
|
2003-11-20 13:12:41 +00:00
|
|
|
|
|
|
|
|
} else {
|
2004-06-08 23:56:09 +00:00
|
|
|
$text = sprintf( '%04d', $year );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
|
|
|
|
* @todo document
|
|
|
|
|
*/
|
|
|
|
|
function makeNormalYear( $iso ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
if ( $iso{0} == '-' ) {
|
2005-08-16 23:36:16 +00:00
|
|
|
$text = (intval( substr( $iso, 1 ) ) + 1) . ' BC';
|
2003-11-20 13:12:41 +00:00
|
|
|
} else {
|
2005-08-16 23:36:16 +00:00
|
|
|
$text = intval( $iso );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
}
|