Modul:JSONutil: Unterschied zwischen den Versionen

Keine Bearbeitungszusammenfassung
Markierungen: Manuelle Zurücksetzung Zurückgesetzt
Keine Bearbeitungszusammenfassung
Markierungen: Manuelle Zurücksetzung Zurückgesetzt
Zeile 1: Zeile 1:
local JSONutil = { suite  = "JSONutil",
local JSONutil = { suite  = "JSONutil",
                   serial = "2019-07-18",
                   serial = "2019-07-11",
                   item  = 63869449 }
                   item  = 63869449 }
--[=[
--[=[
preprocess JSON data
preprocess JSON data
]=]
]=]
local Failsafe = JSONutil




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   -- framework()
                       end
    local free = function ( a, at, f )
                    -- Throws: error if /* is not matched by */
                    local s = a
                    local i = s:find( "//", at, true )
                    local k = s:find( "/*", at, true )
                    if i or k then
                        local m = s:find( sep0, at )
                        if i  and  ( not m  or  i < m ) 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  and  ( not m  or  k < m ) 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
                            i = k
                        else
                            i = false
                        end
                        if i then
                            s = mw.text.trim( s )
                            if s:find( "/", 1, true ) then
                                s = f( s, i, 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 124: Zeile 114:
     end
     end
     while i do
     while i do
         i, s = pcall( free, s, j, free )
         i = s:find( sep0, j )
        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 185: Zeile 168:
                 i = false
                 i = false
             end
             end
         elseif not r then
         else
             stub = s:sub( j )
             stub = s:sub( j )
             if stub:find( '[^"]*,%s*[%]}]' ) then
             if stub:find( '[^"]*,%s*[%]}]' ) then
Zeile 220: 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 ""
         j   = stub:find( "/", 1, true )
         s   = stub:match( "%}%s*(%S[^%}]*)$" )
        if j then
        if s then
            i, stub = pcall( free, stub, j, free )
            s = mw.ustring.sub( s, 1, JSONutil.more )
        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
            r = "CommentEnd"
             s = mw.ustring.sub( stub, - JSONutil.more )
             s = mw.ustring.sub( stub, 1, JSONutil.more )
         end
         end
     end
     end
Zeile 356: Zeile 325:
     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 420: Zeile 346:
         end
         end
     end
     end
     return Failsafe.failsafe( since )  or  ""
     return JSONutil.failsafe( since )  or  ""
end -- p.failsafe()
end -- p.failsafe()