Module:PublicationInfo
This module attempts to find a 4 digit year associated with a publication and add a category for the corresponding year and decade. It uses the header templates or Bahaidata as a source for the year. Later it can be expanded to search/categorize pages by other publication details.
It is referenced in the header template around sections for year and categories (in the header template adding the 'year' parameter on a subpage does not automatically categorize that page by year like it does on the parent page). We take advantage of this behavior by defining years for subpages within the "categories" parameter of the header template, and if it doesn't exist we call the getPublicationDate function (which is primarily used by the yeardecade template) to retrieve the year from Bahaidata. This scheme allows us to get the publication date from bahaidata, but override it with a different year by using the "categories" parameter if necessary.
local p = {}
-- For category pages using {{ROOTPAGENAME}}
function p.forCategories(frame)
local year = tonumber(frame.args[1])
if not year then
return ''
end
local decade = math.floor(year / 10) * 10
return string.format('[[Category:%ds]]', decade)
end
-- For parsing year from a string like "Books/History/2002" or just "1995"
function p.fromSplitParts(frame)
-- First try to find a year in the category parts
for i = 1, 10 do
local part = frame.args[tostring(i)]
if part and string.match(part, '^%d%d%d%d$') then
local year = tonumber(part)
if year then
local decade = math.floor(year / 10) * 10
return string.format('[[Category:%ds]]', decade)
end
end
end
-- If no year found in categories, try to get publication date
-- Call getPublicationDate directly with the same frame
return p.getPublicationDate(frame)
end
-- Return decade from year paramter in the header template
function p.fromYear(frame)
local year = tonumber(frame.args[1])
if not year then return '' end
local decade = math.floor(year / 10) * 10
return string.format('[[Category:%ss]]', decade)
end
-- Return year and decade on a subpage when parent is connected to bahaidata
local function getYearFromDate(claim)
if not claim or not claim.mainsnak or claim.mainsnak.datavalue == nil then
return nil
end
local timeValue = claim.mainsnak.datavalue.value.time
local year = tonumber(timeValue:match("(%-?%d+)"))
return year
end
function p.getPublicationDate(frame)
local baseTitle = mw.title.getCurrentTitle().rootText
local entityId = mw.wikibase.getEntityIdForTitle(baseTitle)
if not entityId then return "" end
local entity = mw.wikibase.getEntity(entityId)
if not entity or not entity.claims or not entity.claims.P29 then return "" end
local year = getYearFromDate(entity.claims.P29[1])
if not year then return "" end
local decade = math.floor(year / 10) * 10
return string.format('[[Category:%d]] [[Category:%ss]]', year, decade)
end
function p.getPublicationData(frame)
local currentTitle = mw.title.getCurrentTitle()
local baseTitle = currentTitle.rootText
-- Initialize categories table
local categories = {}
-- Get entities for current page and parent (base) page
local currentEntityId = mw.wikibase.getEntityIdForTitle(currentTitle.fullText)
local currentEntity = currentEntityId and mw.wikibase.getEntity(currentEntityId)
local parentEntityId = mw.wikibase.getEntityIdForTitle(baseTitle)
local parentEntity = parentEntityId and mw.wikibase.getEntity(parentEntityId)
-- Function to extract claims from an entity
local function getClaims(entity, property)
if entity and entity.claims and entity.claims[property] then
return entity.claims[property]
end
return nil
end
-- Year and decade category from parent entity
local dateClaims = getClaims(parentEntity, 'P29')
if dateClaims then
local year = getYearFromDate(dateClaims[1])
if year then
local decade = math.floor(year / 10) * 10
table.insert(categories, string.format('[[Category:%d]] [[Category:%ss]]', year, decade))
end
end
-- Author category - check current entity first, then parent entity
local authorClaims = getClaims(currentEntity, 'P10') or getClaims(parentEntity, 'P10')
if authorClaims then
for _, claim in ipairs(authorClaims) do
local datavalue = claim.mainsnak and claim.mainsnak.datavalue
if datavalue and datavalue.value and datavalue.value.id then
local authorId = datavalue.value.id
local authorName = mw.wikibase.getLabel(authorId)
if authorName then
table.insert(categories, string.format('[[Category:Text of works by %s]]', authorName))
end
end
end
end
return table.concat(categories, " ")
end
return p