Modul:Homokozó/Tacsipacsi/Wikidata

A Wikipédiából, a szabad enciklopédiából

Homokozó/Tacsipacsi/Wikidata[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

local p = {}
 
local i18n = {
    ["errors"] = {
        ["property-param-not-provided"] = "Hiányzó ''property='' paraméter.",
        ["entity-not-found"] = "Nem létező Wikidata-elem.",
        ["unknown-claim-type"] = "Ismeretlen az állítás típusa.",
        ["unknown-snak-type"] = "Ismeretlen a snak típusa.",
        ["unknown-datavalue-type"] = "Ismeretlen az érték típusa.",
        ["unknown-entity-type"] = "Ismeretlen a Wikidata-elem típusa.",
        ["unknown-value-module"] = "A ''value-module'' és ''value-function'' paramétert egyszerre kell beállítani.",
        ["value-module-not-found"] = "A ''value-module'' nem létező modulra mutat.",
        ["value-function-not-found"] = "A ''value-function'' nem létező funkcióra mutat.",
        ["globecoordinate-element"] = "Az érték típusa ''globecoordinate'' : kell ''element=latitude'' vagy ''longitude''.",
        ["invalid-value"] = "Érvénytelen érték",
    },
    ["somevalue"] = "''nem ismert''",
    ["novalue"] = "''nincs''"
}

function getArgs(frame)
	args = {}
    if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
        for k, v in pairs(frame.args) do
            args[k] = v
        end
    else
        args = frame
    end
    return args
end
 
function getEntityFromId(id)
    if id then
        return mw.wikibase.getEntityObject(id)
    end
    return mw.wikibase.getEntityObject()
end
 
function getEntityIdFromValue(value)
    local prefix = ''
    if value['entity-type'] == 'item' then
        prefix = 'q'
    elseif value['entity-type'] == 'property' then
        prefix = 'p'
    else
        return formatError('unknown-entity-type')
    end
    return prefix .. value['numeric-id']
end
 
function formatError( key )
    return '<strong class="error">' .. i18n.errors[key] .. '</strong>'
end
 
 
function formatStatements( options )
    if not options.property then
        return formatError( 'property-param-not-provided' )
    end
 
    --Get entity
    local entity = nil
    if options.entity and type( options.entity ) == "table" then
        entity = options.entity
    else
        entity = getEntityFromId( options.entityId )
    end
    
    if not entity then
        return '' --TODO error?
    end
 
    if not entity.claims or not entity.claims[options.property:upper()] then
        return '' --TODO error?
    end
 
    if options.first then
        return formatStatement(entity.claims[string.upper(options.property)][1], options)
    end
 
    local max_rank = 1
    for _, statement in pairs(entity.claims[string.upper(options.property)]) do
        if p.RANK[statement.rank] > max_rank and formatStatement(statement, options) then
            max_rank = p.RANK[statement.rank]
        end
    end
    local ranks = {all = 0, normal = 1, preferred = 2}
    local rank = ranks[options.rank] or 1
    if rank == 2 and max_rank == 1 then
        rank = 1
    end
 
    --Format statement and concat them cleanly
    local formattedStatements = {}
    for _, statement in pairs(entity.claims[string.upper(options.property)]) do
        if p.RANK[statement.rank] >= rank then
            local fs
            if string.upper(options.property) == 'P1082' then  -- population
                fs = require('Modul:Homokozó/Tacsipacsi/Wikidata-népesség').formatStatement(statement, options)
            else
                fs = formatStatement(statement, options)
            end
            if fs then
                if options['felsorolás'] == 'lista' then
                    fs = '* ' .. fs
                elseif options['felsorolás'] == 'számozott lista' then
                    fs = '# ' .. fs
                end
                if options.rank == 'one' then
                    if p.RANK[statement.rank] == max_rank then
                        table.insert(formattedStatements, fs)
                        break
                    end
                else
                    table.insert(formattedStatements, fs)
                end
            end
        end
    end
    if options['felsorolás'] == 'lista' or options['felsorolás'] == 'számozott lista' then
        return table.concat(formattedStatements, '\n')
    elseif options['felsorolás'] == 'sorok' then
        return table.concat(formattedStatements, '<br>')
    elseif options['felsorolás'] == 'szöveg' then
        return mw.text.listToText(formattedStatements)
    elseif options.separator or options.conjunction then
        options.separator = options.separator and string.gsub(options.separator, '&#32;', ' ')
        options.conjunction = options.conjunction and string.gsub(options.conjunction, '&#32;', ' ')
        return mw.text.listToText(formattedStatements, options.separator, options.conjunction)
    else
        return table.concat(formattedStatements, '<br>')
    end
end
 
function formatStatement( statement, options )
    if not statement.type or statement.type ~= 'statement' then
        return formatError( 'unknown-claim-type' )
    end
 
    return formatSnak( statement.mainsnak, options )
    --TODO reference and qualifiers
end
 
function formatSnak( snak, options )
    if snak.snaktype == 'somevalue' then
        return i18n['somevalue']
    elseif snak.snaktype == 'novalue' then
        return i18n['novalue']
    elseif snak.snaktype == 'value' then
        return formatDatavalue( snak.datavalue, options )
    else
        return formatError( 'unknown-snak-type' )
    end
end
 
function formatDatavalue(datavalue, options)
    --Use the customize handler if provided
    if options['value-module'] or options['value-function'] then
        if not options['value-module'] or not options['value-function'] then
            return formatError( 'unknown-value-module' )
        end
        local formatter = require ('Module:' .. options['value-module'])
        if formatter == nil then
            return formatError( 'value-module-not-found' )
        end
        local fun = formatter[options['value-function']]
        if fun == nil then
            return formatError( 'value-function-not-found' )
        end
        return fun( datavalue.value, options )
    end
 
    --Default formatters
    -- ld. [[d:Help:Wikidata datamodel]]
    if datavalue.type == 'wikibase-entityid' then
        return formatEntityId(getEntityIdFromValue(datavalue.value), options)
    elseif datavalue.type == 'string' then
        return datavalue.value --TODO ids + media
    elseif datavalue.type == 'time' then
        return formatTimeValue(datavalue.value, options)
    elseif datavalue.type == 'globecoordinate' then
        if options.element == 'latitude' then
            return datavalue.value['latitude']
        elseif options.element == 'longitude' then
            return datavalue.value['longitude']
        else
            return formatError( 'globecoordinate-element' )
        end
    elseif datavalue.type == 'url' then
        return datavalue.value['url']
    elseif datavalue.type == 'quantity' then
        return options.format == 'raw' and datavalue.value.amount or mw.getContentLanguage():formatNum(tonumber(datavalue.value.amount))
    else
        return formatError('unknown-datavalue-type')
    end
end
 
function formatEntityId( entityId, options )
    local label = mw.wikibase.label( entityId )
    local link = mw.wikibase.sitelink( entityId )
    if link and options.link ~= 'nem' then
    	if options.link == 'csak' then
    		return link
        elseif label then
            return '[[' .. link .. '|' .. label .. ']]'
        else
            return '[[' .. link .. ']]'
        end
    else  -- if label then
        return label --TODO what if no links and label + fallback language?
    --[[
    else
        local frame = mw.getCurrentFrame()
        return frame:callParserFunction('#ifexist', entityId, '', entityId) .. 
            frame:expandTemplate{title = 'Wd', args = {entityId}} .. 
            ' [ [Kategória:Hivatkozás nem létező címkére] ]'
    --]]
    end
end
 
function formatTimeValue(value, options)
    if options.format == 'raw' then
        return value.time
    else
        local time = require('Modul:Time').newFromWikidataValue(value)
        if time then
            return time:formatDate(options)
        else
            return formatError('invalid-value') .. ' [[Kategória:Érvénytelen Wikidata-értékre hivatkozó lapok]]'
        end
    end
end
 
function p.formatStatements(frame)
    local args = getArgs(frame)
 
    --If a value if already set, use it
    if args.value and args.value ~= '' then
        return args.value ~= '-' and args.value or ''
    end
    return formatStatements(args)
end
 
p.RANK = {
    deprecated = 0,
    normal = 1,
    preferred = 2
}

function p.interwiki(frame) -- [[s:en:Module:Plain sister]]
	local args = getArgs(frame)
	local code = args.code
	local sites = {}
	local item = getEntityFromId(args.entityId)
	site = sites.code or code or nil
	if code then
		return item:getSitelink(site) or ''
	else return '' end
end

return p