Modulo:Codice statistico

Versione del 10 mag 2016 alle 11:43 di Rotpunkt (discussione | contributi) (+proprietà)

Modulo che implementa il template {{Codice statistico}}.


--[[
* Modulo che implementa il template Codice statistico.
]]--

require('Modulo:No globals')

local getArgs = require('Modulo:Arguments').getArgs
local mWikidata = require('Modulo:Wikidata')

local p = {}

-- Funzione di formattazione del codice municipale austriaco (Statistik Austria)
local function formatP964(id)
	local formatted_id = string.gsub(id, "(%d)(%d%d)(%d%d)", "%1 %2 %3")
	return string.format('[http://www.statistik.at/blickgem/gemDetail.do?gemnr=%s %s]', id, formatted_id)
end

-- Funzione di formattazione del codice municipale tedesco (Statistisches Bundesamt)
local function formatP439(id)
	local formatted_id = string.gsub(id, "(%d%d)(%d)(%d%d)(%d%d%d)", "%1 %2 %3 %4")
	return string.format('[http://www.statistik-portal.de/Statistik-Portal/gemeindeverz.asp?G=%s %s]', id, formatted_id)
end

-- Funzione di formattazione del codice dei distretti tedesco (Statistisches Bundesamt)
local function formatP440(id)
	return string.gsub(id, "(%d%d)(%d)(%d%d)", "%1 %2 %3")
end

-- Funzione di formattazione del codice INSEE (Institut national de la statistique et des études économiques)
local function formatP374(id)
	return string.format('[http://www.insee.fr/fr/themes/dossier_complet.asp?codgeo=COM-%s %s]', id, id)
end

-- Codici statistici configurati
local statCodes = {
	AUT = {
		default = { prop = 'P964', catprefix = 'Codice municipale austriaco', format = formatP964 }
	},
	CHE = {
		default = { prop = 'P771', catprefix = 'Codice OFS' }
	},
	DEU = {
		["3"] = { prop = 'P440', catprefix = 'Codice dei distretti tedesco', format = formatP440 },
		default = { prop = 'P439', catprefix = 'Codice municipale tedesco', format = formatP439 }
	},
	ESP = {
		default = { prop = 'P772', catprefix = 'Codice INE' }
	},
	FRA = {
		["1"] = { prop = 'P2585', catprefix = 'Codice regione INSEE' },
		["2"] = { prop = 'P2586', catprefix = 'Codice dipartimento INSEE' },
		["4"] = { prop = 'P2506', catprefix = 'Codice cantone INSEE' },
		default = { prop = 'P374', catprefix = 'Codice INSEE', format = formatP374 }
	},
	ITA = {
		default = { prop = 'P635', catprefix = 'Codice ISTAT' }
	}
}

local function getWikidataCategory(userval, wdval, code)
	local cat
	if userval then
		if not wdval then
			cat = string.format('%s assente su Wikidata', code.catprefix)
		elseif string.gsub(wdval, ' ', '') == string.gsub(userval, ' ', '') then
			cat = code.catuguali and string.format('%s uguale a Wikidata', code.catprefix)
		else
			cat = string.format('%s differente da Wikidata', code.catprefix)
		end
	elseif wdval then
		cat = string.format('%s letto da Wikidata', code.catprefix)
	end
	return cat and string.format('[[Categoria:%s]]', cat) or ''
end

-- Restituisce una tabella HTML con i codici statistici configurati
function p.report()
	local tableNode
	local sortedCodes = {}

	for key, _ in pairs(statCodes) do
		table.insert(sortedCodes, key)
	end
	table.sort(sortedCodes)

	tableNode = mw.html.create('table')
	tableNode
		:addClass('wikitable')
		:addClass('sortable')
		:tag('tr')
			:tag('th')
				:wikitext('Codice')
				:done()
			:tag('th')
				:wikitext('Paese')
				:done()
			:tag('th')
				:wikitext('Grado amministrativo')
				:done()
			:tag('th')
				:wikitext('Proprietà Wikidata')
				:done()

	for _, iso3166 in ipairs(sortedCodes) do
		local codes = statCodes[iso3166]
		for key, value in pairs(codes) do
			tableNode
				:tag('tr')
					:tag('td')
						:wikitext(value.catprefix)
						:done()
					:tag('td')
						:wikitext(iso3166)
						:done()
					:tag('td')
						:wikitext(key == 'default' and 'predefinito' or key)
						:done()
					:tag('td')
						:wikitext(string.format('[[d:p:%s|%s (%s)]]', value.prop, mWikidata._getLabel( { value.prop } ), value.prop))
						:done()
		end
	end

	return tostring(tableNode)
end

-- Per l'utilizzo da altro modulo
function p._main(args)
	local code, userval, wdval, formatted_userval, formatted_wdval
	local cat = ''

	-- valore utente
	userval = args[1]

	-- ricerca paese
	if args.iso3166 then
		code = statCodes[args.iso3166]
	end
	if not code then
		local iso3166 = mWikidata._getProperty({ 'P17', showprop = 'P298', from = args.from, n = 1 })
		code = statCodes[iso3166]
	end
	-- ricerca grado amministrativo
	if code then
		code = code[args.grado] or code.default
	end

	if code then
		-- valore letto da Wikidata	
		wdval = mWikidata._getProperty({ code.prop, from = args.from, n = 1 })
		formatted_wdval = (wdval and code.format) and code.format(wdval) or wdval
		formatted_userval = (userval and code.format) and code.format(string.gsub(userval, ' ', '')) or userval

		-- categoria di servizio
		if mw.title.getCurrentTitle().namespace == 0 and (userval or wdval) then
			cat = getWikidataCategory(userval, wdval, code)
		end
	end

	return (formatted_userval or formatted_wdval or '') .. cat
end

-- Entry-point per il template {{Codice statistico}}
function p.main(frame)
	return p._main(getArgs(frame, {parentOnly = true}))
end

return p