r86927 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86926‎ | r86927 | r86928 >
Date:00:26, 26 April 2011
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
adding language support to #time parser function, per bug 28655
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions_body.php
@@ -419,11 +419,11 @@
420420 }
421421 }
422422
423 - public static function time( $parser, $format = '', $date = '', $local = false ) {
424 - global $wgContLang, $wgLocaltimezone;
 423+ public static function time( $parser, $format = '', $date = '', $language = '', $local = false ) {
 424+ global $wgLang, $wgContLang, $wgLocaltimezone;
425425 self::registerClearHook();
426 - if ( isset( self::$mTimeCache[$format][$date][$local] ) ) {
427 - return self::$mTimeCache[$format][$date][$local];
 426+ if ( isset( self::$mTimeCache[$format][$date][$language][$local] ) ) {
 427+ return self::$mTimeCache[$format][$date][$language][$local];
428428 }
429429
430430 # compute the timestamp string $ts
@@ -504,15 +504,22 @@
505505 if ( self::$mTimeChars > self::$mMaxTimeChars ) {
506506 return '<strong class="error">' . wfMsgForContent( 'pfunc_time_too_long' ) . '</strong>';
507507 } else {
508 - $result = $wgContLang->sprintfDate( $format, $ts );
 508+ if ( $language == 'user' ) { // use user's interface language
 509+ $result = $wgLang->sprintfDate( $format, $ts );
 510+ } elseif ( $language !== '' ) { // use whatever language is passed as a parameter
 511+ $langObject = Language::factory( $language );
 512+ $result = $langObject->sprintfDate( $format, $ts );
 513+ } else { // use wiki's content language
 514+ $result = $wgContLang->sprintfDate( $format, $ts );
 515+ }
509516 }
510517 }
511 - self::$mTimeCache[$format][$date][$local] = $result;
 518+ self::$mTimeCache[$format][$date][$language][$local] = $result;
512519 return $result;
513520 }
514521
515 - public static function localTime( $parser, $format = '', $date = '' ) {
516 - return self::time( $parser, $format, $date, true );
 522+ public static function localTime( $parser, $format = '', $date = '', $language = '' ) {
 523+ return self::time( $parser, $format, $date, $language, true );
517524 }
518525
519526 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r87305fix issues from r86927, user language and years over 9999kaldari22:21, 2 May 2011
r87308better language handling - abandon magic language switch in favor of using in...kaldari23:30, 2 May 2011
r106987Add test and output for {{#time:d F Y|1988-02-28|nl}}. This test should pass....siebrand21:59, 21 December 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r86805fix for bug 28655 - #time interpreting years incorrectlykaldari01:10, 24 April 2011

Comments

#Comment by Nikerabbit (talk | contribs)   05:29, 26 April 2011

Is this the way we are going, adding language parameter to each function individually?

#Comment by Kaldari (talk | contribs)   06:58, 26 April 2011

I'm not sure if this is the best solution or not. Some admins on Commons requested this addition to #time specifically, so I went ahead and added it. You probably have a better idea of what would make sense for the long term, however. Let me know if you have thoughts on how to implement this functionality in a better way.

#Comment by Nikerabbit (talk | contribs)   07:01, 26 April 2011

There isn't better way, at least not now. I know there are many magic words that would be useful with language parameter, so I've been dreaming of some sort of general solution to solve it all at once. It may just be an utopia though.

#Comment by Brion VIBBER (talk | contribs)   19:58, 28 April 2011

Crazy idea: a parser function to force output language, wrap it around anything?

{{#uselang:de|Die Uhr ist jetzt {{#time}}}}

(May not be a good idea. May cause your grandmother to explode. May eat your dog's lunch and replace it with tofu.)

#Comment by 😂 (talk | contribs)   20:30, 28 April 2011

I tried this idea in my working copy, and my grandmother spontaneously combusted. It's all your fault!

#Comment by Platonides (talk | contribs)   20:48, 28 April 2011

Could that be modified to apply to mothers-in-law instead of grandmothers?

#Comment by 😂 (talk | contribs)   20:50, 28 April 2011

We should make it configurable. Perhaps a $wgCombustableFamilyMembers array?

#Comment by Platonides (talk | contribs)   20:53, 28 April 2011

That wouldn't cover the common use case of combusting the sysadmin if mediawiki is outdated (to be run from cron).

#Comment by Nikerabbit (talk | contribs)   08:26, 28 April 2011

Just occurred to me that this is not as useful as it seems unless also the predefined date formats for each language are accessible.

#Comment by Kaldari (talk | contribs)   19:54, 28 April 2011

Can you elaborate?

#Comment by 😂 (talk | contribs)   20:33, 28 April 2011

Generally speaking, you shouldn't rearrange method parameters like you did here:

-	public static function time( $parser, $format = '', $date = '', $local = false ) {
+	public static function time( $parser, $format = '', $date = '', $language = '', $local = false ) {

Just add language to the end.

#Comment by Kaldari (talk | contribs)   22:09, 28 April 2011

I would have except that this is a strange case. The 'local' param is only used by another function (localTime/#timel) and not by the parser function per se ({{#time)). Since 'language' should actually be used in the parser function call, but not 'local', I have to put 'language' first.

In other words, this is a supported call to #time: 1945 but never: 1945 (even though it was technically possible), as this is covered by #timel

You may want to refer to http://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time for more info.

#Comment by Kaldari (talk | contribs)   22:13, 28 April 2011

Oops, all my examples got parsed. Must use preview button...

This is a supported call:
{{#time:F Y|December 1945|fr}}
but never:
{{#time:F Y|December 1945|true}}

#Comment by Kaldari (talk | contribs)   22:34, 28 April 2011

I'm not sure if my previous explanation makes any sense, so let me try it again...

Before this rev, the only params actually used by the parser function #time were $format and $date. The $local param was tacked on so that #timel/localtime() could just piggyback off the time function instead of having redundant code. The $local param was never intended for use by #time (since {{#time:Y|1945|true}} is the same as {{#timel:Y|1945}} and {{#time:Y|1945|false}} is the same as {{#time:Y|1945}}) and the parameter's presence is not reflected in the documentation for the parser function. Since I want the $language parameter to actually be used by #time, I put it at the end of the parser function parameters (but not the end of the underlying function's parameters). That way it can be used by #time without forcing people to start inserting a '|false' into the call before the language parameter. Since #timel/localtime() is the only thing that actually uses the $local param in time() (I checked), I only have to change the call there to make sure nothing breaks. Hope that makes sense.

#Comment by Platonides (talk | contribs)   16:28, 29 April 2011

It is much clear :D

#Comment by 😂 (talk | contribs)   22:18, 29 April 2011

Yes, thank you for clearing that up.

#Comment by Bawolff (talk | contribs)   15:27, 30 April 2011

For the use user language option. Doesn't that need to communicate to the parser that the content varies by userlanguage so that the cache varies by user language? (aka by calling $parser->getOptions()->getUserLang() ). Otherwise its not so much the language of the current user but the language of the last user to cause a page render.

#Comment by Kaldari (talk | contribs)   18:25, 30 April 2011

Ah that's probably true. Didn't think about that.

#Comment by Kaldari (talk | contribs)   22:27, 2 May 2011

Hopefully this is fixed in r87305. I don't know much about how the language caching works, so let me know if this is wrong. Also, I'm wondering if this is really even a good idea. Will this cause too much cache fragmentation if people start using it widely? Thoughts?

#Comment by Kaldari (talk | contribs)   02:12, 13 May 2011

Hey Bawolff, can you take a look at the follow-up diffs and let me know if anything else needs to be fixed? Thanks!

#Comment by MaxSem (talk | contribs)   16:19, 1 May 2011

Try saving a page with {{#time:j F| +1000000000000 days}}. You'll get the following meltdown:

Notice: Undefined offset: 89 in D:\Projects\MediaWiki\languages\Language.php on line 608

Internal error
Non-string key given

Backtrace:

#0 D:\Projects\MediaWiki\includes\GlobalFunctions.php(694): MessageCache->get(NULL, true, Object(Language))
#1 D:\Projects\MediaWiki\includes\GlobalFunctions.php(818): wfMsgGetKey(NULL, true, Object(Language), false)
#2 D:\Projects\MediaWiki\languages\Language.php(596): wfMsgExt(NULL, Array)
#3 D:\Projects\MediaWiki\languages\Language.php(608): Language->getMessageFromDB(NULL)
#4 D:\Projects\MediaWiki\languages\Language.php(898): Language->getMonthName('90')
#5 D:\Projects\MediaWiki\extensions\ParserFunctions\ParserFunctions_body.php(513): Language->sprintfDate('j F', '273790901804271...')
#6 [internal function]: ExtParserFunctions::time(Object(Parser), 'j F', '+1000000000000 ...')
#7 D:\Projects\MediaWiki\includes\parser\Parser.php(3192): call_user_func_array('ExtParserFuncti...', Array)
#8 D:\Projects\MediaWiki\includes\parser\Preprocessor_DOM.php(986): Parser->braceSubstitution(Array, Object(PPFrame_DOM))
#9 D:\Projects\MediaWiki\includes\parser\Parser.php(2991): PPFrame_DOM->expand(Object(PPNode_DOM), 0)
#10 D:\Projects\MediaWiki\includes\parser\Parser.php(1153): Parser->replaceVariables('<big>'''MediaWi...')
#11 D:\Projects\MediaWiki\includes\parser\Parser.php(315): Parser->internalParse('<big>'''MediaWi...')
#12 D:\Projects\MediaWiki\includes\Article.php(4272): Parser->parse('<big>'''MediaWi...', Object(Title), Object(ParserOptions), true, true, 5567)
#13 D:\Projects\MediaWiki\includes\Article.php(4249): Article->getOutputFromWikitext('<big>'''MediaWi...', true, Object(ParserOptions))
#14 D:\Projects\MediaWiki\includes\Article.php(1542): Article->outputWikiText('<big>'''MediaWi...', true, Object(ParserOptions))
#15 D:\Projects\MediaWiki\includes\Article.php(4525): Article->doViewParse()
#16 D:\Projects\MediaWiki\includes\PoolCounter.php(163): PoolWorkArticleView->doWork()
#17 D:\Projects\MediaWiki\includes\Article.php(1065): PoolCounterWork->execute()
#18 D:\Projects\MediaWiki\includes\Wiki.php(486): Article->view()
#19 D:\Projects\MediaWiki\includes\Wiki.php(104): MediaWiki->performAction(Object(Article))
#20 D:\Projects\MediaWiki\index.php(145): MediaWiki->performRequestForTitle(Object(Article))
#21 {main}
#Comment by Nikerabbit (talk | contribs)   16:45, 1 May 2011

Cute. The code excepts only four digit years everywhere.

#Comment by Kaldari (talk | contribs)   22:27, 2 May 2011

Fixed in r87305 (only for #time, not for Language in general).

#Comment by Kaldari (talk | contribs)   23:34, 2 May 2011

After talking with Platonides, I've redone the language handling. See r87308. The magic language switching is gone, but you can still accomplish the same thing by passing {{int:lang}} as the language param on Commons (the main place this is needed).

#Comment by (talk | contribs)   17:02, 19 July 2011

It doesn't work at translatewiki, see r87308.

#Comment by Kaldari (talk | contribs)   16:58, 16 August 2011

int:lang doesn't exist on most wikis. I put in some defensive programming so that it fails gracefully in r92571 and r92572.

Status & tagging log