jenkins-bot has submitted this change and it was merged. Change subject: Add mw.site.interwikiMap ......................................................................
Add mw.site.interwikiMap This makes the interwiki map available to Lua modules. The code is based on the API interwiki map code in core (the appendInterwikiMap method of includes/api/ApiQuerySiteInfo.php.) Everything that the API includes is added, apart from iw_api and iw_wikiid, which I couldn't think of a use for from Lua modules. Accessing the interwiki map would be useful for modules like enwiki's Module:InterwikiTable,[1] as it would stop module writers having to duplicate the data. [1] https://en.wikipedia.org/wiki/Module:InterwikiTable Change-Id: Ie8ad2582aaf5e422824f7da51714a347bb4041d1 --- M engines/LuaCommon/SiteLibrary.php M engines/LuaCommon/lualib/mw.site.lua M tests/engines/LuaCommon/SiteLibraryTests.lua 3 files changed, 122 insertions(+), 0 deletions(-) Approvals: Anomie: Looks good to me, approved jenkins-bot: Verified diff --git a/engines/LuaCommon/SiteLibrary.php b/engines/LuaCommon/SiteLibrary.php index 5f945d2..475d447 100644 --- a/engines/LuaCommon/SiteLibrary.php +++ b/engines/LuaCommon/SiteLibrary.php @@ -2,6 +2,7 @@ class Scribunto_LuaSiteLibrary extends Scribunto_LuaLibraryBase { private static $namespacesCache = null; + private static $interwikiMapCache = array(); private $pagesInCategoryCache = array(); function register() { @@ -12,6 +13,7 @@ 'pagesInCategory' => array( $this, 'pagesInCategory' ), 'pagesInNamespace' => array( $this, 'pagesInNamespace' ), 'usersInGroup' => array( $this, 'usersInGroup' ), + 'interwikiMap' => array( $this, 'interwikiMap' ), ); $info = array( 'siteName' => $GLOBALS['wgSitename'], @@ -130,4 +132,51 @@ $name = trim( preg_replace( '/[\s_]+/', '_', $name ), '_' ); return array( $wgContLang->getNsIndex( $name ) ); } + + public function interwikiMap( $filter = null ) { + global $wgLocalInterwikis, $wgExtraInterlanguageLinkPrefixes; + $this->checkTypeOptional( 'interwikiMap', 1, $filter, 'string', null ); + $local = null; + if ( $filter === 'local' ) { + $local = 1; + } elseif ( $filter === '!local' ) { + $local = 0; + } elseif ( $filter !== null ) { + throw new Scribunto_LuaError( + "bad argument #1 to 'interwikiMap' (unknown filter '$filter')" + ); + } + $cacheKey = $filter === null ? 'null' : $filter; + if ( !isset( self::$interwikiMapCache[$cacheKey] ) ) { + // Not expensive because we can have a max of three cache misses in the + // entire page parse. + $interwikiMap = array(); + $prefixes = Interwiki::getAllPrefixes( $local ); + foreach ( $prefixes as $row ) { + $prefix = $row['iw_prefix']; + $val = array( + 'prefix' => $prefix, + 'url' => wfExpandUrl( $row['iw_url'], PROTO_CURRENT ), + 'isProtocolRelative' => substr( $row['iw_url'], 0, 2 ) === '//', + 'isLocal' => isset( $row['iw_local'] ) && $row['iw_local'] == '1', + 'isTranscludable' => isset( $row['iw_trans'] ) && $row['iw_trans'] == '1', + 'isCurrentWiki' => in_array( $prefix, $wgLocalInterwikis ), + 'isExtraLanguageLink' => in_array( $prefix, $wgExtraInterlanguageLinkPrefixes ), + ); + if ( $val['isExtraLanguageLink'] ) { + $displayText = wfMessage( "interlanguage-link-$prefix" ); + if ( !$displayText->isDisabled() ) { + $val['displayText'] = $displayText->text(); + } + $tooltip = wfMessage( "interlanguage-link-sitename-$prefix" ); + if ( !$tooltip->isDisabled() ) { + $val['tooltip'] = $tooltip->text(); + } + } + $interwikiMap[$prefix] = $val; + } + self::$interwikiMapCache[$cacheKey] = $interwikiMap; + } + return array( self::$interwikiMapCache[$cacheKey] ); + } } diff --git a/engines/LuaCommon/lualib/mw.site.lua b/engines/LuaCommon/lualib/mw.site.lua index 504faa2..09943a8 100644 --- a/engines/LuaCommon/lualib/mw.site.lua +++ b/engines/LuaCommon/lualib/mw.site.lua @@ -15,6 +15,7 @@ site.stats.pagesInCategory = php.pagesInCategory site.stats.pagesInNamespace = php.pagesInNamespace site.stats.usersInGroup = php.usersInGroup + site.interwikiMap = php.interwikiMap -- Process namespace list into more useful tables site.namespaces = {} diff --git a/tests/engines/LuaCommon/SiteLibraryTests.lua b/tests/engines/LuaCommon/SiteLibraryTests.lua index 2806f29..4b9625c 100644 --- a/tests/engines/LuaCommon/SiteLibraryTests.lua +++ b/tests/engines/LuaCommon/SiteLibraryTests.lua @@ -14,6 +14,43 @@ return t end +local function isNonEmptyString( val ) + return type( val ) == 'string' and val ~= '' +end + +local function isValidInterwikiMap( map ) + assert( type( map ) == 'table', "mw.site.interwikiMap did not return a table" ) + local stringKeys = { 'prefix', 'url' } + local boolKeys = { + 'isProtocolRelative', + 'isLocal', + 'isTranscludable', + 'isCurrentWiki', + 'isExtraLanguageLink' + } + local maybeStringKeys = { 'displayText', 'tooltip' } + for prefix, data in pairs( map ) do + for _, key in ipairs( stringKeys ) do + assert( isNonEmptyString( data[key] ), + key .. " is not a string or is the empty string" + ) + end + assert( prefix == data.prefix, string.format( + "table key '%s' and prefix '%s' do not match", + tostring( prefix ), tostring( data.prefix ) + ) ) + for _, key in ipairs( boolKeys ) do + assert( type( data[key] ) == 'boolean', key .. " is not a boolean" ) + end + for _, key in ipairs( maybeStringKeys ) do + assert( data[key] == nil or isNonEmptyString( data[key] ), + key .. " is not a string or is the empty string, and is not nil" + ) + end + end + return true +end + return testframework.getTestProvider( { { name = 'parameter: siteName', func = type, args = { mw.site.siteName }, @@ -107,4 +144,39 @@ func = nsTest, args = { '_ _ _Project_ _talk_ _ _', 'id' }, expect = { 5 } }, + + { name = 'interwikiMap (all prefixes)', + func = isValidInterwikiMap, args = { mw.site.interwikiMap() }, + expect = { true } + }, + + { name = 'interwikiMap (local prefixes)', + func = isValidInterwikiMap, args = { mw.site.interwikiMap( 'local' ) }, + expect = { true } + }, + + { name = 'interwikiMap (non-local prefixes)', + func = isValidInterwikiMap, args = { mw.site.interwikiMap( '!local' ) }, + expect = { true } + }, + + { name = 'interwikiMap (type error 1)', + func = mw.site.interwikiMap, args = { 123 }, + expect = "bad argument #1 to 'interwikiMap' (string expected, got number)" + }, + + { name = 'interwikiMap (type error 2)', + func = mw.site.interwikiMap, args = { false }, + expect = "bad argument #1 to 'interwikiMap' (string expected, got boolean)" + }, + + { name = 'interwikiMap (unknown filter 1)', + func = mw.site.interwikiMap, args = { '' }, + expect = "bad argument #1 to 'interwikiMap' (unknown filter '')" + }, + + { name = 'interwikiMap (unknown filter 2)', + func = mw.site.interwikiMap, args = { 'foo' }, + expect = "bad argument #1 to 'interwikiMap' (unknown filter 'foo')" + }, } ) -- To view, visit https://gerrit.wikimedia.org/r/181408 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ie8ad2582aaf5e422824f7da51714a347bb4041d1 Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/Scribunto Gerrit-Branch: master Gerrit-Owner: Mr. Stradivarius <misterst...@gmail.com> Gerrit-Reviewer: Anomie <bjor...@wikimedia.org> Gerrit-Reviewer: Jackmcbarn <jackmcb...@gmail.com> Gerrit-Reviewer: Mr. Stradivarius <misterst...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits