Modulo:Tassobox/Sandbox

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
--[[  
Questo modulo è in appoggio al template Tassobox 
]]

local p = {}
local cfg = mw.loadData("Modulo:Tassobox/Configurazione")
local getArgs = require('Module:Arguments').getArgs
local mWikidata = require('Modulo:Wikidata')

local function check_value(valore)
	return valore and cfg.missing_table[mw.ustring.lower(valore)]
end

--===================================================================
-- Richiamo di un'immagine di file mancante appropriata al 
-- regno/phylum/classe/ordine/ecc della voce in cui è inserito
-- il tassobox
--===================================================================
function p.missing_image(frame)
	local args = getArgs(frame, {wrapper = 'Template:Tassobox'})
	local missing_link
	-- testa in ordine dal più specifico al più generico la classificazione di un essere vivente
	for _,test in ipairs(cfg.param_to_check) do
		missing_link = args[test] and check_value(args[test])
		if missing_link then break end
	end 
	-- se ha non trovato il valore nella tabella ricade nell'immagine di default
	missing_link = missing_link or "File:Unknown missing 1.png"
	return mw.ustring.format('[[%s|220px|Immagine di %s mancante]]', missing_link,  mw.title.getCurrentTitle().text)
end

----------------------------------------------------------------------------------
-- Ritorna la configurazione della tabella per le ere geologiche
----------------------------------------------------------------------------------
function p.ere_geologiche_table(frame)
-- Restituisce una tabella delle ere geologiche presenti in configurazione
 
	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th'):wikitext('Era'):done()
			:tag('th'):wikitext('Inizio'):done()
			:tag('th'):wikitext('Successiva'):done()
			:tag('th'):wikitext('Colore'):done()
	for name, values in pairs(cfg.ere_geologiche) do
		local start = values['start'] or "?"
		local next_era = values['next_era'] or "?"
		local color 
		if values['color'] then 
			color = mw.html.create('span')
				:cssText("display:inline-block;border:solid grey 1px;width:1em;height:1em")
				:css('background', values['color'])
				:wikitext(' ')
		else
			color = mw.html.create('span'):done()
		end
		root:tag('tr')
			:tag('td'):wikitext(name):done()
			:tag('td'):css('text-align', 'right'):wikitext(start):done()
			:tag('td'):wikitext(next_era):done()
			:tag('td'):css('text-align', 'center'):node(color)
	end
	return tostring(root)
end

--===================================================================
-- Genera lo stato di conservazione di uno specie
--===================================================================
local function _stato_conservazione(statocons, note_ref, data_est, statocons_versione, from_wikidata, debug_wikidata )
	if true then return statocons end
	local iucn =  { ['iucn2.3'] = true, ['iucn3.1'] = true}
	if debug_wikidata then return statocons end
	statocons = mw.ustring.lower(statocons)
	statocons = cfg.stato_alias[statocons] or statocons
	local row_stato = cfg.stato[statocons]
	if row_stato == nil then 
		local cat_error = ''
		if mw.title.getCurrentTitle().namespace == 0 then
			cat_error = '[[Categoria:Errori di compilazione del template Tassobox]]'
		end
		if from_wikidata then
			return '<strong class=\"error\">Parametro statocons (property P141) non valido su wikidata</div>' .. cat_error
		else
			return '<strong class=\"error\">Parametro statocons non valido</div>' .. cat_error 
		end
	end
	local immagine = ''
	if row_stato.image then
		if row_stato.versione then 
			local versione = statocons_versione or ''
			versione = (iucn[versione] and versione) or (statocons == 'ex' and 'none') or 'iucn3.1'
			if versione == 'none' then 
				immagine = 'Status_none_EX.svg'
			else
				immagine = mw.ustring.format(row_stato.image, versione)
			end
		else
			immagine = row_stato.image
		end
		immagine = '<div>\[\[Image:' .. immagine .. '|200px\]\]</div>'
	end
	local ref = (note_ref and  '<span style=font-size:80%>' .. note_ref .. '</span>') or '' 
	local dataestinzione = ''
	if row_stato.data then
		dataestinzione = (row_stato.pre_data and ' ' .. row_stato.pre_data) or dataestinzione
		dataestinzione = (data_est and mw.ustring.format(row_stato.data, data_est)) or dataestinzione
		dataestinzione = mw.ustring.format('<span style="color:%s;">%s</span>', row_stato.color_data or row_stato.color, dataestinzione)
	end
	local base_msg = 
'<div style="margin:0 auto; text-align:center;%s">'..
	'%s<div style="color:%s;font-weight:bold;">%s%s%s</div>' ..
'</div>'
	return mw.ustring.format(base_msg, ((immagine and 'background:white;') or ''), immagine, row_stato.color, row_stato.text, dataestinzione, ref)
end

--===================================================================
-- Genera lo stato di conservazione di uno specie
--===================================================================
function p.stato_conservazione(frame)

	local args = getArgs(frame, {wrapper = 'template:Tassobox'})
	local statocons, data_est, statocons_ref, note_ref, wikidata_test
	if args.statocons == nil then 
		local this_page = mw.title.getCurrentTitle();
		if this_page.namespace == 0 then 
			if wikidata_test then
				statocons = mWikidata._getProperty( { 'P141', from = wikidata_test} )
			else
				statocons = mWikidata._getProperty( { 'P141'} )
			end
			if statocons then
				if true then return frame:extensionTag{name = 'nowiki', content = statocons} end
				if wikidata_test then 
					data_est = mWikidata._getQualifier ({'P141', 'P8556', from = wikidata_test})
					statocons_ref =mWikidata._getProperty({'P627', from = wikidata_test})
				else
					data_est = mWikidata._getQualifier ({'P141', 'P8556'})
					statocons_ref = mWikidata._getProperty({'P627'})
				end
				if statocons_ref then
					note_ref = frame:expandTemplate{ title = 'IUCN', args = {summ = statocons_ref, cid='IUCN_from_wikidata'} } 
					note_ref = frame:extensionTag{ name='ref', content= note_ref, args={name='IUCN_from_wikidata'}}
				end
			end
		end
		if statocons then
			return _stato_conservazione(statocons, note_ref, data_est, 'iucn3.1', true, args.debug_wikidata)
		end
	else
		return _stato_conservazione(args.statocons, args.statocons_ref, args.dataestinzione, args.statocons_versione, false, false)
	end
end

return p