stringtranslate.com

Module:COVID-19 data

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)local function format_num(number)if not number thenreturn ''endlocal _, _, minus, int, _ = tostring(number):find('([-]?)(%d+)([.]?%d*)')int = int:reverse():gsub("(%d%d%d)", "%1,")int = int:reverse():gsub("^,", "")int = minus .. intreturn intendfunction p.main()local out = ''local data_s = {}local has_notelist = falselocal count_row = 0local count_col = 0-- Build new tablelocal locations = {}if mw.getCurrentFrame().args['locations'] thenfor location in string.gmatch(mw.getCurrentFrame().args['locations'],'([^,]+)') dolocations[location] = trueendelselocations = nilendfor key, value in pairs(data) dolocal indexif mw.getCurrentFrame().args['sort'] thenif value[mw.getCurrentFrame().args['sort']] thenindex = value[mw.getCurrentFrame().args['sort']]elseindex = 0endelseif value.cases thenindex = value.caseselseif value.deaths thenindex = value.deathselseif value.vaccine_doses thenindex = value.vaccine_doseselseif value.total_vaccinated thenindex = value.total_vaccinatedelseif value.fully_vaccinated thenindex = value.fully_vaccinatedelse index = 0endendif (not locations) or locations[key] thendata_s[#data_s+1] = valuedata_s[#data_s]['_index'] = indexdata_s[#data_s]['_code'] = keyendend-- Sort the new tabletable.sort(data_s,function(x, y)if x._code == 'XW' thenreturn trueelseif y._code == 'XW' thenreturn falseelsereturn (x._index > y._index)endend)-- Get columnslocal columns = {cases = false,deaths = false,total_vaccinated = false,vaccine_doses = false,fully_vaccinated = false,percent_vaccinated = false,percent_fully_vaccinated = false,deaths_per_million = false,population = false}local columns_index = {}if mw.getCurrentFrame().args['columns'] thenfor column in string.gmatch(mw.getCurrentFrame().args['columns'],'([^,]+)') docolumns[column] = truecolumns_index[#columns_index+1] = columnendelsecolumns = {cases = true,deaths = true,total_vaccinated = true,vaccine_doses = true,fully_vaccinated = true,percent_vaccinated = false,percent_fully_vaccinated = false,deaths_per_million = false,population = false}columns_index = {'cases','deaths','total_vaccinated','vaccine_doses','fully_vaccinated','percent_vaccinated','percent_fully_vaccinated','deaths_per_million','population'}end-- Check if should show note_vaccinationlocal show_note_vaccination = falseif columns['total_vaccinated'] or columns['vaccine_doses'] or columns['fully_vaccinated'] or columns['percent_vaccinated'] or columns['percent_fully_vaccinated'] thenshow_note_vaccination = trueend-- Generate wikitext contentfor _, row in ipairs(data_s) do-- Test for empty rowslocal has_data = falsefor _, column in pairs(columns_index) doif columns[column] and row[column] thenhas_data = trueendendif has_data then -- Only add row if it has datacount_row = count_row + 1out = out .. '\n|-'if row._code == 'XW' thenout = out .. 'class="sorttop static-row-header"'end-- Add the flagif not mw.getCurrentFrame().args['noflag'] thenif count_row == 1 thencount_col = count_col + 1endout = out .. '\n| style="text-align: center;" data-sort-value="' .. row.name .. '" | 'if row._code == 'XW' thenout = out ..'[[File:OOjs UI icon globe.svg|16px|alt=|link=]]'elseflag_params = {row.name}-- So that it's not too largeif row.name == 'New Caledonia' thenflag_params[2] = 'merged'endout = out ..mw.getCurrentFrame():expandTemplate{title = 'Flagicon',args = flag_params}endend-- Add country nameif count_row == 1 thencount_col = count_col + 1endif mw.getCurrentFrame().args['noflag'] thenout = out .. '\n! scope="row" style="background-color: inherit;" data-sort-value="' .. row.name .. '" | 'elseout = out .. '\n! scope="row" style="background-color: inherit;" |'endif row._code == 'XW' thenout = out .. '[[COVID-19 pandemic|' .. row.name .. ']]'elseif row._code == 'GE' thenout = out ..'[[COVID-19 pandemic in Georgia (country)' ..'|' .. row.name .. ']]'elseif row._code == 'IM' thenout = out ..'[[COVID-19 pandemic in the ' .. row.name ..'|' .. row.name .. ']]'elseout = out ..'[[COVID-19 pandemic in ' .. row.name ..'|' .. row.name .. ']]'end-- Add notesif row.note thenhas_notelist = trueout = out ..mw.getCurrentFrame():expandTemplate{title = 'Efn',args = {row.note}}endif row.note_vaccination and show_note_vaccination thenhas_notelist = trueout = out ..mw.getCurrentFrame():expandTemplate{title = 'Efn',args = {'Vaccination note: ' .. row.note_vaccination}}end-- Fill out columnsfor _, column in ipairs(columns_index) doif columns[column] thenif count_row == 1 thencount_col = count_col + 1endif row[column] thenout = out .. '\n| data-sort-value=' ..tostring(row[column]) ..'|'if column:find('^percent') thenout = out .. string.format('%.2f', row[column]) .. '%'elseout = out .. format_num(row[column])endelseout = out .. '\n| data-sort-value=0 | —'endendendendendif has_notelist thenout = out .. '\n|- class="sortbottom static-row-header" style="text-align: left;"' ..'\n| colspan="' .. count_col .. '" style="width: 0;" |' ..mw.getCurrentFrame():expandTemplate{title = 'Notelist'}endreturn outendfunction p.vac()local out = ''local data_s = {}local has_country_num_doses = falselocal has_country_num_fully = falselocal count_row = 0local count_col = 0-- Build new tablefor key, value in pairs(data) doif value.total_vaccinated thendata_s[#data_s+1] = valuedata_s[#data_s]['index'] = value.total_vaccinatedelseif value.vaccine_doses thendata_s[#data_s+1] = valuedata_s[#data_s]['index'] = value.vaccine_doseselseif value.fully_vaccinated thendata_s[#data_s+1] = valuedata_s[#data_s]['index'] = value.fully_vaccinatedendend-- Sort the new tabletable.sort(data_s,function(x, y)return (x.index > y.index)end)-- Generate wikitext contentfor _, row in pairs(data_s) docount_row = count_row + 1out = out .. '\n|-'-- Add the flagif count_row == 1 thencount_col = count_col + 2endif row.name == 'World' thenout = out ..'class="sorttop static-row-header"' ..'\n| style="text-align: center;" data-sort-value="' .. row.name .. '" | ' ..'[[File:OOjs UI icon globe.svg|16px|alt=|link=]]' ..'\n! scope="row" style="background-color: inherit;" data-sort-value="' .. row.name .. '" | ' ..'[[Deployment of COVID-19 vaccines|World]]'elseflag_params = {row.name}-- So that it's not too largeif row.name == 'New Caledonia' thenflag_params[2] = 'merged'endout = out ..'\n| style="text-align: center;" data-sort-value="' .. row.name .. '" | ' ..mw.getCurrentFrame():expandTemplate{title = 'Flagicon',args = flag_params} ..'\n! scope="row" style="background-color: inherit;" data-sort-value="' .. row.name .. '" | ' ..'[[COVID-19 vaccination in ' .. row.name .. '|' .. row.name .. ']]'end-- Add noteif row.note_vaccination thenout = out ..mw.getCurrentFrame():expandTemplate{title = 'Efn',args = {row.note_vaccination}}end-- Add the numberif count_row == 1 thencount_col = count_col + 1endif row.total_vaccinated thenout = out .. '\n| data-sort-value=' ..tostring(row.total_vaccinated) ..'|' .. format_num(row.total_vaccinated)elseif row.vaccine_doses thenhas_country_num_doses = trueout = out .. '\n| data-sort-value=' ..tostring(row.vaccine_doses) ..'|' ..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 thenhas_country_num_fully = trueout = out .. '\n| data-sort-value=' ..tostring(row.fully_vaccinated) ..'|' ..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 percentageif count_row == 1 thencount_col = count_col + 1endif row.percent_vaccinated thenout = out .. '\n| data-sort-value=' ..tostring(row.percent_vaccinated) ..'|' .. string.format("%.1f", row.percent_vaccinated) .. '%'elseout = out .. '\n| data-sort-value=0 | —'endendif has_country_num_doses or has_country_num_fully thennotelist_refs = ''if has_country_num_doses thennotelist_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.'}}endif has_country_num_fully thennotelist_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.'}}endout = out .. '\n|- class="sortbottom static-row-header" style="text-align: left;"' ..'\n| colspan="' .. count_col .. '" style="width: 0;" |' ..mw.getCurrentFrame():expandTemplate{title = 'Notelist',args = {refs = notelist_refs}}endreturn outendfunction p.text()local location = mw.getCurrentFrame().args['location']local column = mw.getCurrentFrame().args['column']return data[location][column]endreturn p