Moodul:Koroonapandeemia riigiti

Allikas: Vikipeedia
Mine navigeerimisribale Mine otsikasti
Template-info.svg Mooduli dokumentatsioon

Rakendab Mall:Koroonapandeemia riigiti andmed mudeli andmetega: Mall:Koroonapandeemia riigiti/andmed. Palun küsige Tol, kui vajate abi.

local p = {}

local data_title = mw.title.new(
	'Mall:Koroonapandeemia riigiti/andmed'
)
local data_content = data_title:getContent()
local data = mw.text.jsonDecode(data_content)

function format_num(number)
	if not number then
		return ''
	end
	local _, _, minus, int, _ = tostring(number):find('([-]?)(%d+)([.]?%d*)')
	int = int:reverse():gsub("(%d%d%d)", "%1,")
	int = int:reverse():gsub("^,", "")
	int = minus .. int
	return int
end

function p.main()
	local out = ''
	local data_s = {}
	-- Build new table
	local locations = {}
	if mw.getCurrentFrame().args['locations'] then
		for location in string.gmatch(
			mw.getCurrentFrame().args['locations'],
			'([^,]+)'
		) do
			locations[location] = true
		end
	else
		locations = nil
	end
	for key, value in pairs(data) do
		local index
		if mw.getCurrentFrame().args['sort'] then
			if value[mw.getCurrentFrame().args['sort']] then
				index = value[mw.getCurrentFrame().args['sort']]
			else
				index = 0
			end
		else
			if value.cases then
				index = value.cases
			elseif value.deaths then
				index = value.deaths
			elseif value.vaccine_doses then
				index = value.vaccine_doses
			elseif value.total_vaccinated then
				index = value.total_vaccinated
			elseif value.fully_vaccinated then
				index = value.fully_vaccinated
			else index = 0
			end
		end
		if (not locations) or locations[key] then
			data_s[#data_s+1] = value
			data_s[#data_s]['index'] = index
		end
	end
	-- Sort the new table
	table.sort(
		data_s,
		function(x, y)
			if x.name == 'Maa' then
				return true
			elseif y.name == 'Maa' then
				return false
			else
				return (x.index > y.index)
			end
		end
	)
	-- Get columns
	local columns = {
		cases = false,
		deaths = false,
		recoveries = false,
		total_vaccinated = false,
		vaccine_doses = false,
		fully_vaccinated = false,
		percent_vaccinated = false,
		deaths_per_million = false
	}
	local columns_index = {}
	if mw.getCurrentFrame().args['columns'] then
		for column in string.gmatch(
			mw.getCurrentFrame().args['columns'],
			'([^,]+)'
		) do
			columns[column] = true
			columns_index[#columns_index+1] = column
		end
	else
		columns = {
			cases = true,
			deaths = true,
			recoveries = false,
			total_vaccinated = true,
			vaccine_doses = true,
			fully_vaccinated = true,
			percent_vaccinated = false,
			deaths_per_million = false
		}
		columns_index = {
			'cases',
			'deaths',
			'recoveries',
			'total_vaccinated',
			'vaccine_doses',
			'fully_vaccinated',
			'percent_vaccinated',
			'deaths_per_million'
		}
	end
	-- Generate wikitext content
	for _, row in ipairs(data_s) do
		-- Test for empty rows
		local has_data = false
		for _, column in pairs(columns_index) do
			if columns[column] and row[column] then
				has_data = true
			end
		end
		if has_data then -- Only add row if it has data
			local name_base = row.name:match( "^(.*%S) *%(.+%)$" ) -- Saint Helena
			out = out .. '\n|-'
			if row.name == 'Maa' then
				out = out .. 'class="sorttop static-row-header"'
			end
			out = out .. '\n'
			-- Add the flag
			if not mw.getCurrentFrame().args['noflag'] then
				if row.name == 'Maa' then
					out = out ..
						'| [[File:OOjs UI icon globe.svg|16px|alt=|link=]]'
				elseif row.name == 'Caribbean Netherlands' then
					out = out ..
						'| [[Fail:Flag of the Netherlands.svg|23x15px]]'
				else
					flag_params = {name_base or row.name}
					-- So that it's not too large
					if row.name == 'Uus-Kaledoonia' then
						flag_params[2] = 'merged'
					end
					out = out .. '|' ..
						mw.getCurrentFrame():expandTemplate{
							title = 'Riigi ikoon',
							args = flag_params
						}
				end
			end
			-- Add country name
			if name_base then
				out = out .. '|| [[' .. row.name .. '|' .. name_base .. ']]'
			elseif row.name == 'Caribbean Netherlands' then
				out = out .. '|| [[Kariibi Holland]]'
			else
				out = out ..
					'|| [[' ..row.name .. ']]'
			end
			-- Add notes
			if row.note then
				out = out ..
					mw.getCurrentFrame():expandTemplate{
						title = 'Efn',
						args = {row.note}
					}
			end
			-- Fill out columns
			for _, column in ipairs(columns_index) do
				if columns[column] then
					if row[column] then
						out = out .. '||' .. mw.getContentLanguage():formatNum(row[column])
					else
						out = out ..
							'|| style="background: #cccccc;"' ..
							' data-sort-value=-1 | —'
					end
				end
			end
		end
	end
	out = out .. '\n|- class="sortbottom static-row-header"\n' ..
			'| colspan=8 style="width: 0; text-align: left !important;" |' ..
			mw.getCurrentFrame():expandTemplate{
				title = 'Märkused'
			}
	return out
end

function p.text()
	local location = mw.getCurrentFrame().args['location']
	local column = mw.getCurrentFrame().args['column']
	return data[location][column]
end

return p