Jump to content

Module:Authors

From wikibase
Revision as of 13:46, 17 April 2026 by Design (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

function p.list(frame)
    -- Get the Wikibase entity for the current page
    local entity = mw.wikibase.getEntity()
    if not entity then
        return "No Wikibase entity found for this page."
    end
    
    -- Access claims for P2 ("has authors")
    local claims = entity.claims or {}
    local p2Claims = claims.P2 or {}
    
    local authors = {}
    -- Loop through each author claim
    for _, claim in ipairs(p2Claims) do
        local value = claim.mainsnak.datavalue
        if value and value.type == "wikibase-entityid" then
            local itemId = value.value.id
            -- Get label and sitelink for the author
            local label = mw.wikibase.getLabel(itemId) or itemId
            local sitelink = mw.wikibase.getSitelink(itemId)
            
            -- Get the author's entity to fetch P5
            local authorEntity = mw.wikibase.getEntity(itemId)
            local p5Value = ""
            if authorEntity and authorEntity.claims and authorEntity.claims.P5 then
                -- Assume P5 is a string property; take the first value
                local p5Claim = authorEntity.claims.P5[1]
                if p5Claim and p5Claim.mainsnak.datavalue then
                    p5Value = p5Claim.mainsnak.datavalue.value or ""
                end
            end
            
            -- Format: link to author, with P5 in parentheses if available
            local display = label
            if p5Value ~= "" then
                display = display .. " (" .. p5Value .. ")"
            end
            if sitelink then
                table.insert(authors, "[[" .. sitelink .. "|" .. display .. "]]")
            else
                table.insert(authors, display)
            end
        end
    end
    
    if #authors == 0 then
        return "No authors found."
    end
    
    -- Return as comma-separated list
    return table.concat(authors, ", ")
end

return p