From: robin
Date: Fri, 1 Jun 2012 09:52:57 +0000 (+0200)
Subject: Make DateFormatter work in any language, by default in the page content language
X-Git-Tag: 1.31.0-rc.0~22936^2
X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=e16c4c9ed84516ef969934da1b16c153d31e8ece;p=lhc%2Fweb%2Fwiklou.git
Make DateFormatter work in any language, by default in the page content language
Add a lang parameter to DateFormatter so it can work in any language instead of only the site content language.
(The memcached key is now per language code.)
Use by default parser->getTargetLanguage() so it is parsed in the page content language by default.
Also add some documentation and remove unneeded whitespace.
If needed, a parameter to {{#dateformatter}} can now be easily added, to specify the language to format in.
Change-Id: If61854920065f7c3b4170ab89e9aa66b299f9dd8
---
diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php
index 912de41365..9a96242df1 100644
--- a/includes/parser/CoreParserFunctions.php
+++ b/includes/parser/CoreParserFunctions.php
@@ -129,7 +129,8 @@ class CoreParserFunctions {
* @return mixed|string
*/
static function formatDate( $parser, $date, $defaultPref = null ) {
- $df = DateFormatter::getInstance();
+ $lang = $parser->getFunctionLang();
+ $df = DateFormatter::getInstance( $lang );
$date = trim( $date );
diff --git a/includes/parser/DateFormatter.php b/includes/parser/DateFormatter.php
index 1638fb0f19..2917b4a740 100644
--- a/includes/parser/DateFormatter.php
+++ b/includes/parser/DateFormatter.php
@@ -26,14 +26,15 @@
* @todo preferences, OutputPage
* @ingroup Parser
*/
-class DateFormatter
-{
+class DateFormatter {
var $mSource, $mTarget;
var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
var $regexes, $pDays, $pMonths, $pYears;
var $rules, $xMonths, $preferences;
+ protected $lang;
+
const ALL = -1;
const NONE = 0;
const MDY = 1;
@@ -48,15 +49,15 @@ class DateFormatter
const LAST = 8;
/**
- * @todo document
+ * @param $lang Language In which language to format the date
*/
- function __construct() {
- global $wgContLang;
+ function __construct( Language $lang ) {
+ $this->lang = $lang;
$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;
+ $this->xMonths[$this->lang->lc( $this->lang->getMonthName( $i ) )] = $i;
+ $this->xMonths[$this->lang->lc( $this->lang->getMonthAbbreviation( $i ) )] = $i;
}
$this->regexTrail = '(?![a-z])/iu';
@@ -119,16 +120,20 @@ class DateFormatter
/**
* Get a DateFormatter object
*
+ * @param $lang Language|string|null In which language to format the date
+ * Defaults to the site content language
* @return DateFormatter object
*/
- public static function &getInstance() {
- global $wgMemc;
+ public static function &getInstance( $lang = null ) {
+ global $wgMemc, $wgContLang;
static $dateFormatter = false;
+ $lang = $lang ? wfGetLangObj( $lang ) : $wgContLang;
+ $key = wfMemcKey( 'dateformatter', $lang->getCode() );
if ( !$dateFormatter ) {
- $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
+ $dateFormatter = $wgMemc->get( $key );
if ( !$dateFormatter ) {
- $dateFormatter = new DateFormatter;
- $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
+ $dateFormatter = new DateFormatter( $lang );
+ $wgMemc->set( $key, $dateFormatter, 3600 );
}
}
return $dateFormatter;
@@ -141,10 +146,9 @@ class DateFormatter
* @return mixed|String
*/
function reformat( $preference, $text, $options = array('linked') ) {
-
$linked = in_array( 'linked', $options );
$match_whole = in_array( 'match-whole', $options );
-
+
if ( isset( $this->preferences[$preference] ) ) {
$preference = $this->preferences[$preference];
} else {
@@ -166,19 +170,19 @@ class DateFormatter
$this->mTarget = $i;
}
$regex = $this->regexes[$i];
-
+
// Horrible hack
if (!$linked) {
$regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
}
-
+
if ($match_whole) {
// Let's hope this works
$regex = preg_replace( '!^/!', '/^', $regex );
$regex = str_replace( $this->regexTrail,
'$'.$this->regexTrail, $regex );
}
-
+
// Another horrible hack
$this->mLinked = $linked;
$text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
@@ -204,10 +208,15 @@ class DateFormatter
$bits[$key[$p]] = $matches[$p+1];
}
}
-
+
return $this->formatDate( $bits, $linked );
}
-
+
+ /**
+ * @param $bits array
+ * @param $link bool
+ * @return string
+ */
function formatDate( $bits, $link = true ) {
$format = $this->targets[$this->mTarget];
@@ -221,13 +230,13 @@ class DateFormatter
# Construct new date
$text = '';
$fail = false;
-
+
// Pre-generate y/Y stuff because we need the year for the title.
if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) )
$bits['y'] = $this->makeIsoYear( $bits['Y'] );
if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) )
$bits['Y'] = $this->makeNormalYear( $bits['y'] );
-
+
if ( !isset( $bits['m'] ) ) {
$m = $this->makeIsoMonth( $bits['F'] );
if ( !$m || $m == '00' ) {
@@ -236,7 +245,7 @@ class DateFormatter
$bits['m'] = $m;
}
}
-
+
if ( !isset($bits['d']) ) {
$bits['d'] = sprintf( '%02d', $bits['j'] );
}
@@ -266,8 +275,7 @@ class DateFormatter
if ( $m > 12 || $m < 1 ) {
$fail = true;
} else {
- global $wgContLang;
- $text .= $wgContLang->getMonthName( $m );
+ $text .= $this->lang->getMonthName( $m );
}
} else {
$text .= ucfirst( $bits['F'] );
@@ -283,18 +291,18 @@ class DateFormatter
if ( $fail ) {
$text = $matches[0];
}
-
+
$isoBits = array();
if ( isset($bits['y']) )
$isoBits[] = $bits['y'];
$isoBits[] = $bits['m'];
$isoBits[] = $bits['d'];
$isoDate = implode( '-', $isoBits );
-
+
// Output is not strictly HTML (it's wikitext), but is whitelisted.
$text = Html::rawElement( 'span',
array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
-
+
return $text;
}
@@ -303,11 +311,10 @@ class DateFormatter
* @return string
*/
function getMonthRegex() {
- global $wgContLang;
$names = array();
for( $i = 1; $i <= 12; $i++ ) {
- $names[] = $wgContLang->getMonthName( $i );
- $names[] = $wgContLang->getMonthAbbreviation( $i );
+ $names[] = $this->lang->getMonthName( $i );
+ $names[] = $this->lang->getMonthAbbreviation( $i );
}
return implode( '|', $names );
}
@@ -318,9 +325,7 @@ class DateFormatter
* @return string ISO month name
*/
function makeIsoMonth( $monthName ) {
- global $wgContLang;
-
- $n = $this->xMonths[$wgContLang->lc( $monthName )];
+ $n = $this->xMonths[$this->lang->lc( $monthName )];
return sprintf( '%02d', $n );
}
diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt
index 6d324e4d92..dd205f6143 100644
--- a/tests/parser/parserTests.txt
+++ b/tests/parser/parserTests.txt
@@ -9366,6 +9366,17 @@ wgUseDynamicDates=true
!! end
+!! test
+formatdate parser function, with default format and on a page of which the content language is always English and different from the wiki content language
+!! options
+language=nl title=[[MediaWiki:Common.css]]
+!! input
+{{#formatdate:2009-03-24|dmy}}
+!! result
+24 March 2009
+
+!! end
+
#
#
#