Page MenuHomePhabricator

Remove uses of $wgLang from WMF-deployed extension
Open, Needs TriagePublic

Description

Code search: https://codesearch.wmcloud.org/deployed/?q=wgLang%5Cb&files=%5C.php

Write-only:

Event Timeline

Change #1256594 had a related patch set uploaded (by SomeRandomDeveloper; author: SomeRandomDeveloper):

[mediawiki/extensions/Translate@master] CleanChangesFilters: Replace use of $wgRequest and $wgLang

https://gerrit.wikimedia.org/r/1256594

Change #1256594 merged by jenkins-bot:

[mediawiki/extensions/Translate@master] CleanChangesFilters: Replace use of $wgRequest and $wgLang

https://gerrit.wikimedia.org/r/1256594

Change #1259965 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/Echo@master] Remove use of $wgLang in ForeignNotifications class

https://gerrit.wikimedia.org/r/1259965

Change #1259965 merged by jenkins-bot:

[mediawiki/extensions/Echo@master] Remove use of $wgLang in ForeignNotifications class

https://gerrit.wikimedia.org/r/1259965

Change #1260041 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/FlaggedRevs@master] Replace usages of wgLang

https://gerrit.wikimedia.org/r/1260041

Change #1260051 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/Calendar@master] Replace use of wgLang

https://gerrit.wikimedia.org/r/1260051

Change #1260050 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/ConfirmEdit@master] Replace use of wgLang

https://gerrit.wikimedia.org/r/1260050

Change #1260051 merged by jenkins-bot:

[mediawiki/extensions/Calendar@master] Replace use of wgLang

https://gerrit.wikimedia.org/r/1260051

Change #1261471 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/UploadWizard@master] Replace usages of wgLang

https://gerrit.wikimedia.org/r/1261471

Change #1260050 merged by jenkins-bot:

[mediawiki/extensions/ConfirmEdit@master] Replace use of wgLang

https://gerrit.wikimedia.org/r/1260050

@SomeRandomDeveloper Do you happen to know whether there is any way to obtain a context in standard maintenance scripts?

@SomeRandomDeveloper Do you happen to know whether there is any way to obtain a context in standard maintenance scripts?

I personally never had the need to get a context in a maintenance script. RequestContext::getMain() seems to be used occasionally though: https://codesearch.wmcloud.org/deployed/?q=RequestContext%3A%3AgetMain&files=%5Emaintenance%2F
If you need a language, you could also consider using the content language, but whether that's a good idea depends on the use case.

@MGChecker What would you call the context language in a maintenance script? I don’t think this concept makes any sense in scripts. Where do need it?

Flow seems to heavily rely on the use of wgLang in its maintenance scripts. But accessing request context in maintenance scripts seems to more generally occur:
https://codesearch.wmcloud.org/search/?q=RequestContext%3A%3AgetMain%28%29&files=maintenance&excludeFiles=&repos=
I have no clue where these scripts actually would get a sensible language from. There does not seem the place where it could be set, so RequestContext::getLanguage should end up with $code = $wgLanguageCode as a fallback.

Hence, all of these will get the same result:

$lang = RequstContext::getMain()->getLanguage()
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( $wgLanguageCode );
$lang = $this->getServiceContainer()->getContentLanguage()

Probably, the first option is still the best because it automatically migrates if someone comes up with some more elaborate general language handling in maintenance scripts.

For instance, one could set
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );, and make this available in the class,
as has been done particularly in maintenance/update.php (and globalised with RequestContext::setLanguage) following T22633.
But this is rather out of scope for this task.

Looking through the results,

  • many maintenance scripts actually need the site language, regardless of in what context they’re called, because they write back the result in the database. The Flow maintenance scripts interpreting/translating namespace names also belong here, since people see the namespace name in the site language. $wgLang or RequestContext::getMain()->msg(...) is just a shortcut to achieve that, and they should instead use $this->getServiceContainer()->getContentLanguage() or wfMessage(...)->inContentLanguage().
  • Others print the result to the console, and should use $this->getServiceContainer()->getLanguageFactory()->getLanguage('en') in the vein of T22633. If they are really fully internationalized, they may instead use the language from the shell context (LC_ALL). Using the site language is a bad idea even in that case, as the person running the script may not understand the site language (as is the case when Wikimedia administrators run scripts in different-language wikis).
  • Yet others get the context to write it (e.g. CheckUser fakes IP addresses and headers), so that non-maintenance-specific code can read it from there; those are the ones that should really use the request context (until that non-maintenance-specific code stops using the global context, and can be passed a context object directly).

So the answer to your original question T419848#11756062 is: well, it depends.

Change #1264044 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/Flow@master] Replace usages of wgLang

https://gerrit.wikimedia.org/r/1264044

Change #1260058 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/JsonConfig@master] Replace uses of wgLang

https://gerrit.wikimedia.org/r/1260058

Change #1268065 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/Wikibase@master] Reduce use of wgLang

https://gerrit.wikimedia.org/r/1268065

Change #1268065 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] Reduce use of wgLang

https://gerrit.wikimedia.org/r/1268065

Change #1260058 merged by jenkins-bot:

[mediawiki/extensions/JsonConfig@master] Replace uses of wgLang

https://gerrit.wikimedia.org/r/1260058

Change #1261471 merged by jenkins-bot:

[mediawiki/extensions/UploadWizard@master] Replace usages of wgLang

https://gerrit.wikimedia.org/r/1261471

Change #1271075 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/LiquidThreads@master] Replace wgLang with RequestContext

https://gerrit.wikimedia.org/r/1271075

Change #1271075 merged by jenkins-bot:

[mediawiki/extensions/LiquidThreads@master] Replace wgLang with RequestContext

https://gerrit.wikimedia.org/r/1271075

Change #1264044 merged by jenkins-bot:

[mediawiki/extensions/Flow@master] Replace usages of wgLang

https://gerrit.wikimedia.org/r/1264044

Change #1260041 merged by jenkins-bot:

[mediawiki/extensions/FlaggedRevs@master] Avoid accessing main context and replace wgLang

https://gerrit.wikimedia.org/r/1260041

Change #1278478 had a related patch set uploaded (by MGChecker; author: MGChecker):

[mediawiki/extensions/Wikibase@master] Avoid use of deprecated wgLang in Wikibase

https://gerrit.wikimedia.org/r/1278478