ವಿಷಯಕ್ಕೆ ಹೋಗು

ಮಾಡ್ಯೂಲ್:WikidataInfoboxperson

ವಿಕಿಪೀಡಿಯದಿಂದ, ಇದು ಮುಕ್ತ ಹಾಗೂ ಸ್ವತಂತ್ರ ವಿಶ್ವಕೋಶ

Documentation for this module may be created at ಮಾಡ್ಯೂಲ್:WikidataInfoboxperson/doc

local p = {}

-- Helper function to fetch data from Wikidata
local function getWikidataValue(property)
    local entity = mw.wikibase.getEntity() -- Get the current Wikidata entity
    if not entity then return nil end
    local claims = entity.claims[property] -- Get claims for the given property
    if not claims or #claims == 0 then return nil end
    local mainsnak = claims[1].mainsnak
    local datavalue = mainsnak and mainsnak.datavalue

    if not datavalue then return nil end

    -- Handle different data types
    if datavalue.type == "string" or datavalue.type == "number" then
        return datavalue.value
    elseif datavalue.type == "time" then
        return datavalue.value.time -- Extract the time string
    elseif datavalue.type == "wikibase-entityid" then
        return mw.wikibase.label(datavalue.value.id) -- Get the label for an entity ID
    else
        return nil -- Unsupported data type
    end
end

-- Main infobox function
function p.infobox(frame)
    local args = frame:getParent().args
    local infobox = mw.html.create('table') -- Create an HTML table
    infobox:addClass('infobox') -- Add the 'infobox' class for styling

    -- Title
    local title = args.title or mw.wikibase.getLabel() or "Unknown"
    infobox:tag('tr')
        :tag('th')
        :attr('colspan', '2')
        :wikitext(title)

    -- Example fields
    local fields = {
        { label = "Date of birth", property = "P569" }, -- P569 = date of birth
        { label = "Place of birth", property = "P19" }, -- P19 = place of birth
        { label = "Occupation", property = "P106" }    -- P106 = occupation
    }

    for _, field in ipairs(fields) do
        local value = getWikidataValue(field.property)
        if value then
            infobox:tag('tr')
                :tag('th'):wikitext(field.label):done()
                :tag('td'):wikitext(value):done()
        end
    end

    return tostring(infobox)
end

return p -- Export the module