Modul:JSONutil: Unterschied zwischen den Versionen
2019-07-14
(2019-07-11) |
(2019-07-14) |
||
Zeile 1: | Zeile 1: | ||
local JSONutil = { suite = "JSONutil", | local JSONutil = { suite = "JSONutil", | ||
serial = "2019-07- | serial = "2019-07-14", | ||
item = 63869449 } | item = 63869449 } | ||
--[=[ | --[=[ | ||
preprocess JSON data | preprocess JSON data | ||
]=] | ]=] | ||
local Failsafe = JSONutil | |||
Zeile 18: | Zeile 19: | ||
:lower() | :lower() | ||
end -- Fallback() | end -- Fallback() | ||
Zeile 90: | Zeile 54: | ||
end | end | ||
end -- while k | end -- while k | ||
end | end -- framework() | ||
local free = function ( a, f ) | |||
-- Throws: error if /* is not matched by */ | |||
local s = mw.text.trim( a ) | |||
local i = s:find( "//", 1, true ) | |||
local k = s:find( "/*", 1, true ) | |||
if i or k then | |||
if i then | |||
k = s:find( "\n", i + 2, true ) | |||
if k then | |||
if i == 1 then | |||
s = s:sub( k + 1 ) | |||
else | |||
s = s:sub( 1, i - 1 ) .. | |||
s:sub( k + 1 ) | |||
end | |||
elseif i > 1 then | |||
s = s:sub( 1, i - 1 ) | |||
else | |||
s = "" | |||
end | |||
elseif k then | |||
i = s:find( "*/", k + 2, true ) | |||
if i then | |||
if k == 1 then | |||
s = s:sub( i + 2 ) | |||
else | |||
s = s:sub( 1, k - 1 ) .. | |||
s:sub( i + 2 ) | |||
end | |||
else | |||
error( s:sub( k + 2 ), 0 ) | |||
end | |||
end | |||
if ( i or k ) then | |||
s = mw.text.trim( s ) | |||
if s ~= "" then | |||
s = f( s, f ) | |||
end | |||
end | |||
end | |||
return s | |||
end -- free() | |||
if s:sub( 1, 1 ) == '{' then | if s:sub( 1, 1 ) == '{' then | ||
stab = string.char( 9 ) | stab = string.char( 9 ) | ||
Zeile 114: | Zeile 120: | ||
end | end | ||
while i do | while i do | ||
i = s:find( sep0, j ) | i, s = pcall( free, s, free ) | ||
if i then | |||
i = s:find( sep0, j ) | |||
else | |||
r = "CommentEnd" | |||
s = mw.text.trim( s ) | |||
s = mw.ustring.sub( s, 1, JSONutil.more ) | |||
end | |||
if i then | if i then | ||
if j == 1 then | if j == 1 then | ||
Zeile 168: | Zeile 181: | ||
i = false | i = false | ||
end | end | ||
elseif not r then | |||
stub = s:sub( j ) | stub = s:sub( j ) | ||
if stub:find( '[^"]*,%s*[%]}]' ) then | if stub:find( '[^"]*,%s*[%]}]' ) then | ||
Zeile 203: | Zeile 216: | ||
end | end | ||
elseif not ( r or last ) then | elseif not ( r or last ) then | ||
stub = suffix or apply or "" | stub = suffix or apply or "" | ||
s | if stub:find( "/", 1, true ) then | ||
i, stub = pcall( free, stub, free ) | |||
else | |||
i = true | |||
end | |||
stub = mw.text.trim( stub ) | |||
if i then | |||
if stub:sub( - 1 ) ~= "}" then | |||
r = "Trailing" | |||
s = stub:match( "%}%s*(%S[^%}]*)$" ) | |||
if s then | |||
s = mw.ustring.sub( s, 1, JSONutil.more ) | |||
else | |||
s = mw.ustring.sub( stub, - JSONutil.more ) | |||
end | |||
end | |||
else | else | ||
s = mw.ustring.sub( stub, | r = "CommentEnd" | ||
s = mw.ustring.sub( stub, 1, JSONutil.more ) | |||
end | end | ||
end | end | ||
Zeile 325: | Zeile 351: | ||
return r | return r | ||
end -- JSONutil.fetch() | end -- JSONutil.fetch() | ||
Failsafe.failsafe = function ( atleast ) | |||
-- Retrieve versioning and check for compliance | |||
-- Precondition: | |||
-- atleast -- string, with required version or "wikidata" or "~" | |||
-- or false | |||
-- Postcondition: | |||
-- Returns string -- with queried version, also if problem | |||
-- false -- if appropriate | |||
local last = ( atleast == "~" ) | |||
local since = atleast | |||
local r | |||
if last or since == "wikidata" then | |||
local item = Failsafe.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 | |||
if last and vsn.value == Failsafe.serial then | |||
r = false | |||
else | |||
r = vsn.value | |||
end | |||
end | |||
end | |||
end | |||
end | |||
if type( r ) == "nil" then | |||
if not since or since <= Failsafe.serial then | |||
r = Failsafe.serial | |||
else | |||
r = false | |||
end | |||
end | |||
return r | |||
end -- Failsafe.failsafe() | |||
Zeile 346: | Zeile 415: | ||
end | end | ||
end | end | ||
return | return Failsafe.failsafe( since ) or "" | ||
end -- p.failsafe() | end -- p.failsafe() | ||