Timeline

From Young Beethoven
Revision as of 21:11, 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 startDate = frame.args["startDate"]
	local timeRange = frame.args["range"]
	local fromDate = tostring(tonumber(lang:formatDate("Y", startDate))-timeRange).."-"..lang:formatDate("m-d", startDate)
	local toDate = tostring(tonumber(lang:formatDate("Y", startDate))+timeRange).."-"..lang:formatDate("m-d", startDate)
	
	local peopleBirthDates = mw.smw.ask("[[Category:Person]]|[[Person:Date of birth::>{{#time:"..fromDate.."}}]]|[[Person:Date of birth::<{{#time:"..toDate.."}}]]|?Person:Name|?Person:Date of birth")
	local peopleDeathDates = mw.smw.ask("[[Category:Person]]|[[Person:Date of death::>{{#time:"..fromDate.."}}]]|[[Person:Date of death::<{{#time:"..toDate.."}}]]|?Person:Name|?Person:Date of death")
	local compositionDates = mw.smw.ask("[[Category:Composition]]|[[Composition:Date::>{{#time:"..fromDate.."}}]]|[[Composition:Date::<{{#time:"..toDate.."}}]]|?Person:Title|?Composition:Date")
	
	local result = ""
	
	for num, row in pairs( peopleBirthDates ) do
		local date = row["Person:Date of birth"]
		local timestamp = tonumber(lang:formatDate("U", date))
		if not dates[timestamp] then
			dates[timestamp] = {}
			dates[timestamp]["date"] = date
		end
		dates[timestamp][#dates[timestamp]] = {
			row[1] .. " was born"
		}
	end
	for num, row in pairs( peopleDeathDates ) do
		local date = row["Person:Date of death"]
		local timestamp = tonumber(lang:formatDate("U", date))
		if not dates[timestamp] then
			dates[timestamp] = {}
			dates[timestamp]["date"] = date
		end
		dates[timestamp][#dates[timestamp]] = {
			row[1] .. " died"
		}
	end
	for num, row in pairs( compositionDates ) do
		local date = row["Composition:Date"]
		local timestamp = tonumber(lang:formatDate("U", date))
		if not dates[timestamp] then
			dates[timestamp] = {}
			dates[timestamp]["date"] = date
		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 .. dates[key]["date"] .. "<br>"
		for num, row in pairs( dates[key] ) do
			if type(row) == "table" then
				result = result .. row[1] ..'<br>'
			end
		end
		result = result ..'<br>'
	end
	
	return result
end

return p