Module:OnThisDay

Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:OnThisDay/doc. Changes can be proposed in the talk page.
Function list
L 10 — ride_name
L 15 — coaster_category
L 34 — ride_category
L 45 — water_slide_category
L 72 — opened_text
L 158 — get_table
L 225 — add_year_table
L 234 — p.render_onthisday

require("strict");
local tablesModule = require('Module:Tables')
local listModule = require('Module:List')
local aOrAnModule = require('Module:A or an')
local cargo = mw.ext.cargo
local lang = mw.language.getContentLanguage()

local p = {}

local function ride_name(name, pageTitle)
	return '[[' .. pageTitle .. '|' .. name .. ']]'
end

-- Text generation
local function coaster_category(category1, category2, category3, class)
	local category = ','
	if category1 ~= nil then
		category = category .. ' ' .. aOrAnModule._main({'[[' .. category1 .. '|' .. lang:lc(category1) .. ']]'})
	end
	if category2 ~= nil then
		category = category.. ' ' .. '[[' .. category2 .. '|' .. lang:lc(category2) .. ']]'
	end
	if category3 ~= nil then
		category = category.. ' ' .. '[[' .. category3 .. '|' .. lang:lc(category3) .. ']]'
	end
	if class == nil then
		category = category .. ' [[roller coaster]]'
	else
		category = category .. ' [[' .. class .. ' coaster]]'
	end
	return category
end

local function ride_category(category1, product)
	local category =  ','
	if category1 ~= nil then
		category = category .. ' ' .. aOrAnModule._main({category1})
	elseif product ~= nil then
		category = category .. ' ' .. aOrAnModule._main({product})
	end
	category = category .. ' ride'
	return category
end

local function water_slide_category(class, category1, category2, category3, category4, category5, category6)
	local category = ','
	if class ~= nil then
		category = category .. ' ' .. aOrAnModule._main({'[[' .. class .. '|' .. lang:lc(class) .. ']]'})
	end
	if category2 ~= nil then
		category = category.. ' ' .. '[[' .. category1 .. '|' .. lang:lc(category1) .. ']]'
	end
	if category2 ~= nil then
		category = category.. ' ' .. '[[' .. category2 .. '|' .. lang:lc(category2) .. ']]'
	end
	if category3 ~= nil then
		category = category.. ' ' .. '[[' .. category3 .. '|' .. lang:lc(category3) .. ']]'
	end
	if category4 ~= nil then
		category = category.. ' ' .. '[[' .. category4 .. '|' .. lang:lc(category4) .. ']]'
	end
	if category5 ~= nil then
		category = category.. ' ' .. '[[' .. category5 .. '|' .. lang:lc(category5) .. ']]'
	end
	if category6 ~= nil then
		category = category.. ' ' .. '[[' .. category6 .. '|' .. lang:lc(category6) .. ']]'
	end
	category = category .. ' water slide'
	return category
end

local function opened_text(value)
	local listItems = {}

	for i=1,#value do
		listItems[i] = ''

		-- Ride Name
		local nameLink
		if value[i].Name ~= nil then
			nameLink = ride_name(value[i].Name, value[i]._pageTitle)
		elseif value[i].RideName ~= nil then
			nameLink = ride_name(value[i].RideName, value[i]._pageTitle)
		else
			nameLink = ride_name(value[i].CoasterName, value[i]._pageTitle)
		end
		listItems[i] = listItems[i] .. nameLink

		-- Categories for coasters
		if value[i].TableName == 'Roller_Coasters' then
			listItems[i] = listItems[i] .. coaster_category(value[i].Category1, value[i].Category2, value[i].Category3, value[i].Class)
		end

		if value[i].TableName == 'Roller_Coaster_Past_Locations' and value[i].CoasterName ~= nil then
			listItems[i] = listItems[i] .. coaster_category(value[i].CoasterCategory1, value[i].CoasterCategory2, value[i].CoasterCategory3, value[i].Class)
		end

		-- Categories for rides
		if value[i].TableName == 'Rides' then
			listItems[i] = listItems[i] .. ride_category(value[i].Category1, value[i].Product)
		end

		if value[i].TableName == 'Roller_Coaster_Past_Locations' and value[i].RideName ~= nil then
			listItems[i] = listItems[i] .. ride_category(value[i].RideCategory1, value[i].RideProduct)
		end
		
		-- Categories for water slides
		if value[i].TableName == 'Water_Slides' then
			listItems[i] = listItems[i] .. water_slide_category(value[i].Class, value[i].Category1, value[i].Category2, value[i].Category3, value[i].Category4, value[i].Category5, value[i].Category6)
		end

		if value[i].TableName == 'Roller_Coaster_Past_Locations' and value[i].WaterSlideName ~= nil then
			listItems[i] = listItems[i] .. water_slide_category(value[i].WaterSlideClass, value[i].WaterSlideCategory1, value[i].WaterSlideCategory2, value[i].WaterSlideCategory3, value[i].WaterSlideCategory4, value[i].WaterSlideCategory5, value[i].WaterSlideCategory6)
		end

		if value[i].Manufacturer ~= nil then
			listItems[i] = listItems[i] .. ' manufactured by ' .. value[i].Manufacturer
		elseif value[i].CoasterManufacturer ~= nil then
			listItems[i] = listItems[i] .. ' manufactured by ' .. value[i].CoasterManufacturer
		elseif value[i].RideManufacturer ~= nil then
			listItems[i] = listItems[i] .. ' manufactured by ' .. value[i].RideManufacturer
		elseif value[i].WaterSlideManufacturer ~= nil then
			listItems[i] = listItems[i] .. ' manufactured by ' .. value[i].WaterSlideManufacturer
		elseif value[i].Builder ~= nil then
			listItems[i] = listItems[i] .. ' manufactured by ' .. value[i].Builder
		elseif value[i].CoasterBuilder ~= nil then
			listItems[i] = listItems[i] .. ' manufactured by ' .. value[i].CoasterBuilder
		elseif value[i].RideBuilder ~= nil then
			listItems[i] = listItems[i] .. ' manufactured by ' .. value[i].RideBuilder
		end
		if value[i].TableName ~= 'Amusement_Parks' then
			listItems[i] = listItems[i] .. ','
		end
		if value[i].Type == 'Opened' then
			listItems[i] = listItems[i] .. ' opens'
		end
		if value[i].Type == 'Closed' then
			listItems[i] = listItems[i] .. ' closes'
			if value[i].Duration ~= nil then
				if (value[i].Duration == '0') then
					listItems[i] = listItems[i] .. ' after less than one year of operation'
				else
					listItems[i] = listItems[i] .. ' after ' .. value[i].Duration .. ' years of operation'
				end
			end
		end
		if value[i].Park ~= nil then
			listItems[i] = listItems[i] .. ' at ' .. value[i].Park
		end
		if value[i].Location ~= nil then
			listItems[i] = listItems[i] .. ' in ' .. value[i].Location
		end
	end
 	return listModule.bulleted(listItems)
end

-- Cargo query
local function get_table(tableName, type, day, month)
	local tables = tableName
	local fields
	local where
	local join
	if tableName == 'Roller_Coaster_Past_Locations' then
		tables = tables .. ', Rides, Roller_Coasters, Water_Slides'
		fields = string.format([=[
			Roller_Coaster_Past_Locations._pageTitle=_pageTitle,
			Roller_Coaster_Past_Locations.Name=Name,
			Roller_Coaster_Past_Locations.Location=Location,
			Roller_Coaster_Past_Locations.Park=Park,
			YEAR(Roller_Coaster_Past_Locations.%s)=Year,
			IF(1=1, "%s", NULL)=TableName,
			IF(1=1, "%s", NULL)=Type,
			Rides.Name=RideName,
			Rides.Category1=RideCategory1,
			Rides.Product=RideProduct,
			Rides.Manufacturer=RideManufacturer,
			Rides.Builder=RideBuilder,
			Water_Slides.Name=WaterSlideName,
			Water_Slides.Class=WaterSlideClass,
			Water_Slides.Category1=WaterSlideCategory1,
			Water_Slides.Category2=WaterSlideCategory2,
			Water_Slides.Category3=WaterSlideCategory3,
			Water_Slides.Category4=WaterSlideCategory4,
			Water_Slides.Category5=WaterSlideCategory5,
			Water_Slides.Category6=WaterSlideCategory6,
			Water_Slides.Product=WaterSlideProduct,
			Water_Slides.Manufacturer=WaterSlideManufacturer,
			Roller_Coasters.Name=CoasterName,
			CONCAT(Roller_Coasters.Category1)=CoasterCategory1,
			CONCAT(Roller_Coasters.Category2)=CoasterCategory2,
			CONCAT(Roller_Coasters.Category3)=CoasterCategory3,
			Roller_Coasters.Manufacturer=CoasterManufacturer,
			Roller_Coasters.Builder=CoasterBuilder,
			Roller_Coasters.Class=Class
		]=], type, tableName, type)
		if type == 'Closed' then
			fields = fields .. ',TIMESTAMPDIFF(YEAR, Roller_Coaster_Past_Locations.Opened, Roller_Coaster_Past_Locations.Closed)=Duration'
		end
		where = string.format('DAYOFMONTH(Roller_Coaster_Past_Locations.%s) = %s AND MONTH(Roller_Coaster_Past_Locations.%s) = %s AND Roller_Coaster_Past_Locations.%s__precision <= 1', type, day, type, month, type)
		join = 'Roller_Coaster_Past_Locations._pageID = Rides._pageID, Roller_Coaster_Past_Locations._pageID = Roller_Coasters._pageID, Roller_Coaster_Past_Locations._pageID = Water_Slides._pageID'
	else
		fields = string.format('_pageTitle,Name,Location,YEAR(%s)=Year,IF(1=1, "%s", NULL)=TableName,IF(1=1, "%s", NULL)=Type', type, tableName, type)
		if tableName == 'Roller_Coasters' or tableName == 'Rides' then
			fields = fields .. ',Category1,Category2,Category3,Park,Manufacturer,Builder,Product'
		end
		if tableName == 'Water_Slides' then
			fields = fields .. ',Class,Category1,Category2,Category3,Category4,Category5,Category6,Park,Manufacturer'
		end
		if tableName == 'Roller_Coasters' then
			fields = fields .. ',Class'
		end
		if type == 'Closed' then
			fields = fields .. ',TIMESTAMPDIFF(YEAR, Opened, Closed)=Duration'
		end
	    where = string.format('DAYOFMONTH(%s) = %s AND MONTH(%s) = %s AND %s__precision <= 1', type, day, type, month, type)
	end

 	local tableargs = {
    	where = where,
    	join = join
    }
    return cargo.query( tables, fields, tableargs )
end

local function add_year_table(sortedTable, newData)
	for i = 1, #newData do
    	if sortedTable[newData[i].Year] == nil then
			sortedTable[newData[i].Year] = {}
		end
		table.insert(sortedTable[newData[i].Year], newData[i])
	end
end

function p.render_onthisday(frame)
	-- Get the date for the current on this day page, otherwise default to current date
	local day = os.date('%d')
	local month = os.date('%m')
	if (mw.title.getCurrentTitle().rootText == 'On this day') then
		local pageName = mw.title.getCurrentTitle().subpageText
		local pageDate = mw.text.split(pageName, "%s")
		day = pageDate[2]
	 	month = pageDate[1]
	end

	-- Fetch all data
	local openedCoasters = get_table('Roller_Coasters', 'Opened', day, month)
	local closedCoasters = get_table('Roller_Coasters', 'Closed', day, month)
	local openedRelocated = get_table('Roller_Coaster_Past_Locations', 'Opened', day, month)
	local closedRelocated = get_table('Roller_Coaster_Past_Locations', 'Closed', day, month)
	local openedRides = get_table('Rides', 'Opened', day, month)
	local closedRides = get_table('Rides', 'Closed', day, month)
	local openedWaterSlides = get_table('Water_Slides', 'Opened', day, month)
	local closedWaterSlides = get_table('Water_Slides', 'Closed', day, month)
	local openedParks = get_table('Amusement_Parks', 'Opened', day, month)
	local closedParks = get_table('Amusement_Parks', 'Closed', day, month)

	-- Group events by year
    local yearSortedTable = {}
    add_year_table(yearSortedTable, openedCoasters)
    add_year_table(yearSortedTable, closedCoasters)
    add_year_table(yearSortedTable, openedRelocated)
    add_year_table(yearSortedTable, closedRelocated)
    add_year_table(yearSortedTable, openedRides)
    add_year_table(yearSortedTable, closedRides)
    add_year_table(yearSortedTable, openedWaterSlides)
    add_year_table(yearSortedTable, closedWaterSlides)
    add_year_table(yearSortedTable, openedParks)
    add_year_table(yearSortedTable, closedParks)

    -- Sort by year descending
    local tkeys = {}
    for k in pairs(yearSortedTable) do table.insert(tkeys, k) end
    table.sort(tkeys, function(a,b) return a > b end)
    
    -- Output timeline
    local mwtable = mw.html.create('table'):addClass('wikitable timeline')
    local data = {{}}
    for _, k in ipairs(tkeys) do
    	table.insert(data, {{text = k},{text = opened_text(yearSortedTable[k])}})
	end
	tablesModule._table(mwtable, data)
	return tostring(mwtable)
end

return p