Jump to content

Module:Sandbox/Phoenix7777/COVID-19 data

From Wikipedia, the free encyclopedia
local p = {}

local data_title = mw.title.new(
	'Template:COVID-19 data/data'
)
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 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
		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)
			return (x.index > y.index)
		end
	)
	-- Generate wikitext content
	for _, row in ipairs(data_s) do
		out = out .. '\n|-\n'
		-- Add the flag
		if row.name == 'World' then
			out = out ..
				'| [[File:Emojione 1F310.svg|15px|alt=|link=]]' ..
				'|| [[COVID-19 pandemic|World]]'
		else
			flag_params = {row.name}
			-- So that it's not too large
			if row.name == 'New Caledonia' then
				flag_params[2] = 'merged'
			end
			out = out .. '|' ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Flagicon',
					args = flag_params
				} ..
				'|| [[COVID-19 pandemic in ' .. row.name ..
				'|' .. row.name .. ']]'
		end
		-- Add notes
		if row.note then
			out = out ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {row.note}
				}
		end
		-- Add columns
		if not (
			row.cases or
			row.deaths or
			row.recoveries or
			row.total_vaccinated or
			row.vaccine_doses or
			row.fully_vaccinated
		) then
			out = out .. '|| style="background: #ababab;" colspan=6 ' ..
				'| \'\'No data\'\''
		else
			-- Get columns
			local columns = {
				cases = false,
				deaths = false,
				recoveries = false,
				total_vaccinated = false,
				vaccine_doses = false,
				fully_vaccinated = false
			}
			local columns_index = {
				'cases',
				'deaths',
				'recoveries',
				'total_vaccinated',
				'vaccine_doses',
				'fully_vaccinated'
			}
			if mw.getCurrentFrame().args['columns'] then
				columns_index = {}
				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 = true,
					total_vaccinated = true,
					vaccine_doses = true,
					fully_vaccinated = true
				}
			end
			-- Fill out columns
			for _, column in ipairs(columns_index) do
				if columns[column] then
					if row[column] then
						out = out .. '|| data-sort-value=' ..
							tostring(row[column]) ..
							'|' .. format_num(row[column])
					else
						out = out ..
							'|| style="background: #cccccc;"' ..
							' data-sort-value=0 | \'\'Unknown\'\''
					end
				end
			end
		end
	end
	out = out .. '\n|- class="sortbottom"\n| colspan=8 |' ..
			mw.getCurrentFrame():expandTemplate{
				title = 'Notelist'
			}
	return out
end

function p.pan()
	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 value.cases then index = value.cases
		elseif value.deaths then index = value.deaths
		else index = 0
		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)
			return (x.index > y.index)
		end
	)
	-- Generate wikitext content
	for _, row in ipairs(data_s) do
		out = out .. '\n|-\n'
		-- Add the flag
		if row.name == 'World' then
			out = out ..
				'| [[File:Emojione 1F310.svg|15px|alt=|link=]]' ..
				'||style="text-align:left;"| [[COVID-19 pandemic|World]]'
		else
			flag_params = {row.name}
			-- So that it's not too large
			if row.name == 'New Caledonia' then
				flag_params[2] = 'merged'
			end
			out = out .. '|' ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Flagicon',
					args = flag_params
				} ..
				'|| style="text-align:left;"| [[COVID-19 pandemic in ' .. row.name ..
				'|' .. row.name .. ']]'
		end
		-- Add notes
		if row.note then
			out = out ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {row.note}
				}
		end
		if row.name == 'Argentina' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Argentina"'}}
		end
		if row.name == 'Australia' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Australia"'}}
		end
		if row.name == 'Azerbaijan' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Azerbaijan"'}}
		end
		if row.name == 'Bahamas' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Bahamas"'}}
		end
		if row.name == 'Belgium' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Belgium"'}}
		end
		if row.name == 'Botswana' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Botswana"'}}
		end
		if row.name == 'Canada' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Canada"'}}
		end
		if row.name == 'Chile' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Chile"'}}
		end
		if row.name == 'China' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"China"'}}
		end
		if row.name == 'Congo' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Congo"'}}
		end
		if row.name == 'Cuba' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Cuba"'}}
		end
		if row.name == 'Cyprus' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Cyprus"'}}
		end
		if row.name == 'DR Congo' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"DR Congo"'}}
		end
		if row.name == 'Denmark' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Denmark"'}}
		end
		if row.name == 'Diamond Princess' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Diamond Princess"'}}
		end
		if row.name == 'Egypt' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Egypt"'}}
		end
		if row.name == 'Finland' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Finland"'}}
		end
		if row.name == 'France' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"France"'}}
		end
		if row.name == 'Georgia' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Georgia"'}}
		end
		if row.name == 'Germany' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Germany"'}}
		end
		if row.name == 'Guam' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Guam"'}}
		end
		if row.name == 'Isle of Man' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Isle of Man"'}}
		end
		if row.name == 'Israel' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Israel"'}}
		end
		if row.name == 'Japan' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Japan"'}}
		end
		if row.name == 'Moldova' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Moldova"'}}
		end
		if row.name == 'Morocco' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Morocco"'}}
		end
		if row.name == 'Netherlands' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Netherlands"'}}
		end
		if row.name == 'Northern Cyprus' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Northern Cyprus"'}}
		end
		if row.name == 'Norway' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Norway"'}}
		end
		if row.name == 'Russia' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Russia"'}}
		end
		if row.name == 'Sahrawi Arab DR' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Sahrawi Arab DR"'}}
		end
		if row.name == 'Serbia' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Serbia"'}}
		end
		if row.name == 'Somalia' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Somalia"'}}
		end
		if row.name == 'Somaliland' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Somaliland"'}}
		end
		if row.name == 'South Ossetia' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"South Ossetia"'}}
		end
		if row.name == 'Spain' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Spain"'}}
		end
		if row.name == 'Switzerland' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Switzerland"'}}
		end
		if row.name == 'Syria' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Syria"'}}
		end
		if row.name == 'Taiwan' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Taiwan"'}}
		end
		if row.name == 'Transnistria' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Transnistria"'}}
		end
		if row.name == 'Turkey' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Turkey"'}}
		end
		if row.name == 'Ukraine' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"Ukraine"'}}
		end
		if row.name == 'United Kingdom' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"United Kingdom"'}}
		end
		if row.name == 'United States' then
			out = out .. mw.getCurrentFrame():expandTemplate{title = 'Efn', args = {name='"United States"'}}
		end

		-- Add columns
		if not (
			row.cases or
			row.deaths
		) then
			out = out .. '|| style="background: #ababab;" colspan=6 ' ..
				'| \'\'No data\'\''
		else
			-- Get columns
			local columns = {
				cases = false,
				deaths = false
			}
			local columns_index = {
				'cases',
				'deaths'
			}
			if mw.getCurrentFrame().args['columns'] then
				columns_index = {}
				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
				}
			end
			-- Fill out columns
			for _, column in ipairs(columns_index) do
				if columns[column] then
					if row[column] then
						out = out .. '|| data-sort-value=' ..
							tostring(row[column]) ..
							'|' .. format_num(row[column])
					else
						out = out ..
							'|| style="background: #cccccc;"' ..
							' data-sort-value=0 | \'\'Unknown\'\''
					end
				end
			end
		end
	end
	out = out .. '\n|- class="sortbottom"\n'
	return out
end

function p.vac()
	local out = ''
	local data_s = {}
	local has_country_num_doses = false
	local has_country_num_fully = false
	-- Build new table
	for key, value in pairs(data) do
		if value.total_vaccinated then
			data_s[#data_s+1] = value
			data_s[#data_s]['index'] = value.total_vaccinated
		elseif value.vaccine_doses then
			data_s[#data_s+1] = value
			data_s[#data_s]['index'] = value.vaccine_doses
		elseif value.fully_vaccinated then
			data_s[#data_s+1] = value
			data_s[#data_s]['index'] = value.fully_vaccinated
		end
	end
	-- Sort the new table
	table.sort(
		data_s,
		function(x, y)
			return (x.index > y.index)
		end
	)
	-- Generate wikitext content
	for _, row in pairs(data_s) do
		-- Add the flag
		if row.name == 'World' then
			out = out ..
				'|-\n| [[File:Emojione 1F310.svg|15px|alt=|link=]]' ..
				'|| style="text-align:left;" | [[Deployment of COVID-19 vaccines|World]]'
		else
			flag_params = {row.name}
			-- So that it's not too large
			if row.name == 'New Caledonia' then
				flag_params[2] = 'merged'
			end
			out = out ..
				'|-\n|' ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Flagicon',
					args = flag_params
				} ..
				'||  style="text-align:left;" | [[COVID-19 vaccination in ' .. row.name ..
				'|' .. row.name .. ']]'
		end
		-- Add note
		if row.note_vaccination then
			out = out ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {row.note_vaccination}
				}
		end
		-- Add the number
		if row.total_vaccinated then
			out = out ..
				'||' .. format_num(row.total_vaccinated)
		elseif row.vaccine_doses then
			has_country_num_doses = true
			out = out ..
				'||' ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Font color',
					args = {'darkred', format_num(row.vaccine_doses)}
				} ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {name = 'country_num_doses'}
				}
		elseif row.fully_vaccinated then
			has_country_num_fully = true
			out = out ..
				'||' ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Font color',
					args = {'darkorange', format_num(row.fully_vaccinated)}
				} ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {name = 'country_num_fully'}
				}
		end
		-- Add the percentage
		if row.percent_vaccinated then
			out = out .. '||' .. string.format("%.1f", row.percent_vaccinated) .. '%'
		else
			out = out .. '|| style="background: #cccccc;" | '
		end
		if mw.getCurrentFrame().args[1] =='full' or mw.getCurrentFrame().args[1]=='total' then
			if row.fully_vaccinated then
				out = out ..
					'||' .. format_num(row.fully_vaccinated)
			else
				out = out .. '|| style="background: #cccccc;" | '
			end
			if row.percent_fully_vaccinated then
				out = out ..
					'||' .. string.format("%.1f", row.percent_fully_vaccinated) .. '%'
			else
				out = out .. '|| style="background: #cccccc;" | '
			end
		end
		if mw.getCurrentFrame().args[1] =='total' then
			if row.vaccine_doses then
				out = out .. '||' .. format_num(row.vaccine_doses)
			else
				out = out .. '|| style="background: #cccccc;" | '
			end
		end
		out = out .. '\n'
	end
	if has_country_num_doses or has_country_num_fully then
		notelist_refs = ''
		if has_country_num_doses then
			notelist_refs = notelist_refs ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {
						name = 'country_num_doses',
						'This country\'s data are the ' ..
							mw.getCurrentFrame():expandTemplate{
								title = 'Font color',
								args = {
									'darkred',
									'number of vaccine doses administered'
								}
							} ..
							', not the first dose only.'
					}
				}
		end
		if has_country_num_fully then
			notelist_refs = notelist_refs ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {
						name = 'country_num_fully',
						'This country\'s data are the ' ..
							mw.getCurrentFrame():expandTemplate{
								title = 'Font color',
								args = {
									'darkorange',
									'number of people fully vaccinated'
								}
							} ..
							', not the number of people ' ..
							'who have received at least one dose.'
					}
				}
		end
		if mw.getCurrentFrame().args[1] ~= 'full' and  mw.getCurrentFrame().args[1] ~= 'total' then
			out = out .. '|- class="sortbottom"\n| colspan=4 |'
		end
		if mw.getCurrentFrame().args[1] =='full' then
			out = out .. '|- class="sortbottom"\n| colspan=6 |'
		end
		if mw.getCurrentFrame().args[1] ==  'total' then
			out = out .. '|- class="sortbottom"\n| colspan=7 |'
		end
		out = out ..
			mw.getCurrentFrame():expandTemplate{
				title = 'Notelist',
				args = {refs = notelist_refs}
			}
	end
	return out
end

return p