Timeline

From Young Beethoven
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 = tonumber(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:Date of birth")
	local peopleDeathDates = mw.smw.ask("[[Category:Person]][[Person:Date of death::>{{#time:"..fromDate.."}}]][[Person:Date of death::<{{#time:"..toDate.."}}]]|?Person:Date of death")
	local compositionDates = mw.smw.ask("[[Category:Composition]][[Composition:Date::>{{#time:"..fromDate.."}}]][[Composition:Date::<{{#time:"..toDate.."}}]]|?Composition:Date")
	local sourceDates = mw.smw.ask("[[Category:Primary Source]][[Source:Date::>{{#time:"..fromDate.."}}]][[Source:Date::<{{#time:"..toDate.."}}]]|?Source:Date")
	local landscapeDates = mw.smw.ask("[[Category:Landscape]][[Landscape:Year::>{{#time:"..fromDate.."}}]][[Landscape:Year::<{{#time:"..toDate.."}}]]|?Landscape:Year")
	
	local result = ""
	
	if type(peopleBirthDates) == "table" then
		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
	end
	if type(peopleDeathDates) == "table" then
		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
	end
	if type(compositionDates) == "table" then
		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
	end	
	if type(sourceDates) == "table" then
		for num, row in pairs( sourceDates ) do
			local date = row["Source: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]
			}
		end
	end	
	if type(landscapeDates) == "table" then
		for num, row in pairs( landscapeDates ) do
			local date = row["Landscape:Year"]
			local timestamp = tonumber(lang:formatDate("U", date.."-01-01"))
			if not dates[timestamp] then
				dates[timestamp] = {}
				dates[timestamp]["date"] = date
			end
			dates[timestamp][#dates[timestamp]] = {
				row[1]
			}
		end
	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 .. "<b>" .. dates[key]["date"] .. "</b><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