Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:AttractionsByProduct/doc. Changes can be proposed in the talk page.
Function list
L 12 — get_table
L 44 — get_past_locations
L 59 — create_table
L 194 — p.render_table

require("strict");
local tables = require('Module:Tables')
local getArgs = require('Module:Arguments').getArgs
local hatnote = require( 'Module:Hatnote' )._hatnote
local cargoUtils = require('Module:CargoUtils')
local tableUtils = require( 'Module:TableUtils' )
local getStatus = require( 'Module:Status' )
local cargo = mw.ext.cargo

local p = {}

local function get_table(args)
	local tables
	if (args.type == 'Coaster') then
		tables = 'Roller_Coasters'
	elseif (args.type == 'Ride') then
		tables = 'Rides'
	elseif (args.type == 'Water Slide') then
		tables = 'Water_Slides'
	else
		error("expected 'type' argument to be one of 'Coaster', 'Ride', or 'Water Slide', instead got '" .. args.type .. "'")
	end
	
    local fields = '_pageTitle,Name,Park,Country,YEAR(Opening)=Opening,YEAR(Opened)=Opened,Opened__precision,Status,YEAR(Closed)=Closed,Closed__precision'
    if (args.type ~= 'Water Slide') then
    	fields = fields .. ',Operator'
    end
    if (args.Inversions ~= nil) then
    	fields = fields .. ',Inversions'
    end
    local where = 'Product LIKE "%[' .. args.pageName .. '%" AND _pageNamespace = 0'
    local orderBy = 'Name ASC,Park ASC'
    local groupBy = '_pageTitle'

    local tableargs = {
    	where = where,
        limit = 5000,
        orderBy = orderBy,
        groupBy = groupBy
    }
    return cargo.query( tables, fields, tableargs )
end

local function get_past_locations(pageTitle)
	local tables = 'Roller_Coaster_Past_Locations'
	
    local fields = 'Name,Park,Operator,Country,YEAR(Opened)=Opened,Opened__precision,YEAR(Closed)=Closed,Closed__precision'
    local where = '_pageTitle ="' .. pageTitle .. '" AND _pageNamespace = 0'
    local orderBy = 'Ordering ASC'

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

local function create_table( args )
    local mwtable = mw.html.create('table'):addClass('wikitable sortable')
    local tabledata = get_table(args)
    local lang = mw.language.new("en")

    if (#tabledata == 0) then
    	return hatnote(	"No results found. If you know something that should be here, please [[Coasterpedia:Article_Creator|create a page]] and it will appear here. Also, please check that the product field in the relevant infoboxes links directly to this article.", { icon='WikimediaUI-Notice.svg' } )
    end
    local data = {{}}
    
    -- Build header row
    table.insert(data[1], {text = 'Name', tag = 'th'})
	table.insert(data[1], {text = 'Park', tag = 'th'})
    table.insert(data[1], {text = 'Country', tag = 'th'})
    if (args.Inversions ~= nil) then
    	table.insert(data[1], {text = 'Inversions', tag = 'th'})
    end
    table.insert(data[1], {text = 'Opened', tag = 'th'})
	table.insert(data[1], {text = 'Status', tag = 'th'})

	-- Build table body from Cargo query results
    for i=1,#tabledata do
		data[i+1] = {}
		local pastLocations = get_past_locations(tabledata[i]._pageTitle)

		-- Generate name link using name field and page title as page title can often include location
		local name = '[[' .. tabledata[i]._pageTitle .. '|' .. tabledata[i].Name .. ']]'
		
		local park
		if (tabledata[i].Park == nil and tabledata[i].Operator == nil) then
			park = "Unknown"
		elseif (tabledata[i].Park == nil and tabledata[i].Operator ~= nil) then
			park = tabledata[i].Operator
		else
			park = tabledata[i].Park
		end	
		    
		local country
		if (tabledata[i].Country ~= nil) then
			country = '<div class="flag-icon" style="display:inline-block; padding:0">[[File:'.. tabledata[i].Country .. '.svg|link='.. tabledata[i].Country .. '|27px]] '.. tabledata[i].Country .. '</div>'
		else
			country = "Unknown"
		end
		
		local inversions
		if (args.Inversions ~= nil) then
			if (tabledata[i].Inversions ~= nil) then
				inversions = tabledata[i].Inversions
			else
				inversions = "Unknown"
			end
		end
		local opened
		if (tabledata[i].Opened ~= nil) then
			opened = tabledata[i].Opened
		else
			opened = "Unknown"
		end
		
		local status
		if (tabledata[i].Status ~= nil) then
			if (tabledata[i].Status == "operating") then
				status = getStatus.get_status(tabledata[i].Status)
			elseif (tabledata[i].Status == "under construction") then
				if (tabledata[i].Opening ~= nil) then
					status = "[[Under construction]], expected to open " .. tabledata[i].Opening
				else
					status = "[[Under construction]]"
				end
			elseif (tabledata[i].Status == "standing but not operating") then
				if (tabledata[i].Closed ~= nil) then
					status = "[[Standing but not operating]], closed " .. tabledata[i].Closed
				else
					status = "[[Standing but not operating]]"
				end
			elseif (tabledata[i].Status == "defunct") then
				mw.logObject(tabledata[i].Closed)
				if (tabledata[i].Closed ~= nil) then
					status = "Closed " .. tabledata[i].Closed
				else
					status = "Closed"
				end
			end
		else
			status = "Unknown"
		end
		
		for i=1,#pastLocations do
			if pastLocations[i].Name ~= nil then
				name = name ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext("Formerly " .. pastLocations[i].Name))
			else
				name = name ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext("Formerly"))
			end
			
			if pastLocations[i].Park ~= nil then
				park = park ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext(pastLocations[i].Park))
			elseif pastLocations[i].Operator ~= nil then
				park = park ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext(pastLocations[i].Operator))
			else
				park = park ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext('Unknown'))
			end
			
			if pastLocations[i].Country ~= nil then
				country = country ..'<br><div class="flag-icon" style="display:inline-block; padding:0">[[File:'.. pastLocations[i].Country .. '.svg|link='.. pastLocations[i].Country .. '|27px]] '.. pastLocations[i].Country .. '</div>'
			else
				country = country ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext('Unknown'))
			end
			
			if pastLocations[i].Opened ~= nil then
				opened = opened ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext(pastLocations[i].Opened))
			else
				opened = opened ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext('Unknown'))
			end
			
			if pastLocations[i].Closed ~= nil then
				status = status ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext('Closed ' .. pastLocations[i].Closed))
			else
				status = status ..'<br>' .. tostring(mw.html.create():tag('small'):wikitext('Closed'))
			end
		end
		
		table.insert(data[i+1], {text = name})
		table.insert(data[i+1], {text = park})
		table.insert(data[i+1], {text = country})
		if (args.Inversions ~= nil) then
			table.insert(data[i+1], {text = inversions})
		end
		table.insert(data[i+1], {text = opened})
		table.insert(data[i+1], {text = status})
    end

    tables._table(mwtable, data)
	return tostring(mwtable)
end

function p.render_table( frame )
	local args = getArgs(frame)

	local htmlOutput = mw.html.create('div')
	hatnote("This table is generated automatically from articles where the Product field links directly to this page. If something is missing, please [[Coasterpedia:Article_Creator|create the article]] or leave a note on the talk page.")
	htmlOutput:wikitext(create_table({ type = args.Type, pageName = mw.title.getCurrentTitle().text, Inversions=args.Inversions}))
	return htmlOutput
end

return p