Jump to content

Module:SongContestTable

From Wikipedia, the free encyclopedia

local getArgs = require('Module:Arguments').getArgs
local order = require('Module:SongContestData')._order

local rowTemplate = "Template:SongContestTableRow"

-- Attributes in Module:SongContestData
local dataAtts = {
	number = 'number',
	sf_draw = 'sf_draw',
	gf_draw = 'gf_draw',
}

local sortConfig = {
	--country/draw/number
	['contest-pcps'] = 'country',
	['round-result'] = 'draw',
	['history-list'] = 'number',
	['country-list'] = 'number',
}

local p = {}

local function pageExists(pageName)
    local title = mw.title.new(pageName)
    return title and title.exists or false
end

function p.main(f)
	local args = getArgs(f)
	local contest, year, roundInput = args[1], args[2], args[3]
	local wikitable = args['table']
	local sortInput = args['sort']
	
	if not pageExists(rowTemplate..'/'..wikitable) then return '<span class="error">Unknown table "'..wikitable..'" <small>([[Template:SongContestTable|help]])</small></span>' end
	
	local round, roundNo
	if roundInput then
		round, roundNo = roundInput:gsub("%d+", ""), tonumber(roundInput:match("%d+"))
	end
	
	local sortMethods = {
		country = nil,
		draw    = dataAtts[(round or 'gf') .. '_draw'],
		number  = dataAtts['number']
	}
	local sorting = sortMethods[sortInput] or sortMethods[sortConfig[wikitable]]
	
	local ordered = {}
	if roundNo then
		ordered = order({contest, year, sorting, desc=false, excludeAtt=round, excludeVal=roundNo, excludeInvert=true})
	else
		ordered = order({contest, year, sorting, desc=false})
	end
	
	-- build table from template
	local result = f:expandTemplate{title=rowTemplate,args={contest, year, roundInput, ['table'] = wikitable, header=1}}
	for _,entry in pairs(ordered) do
		result = result ..'\n'.. f:expandTemplate{title=rowTemplate,args={contest, year, entry, round, ['table'] = wikitable}}
	end
	return result..'\n|}'
end

return p