Modul:ISO15924/commons

Modul:ISO15924/commons

Die Dokumentation für dieses Modul kann unter Modul:ISO15924/commons/Doku erstellt werden

local ISO15924 = { suite  = "ISO15924",
                   sub    = "commons",
                   serial = "2019-11-12",
                   item   = 71679535,
                   main   = 71584769 }
--[=[
Create ISO 15924 mw.loadData() table from commons:Data:
]=]
local Failsafe = ISO15924
local Fetch    = { }



Fetch.data = function ( access, assume )
    -- Retrieve one Data.tab element
    -- Precondition:
    --     access  -- string, with sub tab, or false
    --     assume  -- string, with expected structure type
    -- Returns: table
    local storage = ISO15924.suite
    local r = { }
    local data, lucky
    if access then
        storage = string.format( "%s/%s", storage, access )
    end
    storage = storage .. ".tab"
    lucky, data = pcall( mw.ext.data.get, storage )
    if type( data ) == "table"  and
       type( data.data ) == "table"  and
       type( Fetch[ assume ] ) == "function" then
        local entry, sign, value
        data = data.data
        for i = 1, #data do
            entry = data[ i ]
            if type( entry ) == "table" then
                sign, value = Fetch[ assume ]( entry )
                if sign then
                    r[ sign ] = value
                elseif value then
                    table.insert( r, value )
                end
            end
        end -- for i
    end
    return r
end -- Fetch.data()



Fetch.boolean = function ( achieved )
    -- Convert ext.data entry into assignment to boolean
    local r1 = achieved[ 1 ]
    local r2 = achieved[ 2 ]
    if not r2 then
        r1 = false
    end
    return r1, r2
end -- Fetch.boolean()



Fetch.ranges = function ( achieved )
    -- Convert ext.data entry into table of ranges or redirects
    local r = achieved[ 2 ]
    if r:sub( 1, 2 ) == "0x" then
        local ranges = mw.text.split( r, "%s*|%s*" )
        local range
        r = { }
        for i = 1, #ranges do
            range = mw.text.split( ranges[ i ], "%s+" )
            range = { tonumber( range[ 1 ] ),
                      tonumber( range[ 2 ] ) }
            table.insert( r, range )
        end -- for i
    else
        r = mw.text.split( r, "%s+" )
    end
    return achieved[ 1 ], r
end -- Fetch.ranges()



Fetch.reverse = function ( achieved )
    -- Convert ext.data entry into table item with range and string table
    local iBeg   = tonumber( achieved[ 1 ], 16 )
    local iEnd   = tonumber( achieved[ 2 ], 16 )
    local permit = mw.text.split( achieved[ 3 ], "%s+" )
    return false,  { iBeg, iEnd, permit }
end -- Fetch.reverse()



Fetch.string = function ( achieved )
    -- Convert ext.data entry into assignment to string
    return achieved[ 1 ], achieved[ 2 ]
end -- Fetch.string()



Fetch.strings = function ( achieved )
    -- Convert ext.data entry into assignment to table of strings
    return achieved[ 1 ], mw.text.split( achieved[ 2 ], "%s+" )
end -- Fetch.strings()



Fetch.run = function ()
    -- Populate object with data
    -- Postcondition:
    --    ISO15924 expanded
    local tables = Fetch.data( false, "string" )
    for k, v in pairs( tables ) do
        ISO15924[ k ] = Fetch.data( k, v )
    end -- for k, v
end -- Fetch.run()



local fresh = function ()
    -- Check for global update
    -- Returns: true, if matching global version; false, if not
    -- 2019-10-15
    local r
    if type( Failsafe.item ) == "number"  and  Failsafe.item > 0 then
        local storage = string.format( "Q%d", Failsafe.item )
        local entity  = mw.wikibase.getEntity( storage )
        if type( entity ) == "table" then
            local seek = Failsafe.serialProperty or "P348"
            local vsn  = entity:formatPropertyValues( seek )
            if type( vsn ) == "table"  and
               vsn.value == Failsafe.serial then
                r = true
            end
        end
    end
    return r or false
end -- fresh()



Fetch.run()
ISO15924.failsafe = ISO15924.serial
if mw.title.getCurrentTitle().namespace > 0 then
    ISO15924.last = fresh()
end



return ISO15924