Module:AuthorsDisplay: Difference between revisions
Appearance
Created page with "local p = {} function p.main(frame) local item = frame.args.item or "Q14" local entity = mw.wikibase.getEntity(item) -- Check if entity exists and has P2 claims if not entity or not entity.claims or not entity.claims.P2 then return "<i>No authors found.</i>" end local authors = entity.claims.P2 local output = '{| class="wikitable" style="border-collapse: collapse;"\n' output = output .. '|-\n! Author !! Country\n'..." |
No edit summary |
||
| Line 1: | Line 1: | ||
-- 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 = {} | 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) | function p.main(frame) | ||
-- Check if entity exists and has P2 claims | -- 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 | if not entity or not entity.claims or not entity.claims.P2 then | ||
return "<i>No authors found.</i>" | return "<i>No authors found.</i>" | ||
end | end | ||
-- Extract all P2 (Has author) statements from the entity | |||
local authors = entity.claims.P2 | local authors = entity.claims.P2 | ||
-- Initialize the wiki table with headers | |||
local output = '{| class="wikitable" style="border-collapse: collapse;"\n' | local output = '{| class="wikitable" style="border-collapse: collapse;"\n' | ||
output = output .. '|-\n! Author !! Country\n' | output = output .. '|-\n! Author !! Country\n' | ||
-- Loop through each P2 | -- Loop through each author claim in P2 | ||
for _, claim in ipairs(authors) do | for _, claim in ipairs(authors) do | ||
-- Verify | |||
-- 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 | 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 | local authorId = claim.mainsnak.datavalue.value.id | ||
-- Fetch the full Wikibase entity for this author | |||
local authorEntity = mw.wikibase.getEntity(authorId) | local authorEntity = mw.wikibase.getEntity(authorId) | ||
-- Process this author only if the entity was successfully retrieved | |||
if authorEntity then | if authorEntity then | ||
-- Get the author's label ( | |||
-- Get the author's label (human-readable name) | |||
-- Falls back to the item ID if no label exists | |||
local authorLabel = authorEntity:getLabel() or authorId | local authorLabel = authorEntity:getLabel() or authorId | ||
-- Initialize country as "Unknown" | -- Initialize country as "Unknown" (default value) | ||
local country = "Unknown" | local country = "Unknown" | ||
-- Try to | -- Try to retrieve P14 (country) from the author's statements | ||
if authorEntity.claims and authorEntity.claims.P14 then | if authorEntity.claims and authorEntity.claims.P14 then | ||
-- Get the first P14 claim (in case there are multiple) | |||
local countryClaim = authorEntity.claims.P14[1] | 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 | if countryClaim and countryClaim.mainsnak.datatype == "string" then | ||
country = countryClaim.mainsnak.datavalue.value | country = countryClaim.mainsnak.datavalue.value | ||
| Line 36: | Line 65: | ||
end | end | ||
-- | -- Append a new table row with author label and country | ||
output = output .. '|-\n| ' .. authorLabel .. ' || ' .. country .. '\n' | output = output .. '|-\n| ' .. authorLabel .. ' || ' .. country .. '\n' | ||
end | end | ||
| Line 42: | Line 71: | ||
end | end | ||
-- Close the wiki table | |||
output = output .. '|}\n' | output = output .. '|}\n' | ||
-- Return the formatted wiki table as a string | |||
return output | return output | ||
end | end | ||
-- Return the module table containing the main function | |||
return p | return p | ||
Latest revision as of 10:44, 20 April 2026
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