Jump to content

Module:CapiuntoOrganization

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

local capiunto = require 'capiunto'
local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local qid = args.qid or args[1]

    if not qid then
        return '<span class="error">Error: No Wikidata QID provided</span>'
    end

    -- Register the entity association
    frame:callParserFunction('#unlinkedwikibase', 'id=' .. qid)

    -- Get entity data from UnlinkedWikibase
    local entity = mw.ext.UnlinkedWikibase.getEntity(qid)

    if not entity then
        return '<span class="error">Error: Entity ' .. qid ..
                   ' not found</span>'
    end

    -- Helper function to get property value from entity
    local function getPropertyValue(pid)
        if not entity.claims or not entity.claims[pid] then return nil end

        local claims = entity.claims[pid]
        if not claims or #claims == 0 then return nil end

        local claim = claims[1]
        if not claim.mainsnak or not claim.mainsnak.datavalue then
            return nil
        end

        return claim.mainsnak.datavalue.value
    end

    -- Helper function to format different value types
    local function formatValue(value, valueType)
        if not value then return nil end

        if type(value) == 'table' then
            -- Handle time values
            if value.time then
                -- Extract year-month-day from +2023-01-15T00:00:00Z format
                local dateStr = value.time
                local year, month, day =
                    dateStr:match('^[+-]?(%d+)-(%d+)-(%d+)')
                if year and month and day then
                    return string.format('%04d-%02d-%02d', tonumber(year),
                                         tonumber(month), tonumber(day))
                end
                return value.time
                -- Handle entity references (Q-IDs)
            elseif value.id then
                local linkedEntity = mw.ext.UnlinkedWikibase.getEntity(value.id)
                if linkedEntity then
                    local label = (linkedEntity.labels and
                                      linkedEntity.labels.en) and
                                      linkedEntity.labels.en.value or value.id
                    -- Then prefer an en.wikipedia sitelink (external link)
                    if linkedEntity.sitelinks and linkedEntity.sitelinks.enwiki and
                        linkedEntity.sitelinks.enwiki.url then
                        return
                            '[' .. linkedEntity.sitelinks.enwiki.url .. ' ' ..
                                label .. ']'
                    end
                    -- Fallback to the English label if available
                    if linkedEntity.labels and linkedEntity.labels.en then
                        return label
                    end
                end
                return value.id
                -- Handle monolingual text
            elseif value.text then
                return value.text
            end
        end

        return tostring(value)
    end

    -- Get the title (use English label or QID as fallback)
    local title = qid
    if entity.labels and entity.labels.en then title = entity.labels.en.value end

    -- Create the infobox
    local infobox = capiunto.create({
        title = title,
        bodyClass = 'infobox vevent',
        bodyStyle = 'width: 300px;'
    })

    -- Add official name (P1448)
    local officialName = getPropertyValue('P1448')
    if officialName then
        infobox:addRow('Official Name', formatValue(officialName))
    end

    -- Add logo (P154)
    local logo = getPropertyValue('P154')
    if logo then
        local logoStr = formatValue(logo)
        if logoStr then
            infobox:addImage('[[File:' .. logoStr .. '|150px]]', 'Logo')
        end
    end

    -- Add image (P18)
    local image = getPropertyValue('P18')
    if image then
        local imageStr = formatValue(image)
        if imageStr then
            infobox:addImage('[[File:' .. imageStr .. '|250px]]')
        end
    end

    -- Add headquarters (P159)
    local headquarters = getPropertyValue('P159')
    if headquarters then
        infobox:addRow('Headquarters', formatValue(headquarters))
    end

    -- Add founding date (P571)
    local foundingDate = getPropertyValue('P571')
    if foundingDate then
        infobox:addRow('Founding Date', formatValue(foundingDate))
    end

    -- Add dissolution date (P576)
    local dissolutionDate = getPropertyValue('P576')
    if dissolutionDate then
        infobox:addRow('Dissolution Date', formatValue(dissolutionDate))
    end

    -- Add country (P17)
    local country = getPropertyValue('P17')
    if country then infobox:addRow('Country', formatValue(country)) end

    -- Add parent organization (P749)
    local parentOrg = getPropertyValue('P749')
    if parentOrg then
        infobox:addRow('Parent Organization', formatValue(parentOrg))
    end

    -- Add official website (P856) as full-width row at bottom
    local website = getPropertyValue('P856')
    if website then
        local websiteStr = formatValue(website)
        if websiteStr then
            -- Strip http:// or https:// from display text
            local displayUrl = websiteStr:gsub('^https?://', '')
            infobox:addWikitext(
                '{| style="width:100%; text-align:center;"\n|-\n| [' ..
                    websiteStr .. ' ' .. displayUrl .. ']\n|}')
        end
    end

    return tostring(infobox)
end

return p