wiki.techinc.nl/includes/parser/DateFormatter.php

284 lines
7.3 KiB
PHP
Raw Normal View History

<?php
/**
* Date formatter, recognises dates in plain text and formats them accoding to user preferences.
* @todo preferences, OutputPage
* @ingroup Parser
*/
2003-11-20 13:12:41 +00:00
class DateFormatter
{
var $mSource, $mTarget;
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;
Merged localisation-work branch: * Made lines from initialiseMessages() appear as list items during installation * Moved the bulk of the localisation data from the Language*.php files to the Messages*.php files. Deleted most of the Languages*.php files. * Introduced "stub global" framework to provide deferred initialisation of core modules. * Removed placeholder values for $wgTitle and $wgArticle, these variables will now be null during the initialisation process, until they are set by index.php or another entry point. * Added DBA cache type, for BDB-style caches. * Removed custom date format functions, replacing them with a format string in the style of PHP's date(). Used string identifiers instead of integer identifiers, in both the language files and user preferences. Migration should be transparent in most cases. * Simplified the initialisation API for LoadBalancer objects. * Removed the broken altencoding feature. * Moved default user options and toggles from Language to User. Language objects are still able to define default preference overrides and extra user toggles, via a slightly different interface. * Don't include the date option in the parser cache rendering hash unless $wgUseDynamicDates is enabled. * Merged LanguageUtf8 with Language. Removed LanguageUtf8.php. * Removed inclusion of language files from the bottom of Language.php. This is now consistently done from Language::factory(). * Add the name of the executing maintenance script to the debug log. Start the profiler during maintenance scripts. * Added "serialized" directory, for storing precompiled data in serialized form.
2006-07-26 07:15:39 +00:00
var $rules, $xMonths, $preferences;
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++ ) {
$this->xMonths[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
$this->xMonths[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
2003-11-20 13:12:41 +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
$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
$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
$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
$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;
Merged localisation-work branch: * Made lines from initialiseMessages() appear as list items during installation * Moved the bulk of the localisation data from the Language*.php files to the Messages*.php files. Deleted most of the Languages*.php files. * Introduced "stub global" framework to provide deferred initialisation of core modules. * Removed placeholder values for $wgTitle and $wgArticle, these variables will now be null during the initialisation process, until they are set by index.php or another entry point. * Added DBA cache type, for BDB-style caches. * Removed custom date format functions, replacing them with a format string in the style of PHP's date(). Used string identifiers instead of integer identifiers, in both the language files and user preferences. Migration should be transparent in most cases. * Simplified the initialisation API for LoadBalancer objects. * Removed the broken altencoding feature. * Moved default user options and toggles from Language to User. Language objects are still able to define default preference overrides and extra user toggles, via a slightly different interface. * Don't include the date option in the parser cache rendering hash unless $wgUseDynamicDates is enabled. * Merged LanguageUtf8 with Language. Removed LanguageUtf8.php. * Removed inclusion of language files from the bottom of Language.php. This is now consistently done from Language::factory(). * Add the name of the executing maintenance script to the debug log. Start the profiler during maintenance scripts. * Added "serialized" directory, for storing precompiled data in serialized form.
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
/**
* @static
*/
function &getInstance() {
global $wgMemc;
static $dateFormatter = false;
if ( !$dateFormatter ) {
$dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
if ( !$dateFormatter ) {
$dateFormatter = new DateFormatter;
$wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
}
}
return $dateFormatter;
2006-01-07 13:31:29 +00:00
}
2004-09-03 16:51:45 +00:00
/**
Merged localisation-work branch: * Made lines from initialiseMessages() appear as list items during installation * Moved the bulk of the localisation data from the Language*.php files to the Messages*.php files. Deleted most of the Languages*.php files. * Introduced "stub global" framework to provide deferred initialisation of core modules. * Removed placeholder values for $wgTitle and $wgArticle, these variables will now be null during the initialisation process, until they are set by index.php or another entry point. * Added DBA cache type, for BDB-style caches. * Removed custom date format functions, replacing them with a format string in the style of PHP's date(). Used string identifiers instead of integer identifiers, in both the language files and user preferences. Migration should be transparent in most cases. * Simplified the initialisation API for LoadBalancer objects. * Removed the broken altencoding feature. * Moved default user options and toggles from Language to User. Language objects are still able to define default preference overrides and extra user toggles, via a slightly different interface. * Don't include the date option in the parser cache rendering hash unless $wgUseDynamicDates is enabled. * Merged LanguageUtf8 with Language. Removed LanguageUtf8.php. * Removed inclusion of language files from the bottom of Language.php. This is now consistently done from Language::factory(). * Add the name of the executing maintenance script to the debug log. Start the profiler during maintenance scripts. * Added "serialized" directory, for storing precompiled data in serialized form.
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 ) {
Merged localisation-work branch: * Made lines from initialiseMessages() appear as list items during installation * Moved the bulk of the localisation data from the Language*.php files to the Messages*.php files. Deleted most of the Languages*.php files. * Introduced "stub global" framework to provide deferred initialisation of core modules. * Removed placeholder values for $wgTitle and $wgArticle, these variables will now be null during the initialisation process, until they are set by index.php or another entry point. * Added DBA cache type, for BDB-style caches. * Removed custom date format functions, replacing them with a format string in the style of PHP's date(). Used string identifiers instead of integer identifiers, in both the language files and user preferences. Migration should be transparent in most cases. * Simplified the initialisation API for LoadBalancer objects. * Removed the broken altencoding feature. * Moved default user options and toggles from Language to User. Language objects are still able to define default preference overrides and extra user toggles, via a slightly different interface. * Don't include the date option in the parser cache rendering hash unless $wgUseDynamicDates is enabled. * Merged LanguageUtf8 with Language. Removed LanguageUtf8.php. * Removed inclusion of language files from the bottom of Language.php. This is now consistently done from Language::factory(). * Add the name of the executing maintenance script to the debug log. Start the profiler during maintenance scripts. * Added "serialized" directory, for storing precompiled data in serialized form.
2006-07-26 07:15:39 +00:00
if ( isset( $this->preferences[$preference] ) ) {
$preference = $this->preferences[$preference];
} else {
$preference = self::NONE;
}
for ( $i=1; $i<=self::LAST; $i++ ) {
2003-11-20 13:12:41 +00:00
$this->mSource = $i;
if ( isset ( $this->rules[$preference][$i] ) ) {
2003-11-20 13:12:41 +00:00
# Specific rules
$this->mTarget = $this->rules[$preference][$i];
} elseif ( isset ( $this->rules[self::ALL][$i] ) ) {
2003-11-20 13:12:41 +00:00
# General rules
$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;
}
$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
$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
if ( !isset($bits['d']) ) {
$text .= sprintf( '%02d', $bits['j'] );
2003-11-20 13:12:41 +00:00
} else {
$text .= $bits['d'];
}
break;
case 'm': # ISO month
if ( !isset($bits['m']) ) {
2003-11-20 13:12:41 +00:00
$m = $this->makeIsoMonth( $bits['F'] );
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
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
if ( !isset($bits['j']) ) {
$text .= intval( $bits['d'] );
2003-11-20 13:12:41 +00:00
} else {
$text .= $bits['j'];
}
break;
case 'F': # long month
if ( !isset( $bits['F'] ) ) {
$m = intval($bits['m']);
2003-11-20 13:12:41 +00:00
if ( $m > 12 || $m < 1 ) {
$fail = true;
} else {
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
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() {
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
* @param $monthName String: month name
2004-09-03 16:51:45 +00:00
* @return string ISO month name
*/
function makeIsoMonth( $monthName ) {
global $wgContLang;
$n = $this->xMonths[$wgContLang->lc( $monthName )];
return sprintf( '%02d', $n );
2003-11-20 13:12:41 +00:00
}
2004-09-03 16:51:45 +00:00
/**
* @todo document
* @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' ) {
$num = intval(substr( $year, 0, -3 )) - 1;
2003-11-20 13:12:41 +00:00
# PHP bug note: sprintf( "%04d", -1 ) fails poorly
$text = sprintf( '-%04d', $num );
2003-11-20 13:12:41 +00:00
} else {
$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} == '-' ) {
$text = (intval( substr( $iso, 1 ) ) + 1) . ' BC';
2003-11-20 13:12:41 +00:00
} else {
$text = intval( $iso );
2003-11-20 13:12:41 +00:00
}
return $text;
}
}