Modul:JSONutil: Unterschied zwischen den Versionen
2019-07-11
(2019-07-14) |
(2019-07-11) |
||
Zeile 1: | Zeile 1: | ||
local JSONutil = { suite = "JSONutil", | local JSONutil = { suite = "JSONutil", | ||
serial = "2019-07- | serial = "2019-07-11", | ||
item = 63869449 } | item = 63869449 } | ||
--[=[ | --[=[ | ||
preprocess JSON data | preprocess JSON data | ||
]=] | ]=] | ||
Zeile 19: | Zeile 18: | ||
:lower() | :lower() | ||
end -- Fallback() | end -- Fallback() | ||
JSONutil.failsafe = function ( atleast ) | |||
-- Retrieve versioning and check for compliance | |||
-- Precondition: | |||
-- atleast -- string, with required version or "wikidata" | |||
-- or false | |||
-- Postcondition: | |||
-- Returns string with appropriate version, or false | |||
local since = atleast | |||
local r | |||
if since == "wikidata" then | |||
local item = JSONutil.item | |||
since = false | |||
if type( item ) == "number" and item > 0 then | |||
local entity = mw.wikibase.getEntity( string.format( "Q%d", | |||
item ) ) | |||
if type( entity ) == "table" then | |||
local vsn = entity:formatPropertyValues( "P348" ) | |||
if type( vsn ) == "table" and | |||
type( vsn.value ) == "string" and | |||
vsn.value ~= "" then | |||
r = vsn.value | |||
end | |||
end | |||
end | |||
end | |||
if not r then | |||
if not since or since <= JSONutil.serial then | |||
r = JSONutil.serial | |||
else | |||
r = false | |||
end | |||
end | |||
return r | |||
end -- JSONutil.failsafe() | |||
Zeile 54: | Zeile 90: | ||
end | end | ||
end -- while k | end -- while k | ||
end | end | ||
if s:sub( 1, 1 ) == '{' then | if s:sub( 1, 1 ) == '{' then | ||
stab = string.char( 9 ) | stab = string.char( 9 ) | ||
Zeile 120: | Zeile 114: | ||
end | end | ||
while i do | while i do | ||
i = s:find( sep0, j ) | |||
if i then | if i then | ||
if j == 1 then | if j == 1 then | ||
Zeile 181: | Zeile 168: | ||
i = false | i = false | ||
end | end | ||
else | |||
stub = s:sub( j ) | stub = s:sub( j ) | ||
if stub:find( '[^"]*,%s*[%]}]' ) then | if stub:find( '[^"]*,%s*[%]}]' ) then | ||
Zeile 216: | Zeile 203: | ||
end | end | ||
elseif not ( r or last ) then | elseif not ( r or last ) then | ||
r = "Trailing" | |||
stub = suffix or apply or "" | stub = suffix or apply or "" | ||
s = stub:match( "%}%s*(%S[^%}]*)$" ) | |||
if s then | |||
s = mw.ustring.sub( s, 1, JSONutil.more ) | |||
else | else | ||
s = mw.ustring.sub( stub, - JSONutil.more ) | |||
end | end | ||
end | end | ||
Zeile 351: | Zeile 325: | ||
return r | return r | ||
end -- JSONutil.fetch() | end -- JSONutil.fetch() | ||
Zeile 415: | Zeile 346: | ||
end | end | ||
end | end | ||
return | return JSONutil.failsafe( since ) or "" | ||
end -- p.failsafe() | end -- p.failsafe() | ||