Module:AuthorsDisplay
Appearance
Documentation for this module may be created at Module:AuthorsDisplay/doc
-- Module:AuthorsDisplay
-- Purpose: Display all authors linked to a Wikibase item via P2 (Has author),
-- along with each author's country from P14 (Has Country)
-- Usage: {{#invoke:AuthorsDisplay|main}}
local p = {}
--- Main function: Fetches authors from the current page's sitelinked Wikibase item
-- and renders them in a wiki table with their countries.
--
-- @function p.main
-- @param frame The Lua frame object (contains template arguments)
-- @return string A wiki table showing authors and their countries, or error message
function p.main(frame)
-- Fetch the Wikibase entity linked to the current MediaWiki page
-- mw.wikibase.getEntity() with no parameter gets the sitelinked item
local entity = mw.wikibase.getEntity()
-- Validate: Check if entity exists and has P2 (author) claims
if not entity or not entity.claims or not entity.claims.P2 then
return "<i>No authors found.</i>"
end
-- Extract all P2 (Has author) statements from the entity
local authors = entity.claims.P2
-- Initialize the wiki table with headers
local output = '{| class="wikitable" style="border-collapse: collapse;"\n'
output = output .. '|-\n! Author !! Country\n'
-- Loop through each author claim in P2
for _, claim in ipairs(authors) do
-- Verify this claim is a valid wikibase-item with an actual value
-- (not a "somevalue" or "novalue" placeholder)
if claim.mainsnak.datatype == "wikibase-item" and claim.mainsnak.snaktype == "value" then
-- Extract the item ID of the author (e.g., "Q5")
local authorId = claim.mainsnak.datavalue.value.id
-- Fetch the full Wikibase entity for this author
local authorEntity = mw.wikibase.getEntity(authorId)
-- Process this author only if the entity was successfully retrieved
if authorEntity then
-- Get the author's label (human-readable name)
-- Falls back to the item ID if no label exists
local authorLabel = authorEntity:getLabel() or authorId
-- Initialize country as "Unknown" (default value)
local country = "Unknown"
-- Try to retrieve P14 (country) from the author's statements
if authorEntity.claims and authorEntity.claims.P14 then
-- Get the first P14 claim (in case there are multiple)
local countryClaim = authorEntity.claims.P14[1]
-- Extract the country value only if it's a string datatype
if countryClaim and countryClaim.mainsnak.datatype == "string" then
country = countryClaim.mainsnak.datavalue.value
end
end
-- Append a new table row with author label and country
output = output .. '|-\n| ' .. authorLabel .. ' || ' .. country .. '\n'
end
end
end
-- Close the wiki table
output = output .. '|}\n'
-- Return the formatted wiki table as a string
return output
end
-- Return the module table containing the main function
return p