Module:MedicoInfobox

From determinar.ia.br - Determine suas informações
Revision as of 11:38, 1 August 2026 by Determinaradmin (talk | contribs)

Documentation for this module may be created at Module:MedicoInfobox/doc

local p = {}

local NOMES_PROPRIEDADE_FOTO = {
    ['foto'] = true,
    ['imagem'] = true,
    ['photo'] = true,
    ['image'] = true,
    ['picture'] = true,
}

local function formatarValor(dv, propLabelLower)
    if dv.type == 'string' then
        if NOMES_PROPRIEDADE_FOTO[propLabelLower] then
            return nil, dv.value
        end
        return dv.value
    elseif dv.type == 'monolingualtext' then
        return dv.value.text
    elseif dv.type == 'wikibase-entityid' then
        local id = dv.value.id
        local label = mw.wikibase.label(id) or id
        return '[[Item:' .. id .. '|' .. label .. ']]'
    elseif dv.type == 'quantity' then
        return dv.value.amount
    elseif dv.type == 'time' then
        return dv.value.time
    else
        return tostring(dv.value)
    end
end

function p.main(frame)
    local id = frame.args.ID or frame.args[1]
    local entity = mw.wikibase.getEntity(id)

    if not entity then
        return '<strong>Item não encontrado: ' .. tostring(id) .. '</strong>'
    end

    local label = entity:getLabel('pt-br') or entity:getLabel('pt') or entity:getLabel('en') or id
    local description = entity:getDescription('pt-br') or entity:getDescription('pt') or ''

    local imagem = nil
    local linhas = {}

    if entity.claims then
        for propertyId, claims in pairs(entity.claims) do
            local propLabel = mw.wikibase.label(propertyId) or propertyId
            local propLabelLower = mw.ustring.lower(propLabel)
            local valores = {}

            for _, claim in ipairs(claims) do
                local snak = claim.mainsnak
                if snak and snak.datavalue then
                    local formatado, arquivoImagem = formatarValor(snak.datavalue, propLabelLower)
                    if arquivoImagem then
                        imagem = arquivoImagem
                    elseif formatado then
                        table.insert(valores, formatado)
                    end
                end
            end

            if #valores > 0 then
                table.insert(linhas, { label = propLabel, valor = table.concat(valores, ', ') })
            end
        end
    end

    local html = mw.html.create('div'):addClass('infobox-medico')

    if imagem then
        html:tag('div')
            :addClass('infobox-medico-imagem')
            :wikitext('[[File:' .. imagem .. '|280px|center]]')
    end

    html:tag('div'):addClass('infobox-medico-titulo'):wikitext(label)

    if description ~= '' then
        html:tag('div'):addClass('infobox-medico-descricao'):wikitext(description)
    end

    local tabela = html:tag('table'):addClass('infobox-medico-tabela')
    for _, linha in ipairs(linhas) do
        local tr = tabela:tag('tr')
        tr:tag('th'):wikitext(linha.label)
        tr:tag('td'):wikitext(linha.valor)
    end

    return tostring(html)
end

return p