Mr. Stradivarius has uploaded a new change for review.

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

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, 111 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Scribunto 
refs/changes/08/181408/1

diff --git a/engines/LuaCommon/SiteLibrary.php 
b/engines/LuaCommon/SiteLibrary.php
index 5f945d2..b4ed448 100644
--- a/engines/LuaCommon/SiteLibrary.php
+++ b/engines/LuaCommon/SiteLibrary.php
@@ -12,6 +12,7 @@
                        'pagesInCategory' => array( $this, 'pagesInCategory' ),
                        'pagesInNamespace' => array( $this, 'pagesInNamespace' 
),
                        'usersInGroup' => array( $this, 'usersInGroup' ),
+                       'interwikiMap' => array( $this, 'interwikiMap' ),
                );
                $info = array(
                        'siteName' => $GLOBALS['wgSitename'],
@@ -130,4 +131,50 @@
                $name = trim( preg_replace( '/[\s_]+/', '_', $name ), '_' );
                return array( $wgContLang->getNsIndex( $name ) );
        }
+
+       public function interwikiMap( $filter = null ) {
+               $this->checkTypeOptional( 'interwikiMap', 1, $filter, 'string', 
null );
+               global $wgLocalInterwikis, $wgExtraInterlanguageLinkPrefixes;
+               static $interwikiMapCache = array();
+
+               $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')"
+                       );
+               }
+
+               if ( !isset( $interwikiMapCache[$local] ) ) {
+                       $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 ),
+                                       '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'] ) {
+                                       $name = wfMessage( 
"interlanguage-link-$prefix" );
+                                       if ( !$name->isDisabled() ) {
+                                               $val['name'] = $name->text();
+                                       }
+                                       $description = wfMessage( 
"interlanguage-link-sitename-$prefix" );
+                                       if ( !$description->isDisabled() ) {
+                                               $val['description'] = 
$description->text();
+                                       }
+                               }
+                               $interwikiMap[$prefix] = $val;
+                       }
+                       $interwikiMapCache[$local] = $interwikiMap;
+               }
+               return array( $interwikiMapCache[$local] );
+       }
 }
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..3ea99ca 100644
--- a/tests/engines/LuaCommon/SiteLibraryTests.lua
+++ b/tests/engines/LuaCommon/SiteLibraryTests.lua
@@ -14,6 +14,34 @@
        return t
 end
 
+local function isNonBlankString( val )
+       return type( val ) == 'string' and #val > 0
+end
+
+local function isValidInterwikiMap( map )
+       assert( type( map ) == 'table', "mw.site.interwikiMap did not return a 
table" )
+       local stringKeys = { 'prefix', 'url' }
+       local boolKeys = { 'isLocal', 'isTranscludable', 'isCurrentWiki', 
'isExtraLanguageLink' }
+       local maybeStringKeys = { 'name', 'description' }
+       for prefix, data in pairs( map ) do
+               for _, key in ipairs( stringKeys ) do
+                       assert( isNonBlankString( data[key] ),
+                               key .. " is not a string or is the blank 
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 isNonBlankString( data[key] 
),
+                               key .. " is not a string or is the blank 
string, and is not nil" )
+               end
+       end
+       return true
+end
+
 return testframework.getTestProvider( {
        { name = 'parameter: siteName',
          func = type, args = { mw.site.siteName },
@@ -107,4 +135,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: newchange
Gerrit-Change-Id: Ie8ad2582aaf5e422824f7da51714a347bb4041d1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Mr. Stradivarius <misterst...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to