Jump to content
Toggle menu
  • 568 articles
  • 1.5K files
  • 1 users
  • 13.6K edits
DemocracyCraft Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 22:17, 19 September 2026 by RylandW (talk | contribs) (for purposes of experimentation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

-- Get a DCRKI entity.
local function getEntity(qid)
    return mw.wikibase.getEntity(qid)
end

-- Get the label of an entity.
function p.label(frame)
    local qid = frame.args[1]
    if not qid or qid == "" then
        return ""
    end

    local entity = getEntity(qid)
    if not entity then
        return ""
    end

    return mw.wikibase.getLabel(qid) or ""
end

-- Get the first value of a property.
function p.value(frame)
    local qid = frame.args[1]
    local property = frame.args[2]

    if not qid or not property then
        return ""
    end

    local entity = getEntity(qid)
    if not entity or not entity.claims or not entity.claims[property] then
        return ""
    end

    local claims = entity.claims[property]

    for _, claim in ipairs(claims) do
        if claim.mainsnak
            and claim.mainsnak.datavalue
            and claim.mainsnak.datavalue.value then

            local value = claim.mainsnak.datavalue.value

            if type(value) == "table" and value.id then
                return mw.wikibase.getLabel(value.id) or value.id
            elseif type(value) == "string" then
                return value
            end
        end
    end

    return ""
end

-- Get all values of a property, separated by small bullets.
function p.values(frame)
    local qid = frame.args[1]
    local property = frame.args[2]

    if not qid or not property then
        return ""
    end

    local entity = getEntity(qid)
    if not entity or not entity.claims or not entity.claims[property] then
        return ""
    end

    local values = {}

    for _, claim in ipairs(entity.claims[property]) do
        if claim.mainsnak
            and claim.mainsnak.datavalue
            and claim.mainsnak.datavalue.value then

            local value = claim.mainsnak.datavalue.value

            if type(value) == "table" and value.id then
                local label = mw.wikibase.getLabel(value.id) or value.id
                table.insert(values, label)
            elseif type(value) == "string" then
                table.insert(values, value)
            end
        end
    end

    return table.concat(values, ' <small>•</small> ')
end

-- P96 → preferred P75 → P18 qualifier
function p.secretary(frame)
    local qid = frame.args[1]

    local entity = getEntity(qid)
    if not entity or not entity.claims or not entity.claims.P96 then
        return ""
    end

    -- Find the preferred P96 statement.
    local preferred

    for _, claim in ipairs(entity.claims.P96) do
        if claim.rank == "preferred"
            and claim.mainsnak
            and claim.mainsnak.datavalue then

            preferred = claim
            break
        end
    end

    -- Fall back to normal rank if no preferred statement exists.
    if not preferred then
        for _, claim in ipairs(entity.claims.P96) do
            if claim.rank ~= "deprecated"
                and claim.mainsnak
                and claim.mainsnak.datavalue then

                preferred = claim
                break
            end
        end
    end

    if not preferred then
        return ""
    end

    local value = preferred.mainsnak.datavalue.value

    if type(value) ~= "table" or not value.id then
        return ""
    end

    return mw.wikibase.getLabel(value.id) or value.id
end

-- P96 → preferred P75 → qualifier P18
function p.since(frame)
    local qid = frame.args[1]

    local entity = getEntity(qid)
    if not entity or not entity.claims or not entity.claims.P96 then
        return ""
    end

    local secretaryStatement

    -- Find preferred P96.
    for _, claim in ipairs(entity.claims.P96) do
        if claim.rank == "preferred"
            and claim.mainsnak
            and claim.mainsnak.datavalue then

            secretaryStatement = claim
            break
        end
    end

    -- Fall back if necessary.
    if not secretaryStatement then
        for _, claim in ipairs(entity.claims.P96) do
            if claim.rank ~= "deprecated"
                and claim.mainsnak
                and claim.mainsnak.datavalue then

                secretaryStatement = claim
                break
            end
        end
    end

    if not secretaryStatement then
        return ""
    end

    -- Get the secretary's Q-ID.
    local secretaryValue = secretaryStatement.mainsnak.datavalue.value

    if type(secretaryValue) ~= "table" or not secretaryValue.id then
        return ""
    end

    local secretaryQid = secretaryValue.id
    local secretary = getEntity(secretaryQid)

    if not secretary
        or not secretary.claims
        or not secretary.claims.P75 then
        return ""
    end

    -- Find preferred P75.
    local positionStatement

    for _, claim in ipairs(secretary.claims.P75) do
        if claim.rank == "preferred"
            and claim.mainsnak
            and claim.mainsnak.datavalue then

            positionStatement = claim
            break
        end
    end

    -- Fall back to normal rank.
    if not positionStatement then
        for _, claim in ipairs(secretary.claims.P75) do
            if claim.rank ~= "deprecated"
                and claim.mainsnak
                and claim.mainsnak.datavalue then

                positionStatement = claim
                break
            end
        end
    end

    if not positionStatement then
        return ""
    end

    -- Look for P18 qualifier on the P75 statement.
    if not positionStatement.qualifiers
        or not positionStatement.qualifiers.P18 then
        return ""
    end

    for _, qualifier in ipairs(positionStatement.qualifiers.P18) do
        if qualifier.datavalue and qualifier.datavalue.value then
            local value = qualifier.datavalue.value

            if type(value) == "string" then
                return value
            elseif type(value) == "table" and value.text then
                return value.text
            end
        end
    end

    return ""
end

return p