Vai al contenuto

Modulo:Controllo Wikidata

De Vichipedie, la enciclopedie libare dute in marilenghe.

La documentazione per questo modulo può essere creata in Modulo:Controllo Wikidata/man

local p = {}

-- Funzione principale per leggere singole proprietà da Wikidata
function p.main(frame)
    local args = frame.args
    if not args[1] or args[1] == "" then
        return ""
    end
    
    local property = args[1]:upper()
    local qid = args.qid or frame:getParent().args.qid
    
    local entity = nil
    if qid and qid ~= "" then
        entity = mw.wikibase.getEntity(qid)
    else
        entity = mw.wikibase.getEntity()
    end
    
    if not entity or not entity.claims or not entity.claims[property] then
        return ""
    end
    
    local claims = entity.claims[property]
    local results = {}
    
    for _, claim in ipairs(claims) do
        if claim.mainsnak and claim.mainsnak.snaktype == "value" then
            local datavalue = claim.mainsnak.datavalue
            local valtype = datavalue.type
            
            if valtype == "string" then
                table.insert(results, datavalue.value)
            elseif valtype == "wikibase-entityid" then
                local id = datavalue.value.id
                local item = mw.wikibase.getEntity(id)
                local label = item and item:getLabel() or id
                table.insert(results, "[[" .. label .. "]]")
            elseif valtype == "time" then
                table.insert(results, datavalue.value.time)
            end
        end
    end
    
    return table.concat(results, ", ")
end

-- Funzione per generare il Controllo di autorità
function p.authorityControl(frame)
    local qid = frame.args.qid or frame:getParent().args.qid
    local entity = nil
    
    if qid and qid ~= "" then
        entity = mw.wikibase.getEntity(qid)
    else
        entity = mw.wikibase.getEntity()
    end
    
    if not entity or not entity.claims then
        return ""
    end

    local properties = {
        { id = "P214", name = "VIAF", url = "https://viaf.org/viaf/" },
        { id = "P227", name = "GND", url = "https://d-nb.info/gnd/" },
        { id = "P244", name = "LCCN", url = "https://id.loc.gov/authorities/names/" },
        { id = "P396", name = "SBN", url = "https://opac.sbn.it/risultati-autori/-/opac-autori/detail/" },
        { id = "P213", name = "ISNI", url = "https://isni.org/isni/" }
    }

    local links = {}

    for _, prop in ipairs(properties) do
        local claims = entity.claims[prop.id]
        if claims and claims[1] and claims[1].mainsnak and claims[1].mainsnak.datavalue then
            local val = claims[1].mainsnak.datavalue.value
            if type(val) == "string" then
                table.insert(links, "<span>'''" .. prop.name .. "''': [" .. prop.url .. val .. " " .. val .. "]</span>")
            end
        end
    end

    if #links == 0 then
        return ""
    end

    local html = {}
    table.insert(html, '<div class="navbox" style="border:1px solid #aaa; background-color:#f9f9f9; padding:5px; margin-top:10px; font-size:90%; text-align:center;">')
    table.insert(html, '<span style="font-weight:bold; margin-right:10px;">Control di autoritât:</span>')
    table.insert(html, table.concat(links, " <span style=\"color:#aaa;\">•</span> "))
    table.insert(html, '</div>')

    return table.concat(html, "\n")
end

return p