Timeline

From Young Beethoven
Revision as of 20:46, 10 November 2022 by Admin (talk | contribs)
Jump to navigation Jump to search

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

local p = {}
local lang = mw.language.new("en")

function p.render( frame )
	if not mw.smw then
        return "mw.smw module not found"
	end
	
	local dates = {}
	
	local peopleBirthDates = mw.smw.ask("[[Category:Person]]|?Person:Name|?Person:Date of birth")
	local peopleDeathDates = mw.smw.ask("[[Category:Person]]|?Person:Name|?Person:Date of death")
	local compositionDates = mw.smw.ask("[[Category:Composition]]|?Person:Title|?Composition:Date")
	
	local result = ""
	
	for num, row in pairs( peopleBirthDates ) do
		local timestamp = tonumber(lang:formatDate("U", row["Person:Date of birth"]))
		if not dates[timestamp] then
			dates[timestamp] = {}	
		end
		dates[timestamp][#dates[timestamp]] = {
			row[1] .. " was born"
		}
	end
	for num, row in pairs( peopleDeathDates ) do
		local timestamp = tonumber(lang:formatDate("U", row["Person:Date of death"]))
		if not dates[timestamp] then
			dates[timestamp] = {}	
		end
		dates[timestamp][#dates[timestamp]] = {
			row[1] .. " died"
		}
	end
	for num, row in pairs( compositionDates ) do
		local timestamp = tonumber(lang:formatDate("U", row["Composition:Date"]))
		if not dates[timestamp] then
			dates[timestamp] = {}	
		end
		dates[timestamp][#dates[timestamp]] = {
			row[1] .. " was composed"
		}
	end
	
	local keys = {}
	for k in pairs(dates) do table.insert(keys, k) end
	table.sort(keys)
	
	for _, key in ipairs( keys ) do
		result = result .. lang:formatDate("Y-m-d", key)
		for num, row in pairs( dates[key] ) do
			result = result .. row[1]
		end
	end
	
	return result
end

return p