Module:Authors: Difference between revisions
Appearance
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p.list(frame) | function p.list(frame) | ||
-- Get the Wikibase entity for the current page | -- Get the Wikibase entity for the current page | ||
local entity = mw.wikibase.getEntity() | local entity = mw.wikibase.getEntity() | ||
if not entity then | if not entity then | ||
| Line 8: | Line 8: | ||
end | end | ||
-- Access | -- Access claims for P2 ("has authors") | ||
local claims = entity.claims or {} | local claims = entity.claims or {} | ||
local p2Claims = claims.P2 or {} | local p2Claims = claims.P2 or {} | ||
local authors = {} | local authors = {} | ||
-- Loop through each claim | -- Loop through each author claim | ||
for _, claim in ipairs(p2Claims) do | for _, claim in ipairs(p2Claims) do | ||
local value = claim.mainsnak.datavalue | local value = claim.mainsnak.datavalue | ||
if value and value.type == "wikibase-entityid" then | if value and value.type == "wikibase-entityid" then | ||
local itemId = value.value.id | local itemId = value.value.id | ||
-- Get | -- Get label and sitelink for the author | ||
local label = mw.wikibase.getLabel(itemId) or itemId | local label = mw.wikibase.getLabel(itemId) or itemId | ||
local sitelink = mw.wikibase.getSitelink(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 | if sitelink then | ||
table.insert(authors, "[[" .. sitelink .. "|" .. | table.insert(authors, "[[" .. sitelink .. "|" .. display .. "]]") | ||
else | else | ||
table.insert(authors, display) | |||
table.insert(authors, | |||
end | end | ||
end | end | ||
end | end | ||
if #authors == 0 then | if #authors == 0 then | ||
return "No authors found." | return "No authors found." | ||
end | end | ||
-- | -- Return as comma-separated list | ||
return table.concat(authors, ", ") | return table.concat(authors, ", ") | ||
end | end | ||
return p | return p | ||
Latest revision as of 13:46, 17 April 2026
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