Improve ApiQuerySiteInfo handling of deprecated and "extra" language links

Fix documentation related to ExtraInterlanguageLinkPrefixes
configuration: it should be a list, not a map, and described usage
better.

In ApiQuerySiteInfo, third-party clients (like Parsoid) need to know
whether a given language link core corresponds to a deprecated
language code or a "real" one; the API was also missing information
regarding which language code an "extra language link" prefix
corresponds to (given by InterlanguageLinkCodeMap in the
configuration).

Finally, add the corresponding bcp47 codes for these interlanguage
links, so third-party clients don't need to know details of mediawiki
internal and deprecated language codes.

Change-Id: I82465261bc66f0b0cd30d361c299f08066494762
This commit is contained in:
C. Scott Ananian 2023-02-02 14:42:58 -05:00 committed by C. Scott Ananian
parent 4f75bd859d
commit 310d335136
7 changed files with 43 additions and 4 deletions

View file

@ -2913,8 +2913,8 @@ config-schema:
default: false
description: 'Hide interlanguage links from the sidebar'
ExtraInterlanguageLinkPrefixes:
default: { }
type: object
default: []
type: array
description: |-
List of additional interwiki prefixes that should be treated as
interlanguage links (i.e. placed in the sidebar).
@ -2927,6 +2927,12 @@ config-schema:
- A friendly name for each site, used for tooltip text, may optionally be
placed in the system message "interlanguage-link-sitename-xyz" where xyz is
the prefix in this array.
- This should be a list of "interwiki prefixes" (ie, what appears in
wikitext), and you probably want to add an entry to
InterlanguageLinkCodeMap as well to specify which mediawiki internal
(or custom) language code this prefix corresponds to, and perhaps
then map that custom language code to a language name in
ExtraLanguageNames.
InterlanguageLinkCodeMap:
default: { }
type: object

View file

@ -4694,10 +4694,16 @@ class MainConfigSchema {
* - A friendly name for each site, used for tooltip text, may optionally be
* placed in the system message "interlanguage-link-sitename-xyz" where xyz is
* the prefix in this array.
* - This should be a list of "interwiki prefixes" (ie, what appears in
* wikitext), and you probably want to add an entry to
* InterlanguageLinkCodeMap as well to specify which mediawiki internal
* (or custom) language code this prefix corresponds to, and perhaps
* then map that custom language code to a language name in
* ExtraLanguageNames.
*/
public const ExtraInterlanguageLinkPrefixes = [
'default' => [],
'type' => 'map',
'type' => 'list',
];
/**

View file

@ -514,6 +514,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
$getPrefixes = $this->interwikiLookup->getAllPrefixes( $local );
$extraLangPrefixes = $this->getConfig()->get( MainConfigNames::ExtraInterlanguageLinkPrefixes );
$extraLangCodeMap = $this->getConfig()->get( MainConfigNames::InterlanguageLinkCodeMap );
$localInterwikis = $this->getConfig()->get( MainConfigNames::LocalInterwikis );
$data = [];
@ -530,12 +531,21 @@ class ApiQuerySiteinfo extends ApiQueryBase {
if ( $interwikiMagic && isset( $langNames[$prefix] ) ) {
$val['language'] = $langNames[$prefix];
$standard = LanguageCode::replaceDeprecatedCodes( $prefix );
if ( $standard !== $prefix ) {
# Note that even if this code is deprecated, it should
# only be remapped if extralanglink (set below) is false.
$val['deprecated'] = $standard;
}
$val['bcp47'] = LanguageCode::bcp47( $standard );
}
if ( in_array( $prefix, $localInterwikis ) ) {
$val['localinterwiki'] = true;
}
if ( $interwikiMagic && in_array( $prefix, $extraLangPrefixes ) ) {
$val['extralanglink'] = true;
$val['code'] = $extraLangCodeMap[$prefix] ?? $prefix;
$val['bcp47'] = LanguageCode::bcp47( $val['code'] );
$linktext = $this->msg( "interlanguage-link-$prefix" );
if ( !$linktext->isDisabled() ) {

View file

@ -2644,7 +2644,7 @@ return [
'CdnServersNoPurge' => 'object',
'HTCPRouting' => 'object',
'GrammarForms' => 'object',
'ExtraInterlanguageLinkPrefixes' => 'object',
'ExtraInterlanguageLinkPrefixes' => 'array',
'InterlanguageLinkCodeMap' => 'object',
'ExtraLanguageNames' => 'object',
'ExtraLanguageCodes' => 'object',

View file

@ -24,6 +24,7 @@ namespace MediaWiki\Parser\Parsoid\Config;
use Config;
use ExtensionRegistry;
use Language;
use LanguageCode;
use LanguageConverter;
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
use MediaWiki\Config\ServiceOptions;
@ -77,6 +78,7 @@ class SiteConfig extends ISiteConfig {
MainConfigNames::ArticlePath,
MainConfigNames::InterwikiMagic,
MainConfigNames::ExtraInterlanguageLinkPrefixes,
MainConfigNames::InterlanguageLinkCodeMap,
MainConfigNames::LocalInterwikis,
MainConfigNames::LanguageCode,
MainConfigNames::NamespaceAliases,
@ -435,6 +437,7 @@ class SiteConfig extends ISiteConfig {
$getPrefixes = $this->interwikiLookup->getAllPrefixes();
$langNames = $this->languageNameUtils->getLanguageNames();
$extraLangPrefixes = $this->config->get( MainConfigNames::ExtraInterlanguageLinkPrefixes );
$extraLangCodeMap = $this->config->get( MainConfigNames::InterlanguageLinkCodeMap );
$localInterwikis = $this->config->get( MainConfigNames::LocalInterwikis );
foreach ( $getPrefixes as $row ) {
@ -462,12 +465,21 @@ class SiteConfig extends ISiteConfig {
}
if ( isset( $langNames[$prefix] ) ) {
$val['language'] = true;
$standard = LanguageCode::replaceDeprecatedCodes( $prefix );
if ( $standard !== $prefix ) {
# Note that even if this code is deprecated, it should
# only be remapped if extralanglink (set below) is false.
$val['deprecated'] = $standard;
}
$val['bcp47'] = LanguageCode::bcp47( $standard );
}
if ( in_array( $prefix, $localInterwikis, true ) ) {
$val['localinterwiki'] = true;
}
if ( in_array( $prefix, $extraLangPrefixes, true ) ) {
$val['extralanglink'] = true;
$val['code'] = $extraLangCodeMap[$prefix] ?? $prefix;
$val['bcp47'] = LanguageCode::bcp47( $val['code'] );
}
$this->interwikiMap[$prefix] = $val;

View file

@ -226,8 +226,10 @@ class ApiQuerySiteinfoTest extends ApiTestCase {
'local' => true,
'trans' => true,
'language' => 'Recursion',
'bcp47' => 'self',
'localinterwiki' => true,
'extralanglink' => true,
'code' => 'self',
'linktext' => 'Self!',
'sitename' => 'Circular logic',
'url' => 'https://local.example/w/index.php?title=$1',

View file

@ -42,6 +42,7 @@ class SiteConfigTest extends MediaWikiUnitTestCase {
MainConfigNames::ArticlePath => false,
MainConfigNames::InterwikiMagic => true,
MainConfigNames::ExtraInterlanguageLinkPrefixes => [],
MainConfigNames::InterlanguageLinkCodeMap => [],
MainConfigNames::LocalInterwikis => [],
MainConfigNames::LanguageCode => 'qqq',
MainConfigNames::DisableLangConversion => false,
@ -524,8 +525,10 @@ class SiteConfigTest extends MediaWikiUnitTestCase {
'protorel' => true,
'local' => true,
'language' => true,
'bcp47' => 'ru',
'localinterwiki' => true,
'extralanglink' => true,
'code' => 'ru',
]
], $config->interwikiMap() );
}