Modulo:Fuso orario/sandbox

Versione del 6 mag 2016 alle 15:10 di Rotpunkt (discussione | contributi) (Nuova sandbox)
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
--[[
* Modulo che implementa il template Fuso orario.
]]--

require('Modulo:No globals')

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

-- Configurazione
local cfg = mw.loadData('Modulo:Fuso orario/Configurazione')

local p = {}

local function titleExists(name)
	local title = mw.title.new(name)
	return title and title.exists
end

local function compValues(val1, val2)
	val1 = val1:match('UTC(.*)%]%]'):gsub(':', '.')
	val2 = val2:match('UTC(.*)%]%]'):gsub(':', '.')
	return tonumber(val1) < tonumber(val2)
end

local function formatList(values)
	table.sort(values, compValues)
	table.insert(values, 1, '<ul><li>')
	table.insert(values, '</li></ul>')
	return mw.text.listToText(values, '</li><li>', '</li><li>')
end

local function formatTimezones(items)
	local formattedItems = {}
	for item, _ in pairs(items) do
		table.insert(formattedItems, string.format('[[%s]]', mw.wikibase.sitelink(item)))
	end
	return #formattedItems > 1 and formatList(formattedItems) or formattedItems[1]
end

-- per retrocompatibilità con l'input di Divisione amministrativa
local function formatUserInput(value)
	return titleExists('UTC' .. value) and string.format('[[UTC%s]]', value) or nil
end

local function checkTimezone(item)
	local ret
	
	ret = cfg.validTimezones[item] and item or nil
	
	-- se non è tra i validTimezones verifica la proprietà "considerato essere uguale a (P460)"
	if not ret then
		ret = mWikidata._getProperty( { 'P460', from = item, formatting = 'raw', n = 1 } )
		if ret then
			ret = cfg.alias[ret] or ret
			ret = cfg.validTimezones[ret] and ret or nil
		end
	end
	
	-- invece di P460 in alcuni casi è stato usato P421
	if not ret then
		ret = mWikidata._getProperty( { 'P421', from = item, formatting = 'raw', n = 1 } )
		if ret then
			ret = cfg.alias[ret] or ret
			ret = cfg.validTimezones[ret] and ret or nil
		end
	end

	return ret
end

local function getTimezones(args)
	local ret = {}
	local numTimezones = 0

	-- ricerca la proprietà fuso orario (P421) 
	local claims = mWikidata._getClaims('P421', { from = args.from, rank = 'best' } ) or {}
	-- se non presente la ricerca nell'elemento ottenuto da iso3166
	if #claims == 0 and args.iso3166 then
		claims = mWikidata._getClaims('P421', { from = cfg.iso3166[args.iso3166] } ) or {}
	end
	-- se non presente la ricerca nell'item in nazione (P17)
	if #claims == 0 then
		local item = mWikidata._getProperty( { 'P17', from = args.from, formatting = 'raw', n = 1 } )
		claims = mWikidata._getClaims('P421', { from = item, rank = 'best' } ) or {}
	end
	for _, claim in ipairs(claims) do
		local item = mWikidata._formatStatement(claim, { formatting = 'raw' })
		-- se ha "periodo di validità (P1264)" deve valere "tempo standard (Q1777301)"
		local period = mWikidata._formatQualifiers(claim, 'P1264', { formatting = 'raw' })
		if not period or period == 'Q1777301' then
			item = cfg.alias[item] or item
			item = checkTimezone(item)
			if item then
				-- per evitare duplicati
				numTimezones = ret[item] and numTimezones or numTimezones + 1
				ret[item] = true
			end
		end
	end

	return ret, numTimezones
end

-- Per l'utilizzo da altro modulo
function p._main(args)
	local ret
	if args[1] then
		ret = formatUserInput(args[1])
		if not ret and args.errmsg then
			local msg = mw.ustring.gsub(args.errmsg, '\\{', '{')
			msg = mw.ustring.gsub(msg, '\\}', '}')
			ret = mw.getCurrentFrame():preprocess(msg)
		end
	else
		local timezones, numTimezones = getTimezones(args)
		if args.unico and numTimezones > 1 then
			numTimezones = 0
		end
		if numTimezones > 0 then
			ret = formatTimezones(timezones)
		elseif args.cat and mw.title.getCurrentTitle().namespace == 0 then
			return string.format('[[Categoria:%s]]', args.cat)
		end
	end
	return ret
end

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

return p