Modul:TemplateData: Unterschied zwischen den Versionen
2019-05-18
w>PerfektesChaos (2019-01-03) |
w>PerfektesChaos (2019-05-18) |
||
| Zeile 1: | Zeile 1: | ||
local TemplateData = { suite = "TemplateData", | local TemplateData = { suite = "TemplateData", | ||
serial = "2019- | serial = "2019-05-18", | ||
item = 46997995 } | item = 46997995 } | ||
--[=[ | --[=[ | ||
| Zeile 57: | Zeile 57: | ||
scream = false, -- error messages | scream = false, -- error messages | ||
sibling = false, -- TOC juxtaposed | sibling = false, -- TOC juxtaposed | ||
slang = | slang = nil, -- project/user language code | ||
slim = false, -- JSON reduced to plain | slim = false, -- JSON reduced to plain | ||
source = false, -- JSON input | source = false, -- JSON input | ||
| Zeile 136: | Zeile 136: | ||
local function Fetch( ask ) | local function Fetch( ask, allow ) | ||
-- Fetch module | -- Fetch module | ||
-- Parameter: | -- Parameter: | ||
-- ask | -- ask -- string, with name | ||
-- | -- "/global" | ||
-- | -- "JSONutil" | ||
-- | -- "Multilingual" | ||
-- | -- "Text" | ||
-- "WLink" | |||
-- allow -- true: no error if unavailable | |||
-- Returns table of module | -- Returns table of module | ||
-- error: Module not available | -- error: Module not available | ||
| Zeile 168: | Zeile 170: | ||
end | end | ||
TemplateData.extern[ sign ] = r | TemplateData.extern[ sign ] = r | ||
elseif not allow then | |||
error( string.format( "Fetch(%s) %s", sign, g ) ) | error( string.format( "Fetch(%s) %s", sign, g ) ) | ||
end | end | ||
| Zeile 174: | Zeile 176: | ||
return r | return r | ||
end -- Fetch() | end -- Fetch() | ||
local function Foreign() | |||
-- Guess human language | |||
-- Returns slang, or not | |||
if type( Data.slang ) == "nil" then | |||
local Multilingual = Fetch( "Multilingual" ) | |||
if type( Multilingual.userLangCode ) == "function" then | |||
Data.slang = Multilingual.userLangCode() | |||
else | |||
Data.slang = false | |||
end | |||
end | |||
if Data.slang and | |||
mw.ustring.codepoint( Data.slang, 1, 1 ) > 122 then | |||
Data.slang = false | |||
end | |||
return Data.slang | |||
end -- Foreign() | |||
| Zeile 213: | Zeile 235: | ||
-- Returns string, with localized text | -- Returns string, with localized text | ||
local o = mw.message.new( "templatedata-" .. adapt ) | local o = mw.message.new( "templatedata-" .. adapt ) | ||
if | if Foreign() then | ||
o:inLanguage( Data.slang ) | o:inLanguage( Data.slang ) | ||
end | end | ||
| Zeile 360: | Zeile 376: | ||
local variants = { } | local variants = { } | ||
local r1, r2 | local r1, r2 | ||
Foreign() | |||
for k, v in pairs( alternatives ) do | for k, v in pairs( alternatives ) do | ||
if type( v ) == "string" then | if type( v ) == "string" then | ||
| Zeile 1.661: | Zeile 1.674: | ||
TemplateData.failsafe = function ( | TemplateData.failsafe = function ( atleast ) | ||
-- Retrieve versioning and check for compliance | -- Retrieve versioning and check for compliance | ||
-- Precondition: | -- Precondition: | ||
-- | -- atleast -- string, with required version or "wikidata" | ||
-- | -- or false | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string with appropriate version, or false | -- Returns string with appropriate version, or false | ||
local since = | local since = atleast | ||
local r | local r | ||
if since == "wikidata" then | if since == "wikidata" then | ||
| Zeile 1.730: | Zeile 1.743: | ||
-- Returns string, or not | -- Returns string, or not | ||
if type( adapt ) == "string" then | if type( adapt ) == "string" then | ||
local | local JSONutil = Fetch( "JSONutil", true ) | ||
Data.source = adapt | Data.source = adapt | ||
free() | free() | ||
lucky, Data.got = pcall( mw.text.jsonDecode, Data.source ) | if JSONutil then | ||
if | Data.got = JSONutil.fetch( Data.source, | ||
true, | |||
Foreign() ) | |||
else | |||
local lucky | |||
lucky, Data.got = pcall( mw.text.jsonDecode, Data.source ) | |||
end | |||
if type( Data.got ) == "table" then | |||
full() | full() | ||
elseif not Data.strip then | elseif not Data.strip then | ||