Module:Interwiki extra
Appearance
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
Usage
{{#invoke:Interwiki extra|interwiki}}
See also {{Interwiki extra}} — wrapper.
Pages that use this module to add additional interwiki links are contained in the tracking category Category:Module:Interwiki extra: additional interwiki links (1,214).
-- This is the interwiki library. It is not meant to be used directly in articles, but to be used by other Lua modules.
local interwikiTable = mw.loadData( 'Module:InterwikiTable' )
local libraryUtil = require( 'libraryUtil' )
local iw = {}
local function getProjectCode( prefix )
for code, t in pairs( interwikiTable ) do
for _, interwikiPrefix in ipairs( t.iw_prefix ) do
if prefix == interwikiPrefix then
return code
end
end
end
return nil
end
function iw.isKnownPrefix( prefix )
return getProjectCode( prefix ) and true or false
end
function iw.isValidLanguageCode( code )
return mw.language.isKnownLanguageTag( code )
end
function iw.new( prefix )
local obj = {}
local data = {}
local checkSelf = libraryUtil.makeCheckSelfFunction( 'Module:Interwiki', 'interwiki', obj, 'interwiki object' )
local code = interwikiTable[ prefix ] and prefix or getProjectCode( prefix )
if not code then
return nil
end
local iwTable = interwikiTable[ code ]
data.domain = iwTable.domain
data.titlePrefix = iwTable.title_prefix
data.takesLanguagePrefix = iwTable.takesLanguagePrefix
function data:getPrefixes()
checkSelf( self, 'getPrefixes' )
local t = {}
for i, iwPrefix in ipairs( iwTable.iw_prefix ) do
table.insert( t, iwPrefix )
end
return t
end
-- Specify which fields are read-only, and prepare the metatable.
local readOnlyFields = {
domain = true,
titlePrefix = true,
takesLanguagePrefix = true,
getPrefixes = true,
}
local function pairsfunc( t, k )
local v
repeat
k = next( readOnlyFields, k )
if k == nil then
return nil
end
v = t[ k ]
until v ~= nil
return k, v
end
return setmetatable( obj, {
__pairs = function ( t )
return pairsfunc, t, nil
end,
__index = data,
__newindex = function( t, key, value )
if readOnlyFields[ key ] then
error( 'index "' .. key .. '" is read-only', 2 )
else
rawset( t, key, value )
end
end,
__tostring = function( t )
return t:getPrefixes()[ 1 ]
end
} )
end
function iw.test()
local new = iw.new( 'wikibooks' )
return tostring( new )
end
return iw