وحدة:CoronaTable

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث
local Localization = require('Module:CoronaTable/local')
local headers = Localization.headers
local i18nTeritory = Localization.i18nTeritory -- optional. set to nil if not translated, adn use lua log to get the generated translation
local i18nSum = Localization.sum or '' -- optional
local editLabel = Localization.editLabel or 'Edit' -- optional

function translateTeritory(enName, teritoriesWikidata)
	if i18nTeritory~=nil and i18nTeritory[enName] then
		return i18nTeritory[enName]
	end

	-- fallback to wikidata
	if teritoriesWikidata[enName]~=nil then
			local label = teritoriesWikidata[enName]
			mw.logObject(mw.ustring.format('[\'%s\']=\'%s\',', enName, label))
			return label
	end
	local entityId = mw.wikibase.getEntityIdForTitle( enName, 'arwiki' )
	if entityId then
		local label =  mw.wikibase.getLabel( entityId )
		mw.logObject(mw.ustring.format('[\'%s\']=\'%s\',', enName, label))
		return label
	end
	-- otherwise - use the source
	return enName
end

function showTable(frame)
	local data = mw.ext.data.get("2019–20 coronavirus outbreak.tab")
	local CoronavirusOutbreak1920WikidataId = 'Q81068910'
	local teritoriesWikidata = mw.wikibase.getBestStatements( CoronavirusOutbreak1920WikidataId, 'P17' )
	local teritoriesWikidataDict = {}
	if i18nTeritory == nil then
		mw.logObject('Generating teritory translation from wikidata! please consider to set i18nTeritory by copy the Lua log to Module:CoronaTable/local')
		for k,v in pairs(teritoriesWikidata) do
			local enLabel = mw.wikibase.getLabelByLang(v.mainsnak.datavalue.value.id ,'ar')
			local label = mw.wikibase.getLabel(v.mainsnak.datavalue.value.id)
			if v.qualifiers and v.qualifiers['P805'] then
				local relatedArticleId = v.qualifiers['P805'][1].datavalue.value.id
				local relatedArticle = mw.wikibase.getSitelink( relatedArticleId )
				if relatedArticle then
					label = mw.ustring.format('[[%s|%s]]', relatedArticle, label)
				end
			end
			teritoriesWikidataDict[enLabel] = label
		end
	end
	
	local lang = mw.language.getContentLanguage()
	local stylesheet = frame:extensionTag{ name = 'templatestyles', args = { src = 'Template:CoronaTable/style.css' } }
	local root = mw.html.create('table'):attr('id','coronavirusTable'):addClass('wikitable plainrowheaders sortable')
	root:tag('caption'):wikitext(Localization.caption)
	root:tag('tr')
		:tag('th')
			:wikitext(headers['Country'])
			:done()
		:tag('th')
			:wikitext(headers['Cases'])
			:done()
		:tag('th')
			:wikitext(headers['Deaths'])
			:done()			
		:tag('th')
			:wikitext(headers['Recoveries'])
			:done()
	local sums = {0, 0, 0}
	for k,v in pairs(data['data']) do
		sums[1] = sums[1] + v[2]
		sums[2] = sums[2] + v[3]
		sums[3] = sums[3] + v[4]
		local teritory = frame:preprocess(translateTeritory(v[1], teritoriesWikidataDict))
		local tdCases = mw.html.create('td'):wikitext(lang:formatNum(v[2]))
		if v[2] == 0 then tdCases:cssText('color:gray;') end
		local tdDeaths = mw.html.create('td'):wikitext(lang:formatNum(v[3]))
		if v[3] == 0 then tdDeaths:cssText('color:gray;') end
		local tdRecoveries = mw.html.create('td'):wikitext(lang:formatNum(v[4]))
		if v[4] == 0 then tdRecoveries:cssText('color:gray;') end
		root:tag('tr')
			:tag('th')
				:attr('scope', 'row')
				:wikitext(teritory)
				:done()
				:node(tdCases)
				:node(tdDeaths)
				:node(tdRecoveries)
				:done()
	end
	
	root:tag('tr')
			:tag('th')
				:wikitext(i18nSum)
				:done()
			:tag('th')
				:wikitext(lang:formatNum(sums[1]))
				:done()
			:tag('th')
				:wikitext(lang:formatNum(sums[2]))
				:done()			
			:tag('th')
				:wikitext(lang:formatNum(sums[3]))
				:done()
	
	root:tag('tr')
		:tag('td')
		:attr('colspan', '4')
		:wikitext(mw.ustring.format('[[c:Data:2019–20 coronavirus outbreak.tab|%s]]', editLabel))
	return stylesheet..tostring(root)
end
return {
	showTable=showTable
}