Bước tới nội dung

Mô đun:Dữ liệu đại dịch COVID-19

Bách khoa toàn thư mở Wikipedia

local vWikiData = mw.loadData('Mô đun:Dữ liệu đại dịch COVID-19/Định nghĩa');
local cName = vWikiData.countryName;
local p = {}

local data_title = mw.title.new(
	'Bản mẫu:Dữ liệu đại dịch COVID-19/dữ liệu'
)
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 = {}
	-- Khởi tạo bảng mới
	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
		value.en_name = value.name
		if (value.name and cName[key] ~= nil) then
			value.name = cName[key]
		end
		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
			data_s[#data_s]['_code'] = key
		end
	end
	-- Sắp xếp bảng
	table.sort(
		data_s,
		function(x, y)
			if x._code == 'XW' then
				return true
			elseif y._code == 'XW' then
				return false
			else
				return (x._index > y._index)
			end
		end
	)
	-- Khởi tạo cột
	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
	-- Tạo nội dung wikitext
	for _, row in ipairs(data_s) do
		-- Kiểm tra các hàng rỗng
		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 -- Chỉ thêm hàng nếu có dữ liệu
			out = out .. '\n|-'
			if row._code == 'XW' then
				out = out .. 'class="sorttop static-row-header"'
			end
			out = out .. '\n'
			-- Thêm cờ
			if not mw.getCurrentFrame().args['noflag'] then
				if row._code == 'XW' then
					out = out ..
						'| [[Tập tin:OOjs UI icon globe.svg|16px|alt=|link=]]'
				else
					flag_params = {row.en_name}
					-- Làm cho nó không quá lớn
					if row.name == 'New Caledonia' then
						flag_params[2] = 'merged'
					end
					out = out .. '|' ..
						mw.getCurrentFrame():expandTemplate{
							title = 'Flagicon',
							args = flag_params
						}
				end
			end
			-- Thêm tên
			if row._code == 'XW' then
				out = out .. '|| [[Đại dịch COVID-19|' .. row.name .. ']]'
			elseif row._code == 'CN' then
				out = out ..
					'|| [[Đại dịch COVID-19 tại Trung Quốc đại lục' ..
					'|' .. row.name .. ']]'
			else
				out = out ..
					'|| [[Đại dịch COVID-19 tại ' .. row.name ..
					'|' .. row.name .. ']]'
			end
			-- Add notes
			if row.note then
				out = out ..
					mw.getCurrentFrame():expandTemplate{
						title = 'Efn',
						args = {row.note}
					}
			end
			-- Nạp các cột
			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 | —'
					end
				end
			end
		end
	end
	out = out .. '\n|- class="sortbottom static-row-header" ' ..
			'style="text-align: left;"\n' ..
			'| colspan=8 style="width: 0;" |' ..
			mw.getCurrentFrame():expandTemplate{
				title = 'Danh sách ghi chú'
			}
	return out
end

function p.vac()
	local out = ''
	local data_s = {}
	local has_country_num_doses = false
	local has_country_num_fully = false
	-- Khởi tạo bảng mới
	for key, value in pairs(data) do
		value.en_name = value.name
		if (value.name and cName[key] ~= nil) then
			value.name = cName[key]
		end
		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
	-- Sắp xếp bảng mới
	table.sort(
		data_s,
		function(x, y)
			return (x.index > y.index)
		end
	)
	-- Tạo nội dung wikitext
	for _, row in pairs(data_s) do
		-- Add the flag
		if row.en_name == 'World' then
			out = out ..
				'|-\n| [[File:OOjs UI icon globe.svg|16px|alt=|link=]]' ..
				'|| style="text-align:left;" | [[Vắc-xin COVID-19|Thế giới]]'
		else
			flag_params = {row.en_name}
			-- Làm nó không quá lớn
			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;" | ' .. '[[' .. (mw.title.new('Tiêm chủng ngừa COVID-19 tại ' .. row.name).exists and ('Tiêm chủng ngừa COVID-19 tại ' .. row.name) or 'Vắc-xin COVID-19') ..
				'|' .. row.name .. ']]'
		end
		-- Thêm ghi chú
		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 = 'Màu chữ',
					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 = 'Màu chữ',
					args = {'darkorange', format_num(row.fully_vaccinated)}
				} ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {name = 'country_num_fully'}
				}
		end
		-- Thêm phần trăm
		if row.percent_vaccinated then
			out = out .. '||' .. string.format("%.1f", row.percent_vaccinated):gsub("%.", ",") .. '%'
		else
			out = out .. '|| style="background: #cccccc;" |'
		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',
						'Dữ liệu của quốc gia này là ' ..
							mw.getCurrentFrame():expandTemplate{
								title = 'Màu chữ',
								args = {
									'darkred',
									'tổng số liều vắc xin đã tiêm'
								}
							} ..
							', không chỉ là liều đầu tiên.'
					}
				}
		end
		if has_country_num_fully then
			notelist_refs = notelist_refs ..
				mw.getCurrentFrame():expandTemplate{
					title = 'Efn',
					args = {
						name = 'country_num_fully',
						'Dữ liệu của quốc gia này là ' ..
							mw.getCurrentFrame():expandTemplate{
								title = 'Màu chữ',
								args = {
									'darkorange',
									'số người được tiêm chủng đầy đủ'
								}
							} ..
							', không phải là số người chỉ mới tiêm 1 lần.'
					}
				}
		end
		out = out .. '|- class="sortbottom"\n| colspan=4 |' ..
			mw.getCurrentFrame():expandTemplate{
				title = 'Danh sách ghi chú',
				args = {refs = notelist_refs}
			}
	end
	return out
end

function p.text()
	local location = mw.getCurrentFrame().args['location']
	local column = mw.getCurrentFrame().args['column']
	return data[location][column] or 'Không xác định'
end

return p