Timeline

From Young Beethoven
Jump to navigation Jump to search
 
(26 intermediate revisions by the same user not shown)
Line 9: Line 9:
local dates = {}
local dates = {}
local peopleBirthDates = mw.smw.ask("[[Category:Person]]|?Person:Name|?Person:Date of birth")
local startDate = frame.args["startDate"]
local peopleDeathDates = mw.smw.ask("[[Category:Person]]|?Person:Name|?Person:Date of death")
local timeRange = tonumber(frame.args["range"])
local compositionDates = mw.smw.ask("[[Category:Composition]]|?Person:Title|?Composition:Date")
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 = ""
local result = ""
for num, row in pairs( peopleBirthDates ) do
if type(peopleBirthDates) == "table" then
dates[lang:formatDate("U", row["Person:Date of birth"])] = {
for num, row in pairs( peopleBirthDates ) do
row["Person:Name"] .. " was born"
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
end
for num, row in pairs( peopleDeathDates ) do
if type(peopleDeathDates) == "table" then
dates[lang:formatDate("U", row["Person:Date of death"])] = {
for num, row in pairs( peopleDeathDates ) do
row["Person:Name"] .. " died"
local date = row["Person:Date of death"]
}
local timestamp = tonumber(lang:formatDate("U", date))
end
if not dates[timestamp] then
for num, row in pairs( compositionDates ) do
dates[timestamp] = {}
dates[lang:formatDate("U", row["Composition:Date"])] = {
dates[timestamp]["date"] = date
row["Composition:Title"] .. " was composed"
end
}
dates[timestamp][#dates[timestamp]] = {
row[1] .. " died"
}
end
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 date, row in pairs( dates ) do
for _, key in ipairs( keys ) do
for num, row2 in pairs( row ) do
result = result .. "<b>" .. dates[key]["date"] .. "</b><br>"
result = result .. row2[0]
for num, row in pairs( dates[key] ) do
if type(row) == "table" then
result = result .. row[1] ..'<br>'
end
end
end
result = result ..'<br>'
end
end

Latest revision as of 21:36, 10 November 2022

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