Documentation[voir] [modifier] [historique] [purger]

Cette page définit un module d'infobox.


--[[
  Ceci est un test, qui sera supprimé.
--]]

local localdata = require "Module:Infobox/Localdata"
local italiques = require "Module:Italiques biologiques"
local data = require "Module:Taxobox données"


local syntaxe = {
	['classification'] = {
		['obligatoire'] = {},
		['optionnel'] = {'classification'},
		['booléen'] = {},
	},
	['rang'] = {
		['obligatoire'] = {'rang', 'taxon'},
		['optionnel'] = {'affiche'},
		['booléen'] = {'éteint', 'ancien'},
	},
	['iucn'] = {
		['obligatoire'] = {'niveau'},
		['optionnel'] = {'critère', 'commentaire'},
		['booléen'] = {},
	},
	['cites'] = {
		['obligatoire'] = {'annexe'},
		['optionnel'] = {'révision', 'commentaire'},
		['booléen'] = {},
	},
}

-- debug
local function build()
	local out = ""
	for _, v in pairs(localdata) do
		out = out .. _ .. " " .. v .. "<br/>"
	end
	return out
end

local function mytrim(val)
	if (val == nil) then
		return ""
	end
	return mw.text.trim(val)
end

local function erreur(titre, texte, cat)
	return {
		type = 'bloc',
		title = titre,
		titlestyle = {['background-color']='#FFE4C4'},
		text = "<span class='error'>" .. (texte or "Erreur non précisée") .. "</span>",
		-- cat = cat,
	}
end

-- lecture d'une "ligne" de données
local function lire_ligne(pos)
	out = {}
	-- pas de donnée à cette position
	if (localdata[pos] == nil) then
		return "Erreur interne (1)", pos+1, false, "Erreur interne"
	end
	local syn = mw.ustring.lower(mw.text.trim(localdata[pos]))
	-- pas de mot clé à cette position
	if (syntaxe[syn] == nil) then
		return "Erreur interne (2)", pos+1, false, "Erreur interne"
	end
	local def = syntaxe[syn]
	pos = pos + 1  -- position suivante
	local pobl = 1
	local pfac = 1
	while (true) do
		-- si plus de données ou mot clé on sort de la boucle
		if (localdata[pos] == nil) then
			break
		end
		local mot = mw.text.trim(localdata[pos])
		if (syntaxe[mw.ustring.lower(mot)]) then
			break
		end
		-- on traite d'abord les booléens
		local done = false
		for _, v in pairs(def['booléen'] or {}) do
			if (mot == v) then
				out[v] = true
				done = true
				break
			end
		end
		-- si ce n'était pas un booléen
		if (not done) then
			-- on test les obligatoires
			if (def['obligatoire'][pobl]) then
				out[def['obligatoire'][pobl]] = mot
				pobl = pobl + 1
			elseif (def['optionnel'][pfac]) then
				out[def['optionnel'][pfac]] = mot
				pfac = pfac + 1
			else
				-- trop de paramètres
				return "Trop de paramètres pour " .. syn, pos+1, false, "Trop de paramètres"
			end
		end
		pos = pos + 1
	end
	-- on vérifie que les paramètres obligatoires sont présents
	if (#def['obligatoire'] ~= pobl-1) then
		return "Trop peu de paramètres pour " .. syn, pos+1, false, "Trop peu de paramètres"
	end
	return out, pos, true, "" -- on retourne les données + ok
end

local function build_rang(ligne)
	out = nil
	
	-- le rang, wikifié
	local rg
	if (data.rangs[ligne['rang']]) then
		rg = data.rangs[ligne['rang']]['wikif']
	else
		rg = data.rangs['inconnu']['wikif']
	end
	-- le taxon
	local tx = ""
	if (ligne['éteint']) then
		tx = tx .. "<abbr class='abbr' title='éteint'>†</abbr>[[Catégorie:Taxobox taxon utilisant le paramètre éteint]]&nbsp;"
	end
	if (ligne['affiche']) then
		tx = tx .. italiques.italique_biologique("[[" .. ligne['taxon'] .. "|" .. ligne['affiche'] .. "]]", ligne['rang'], localdata["charte"])
	else
		tx = tx .. italiques.italique_biologique("[[" .. ligne['taxon'] .. "]]", ligne['rang'], localdata["charte"])
	end
	-- ancien
	if (ligne['ancien']) then
		rg = "<s>" .. rg .. "</s>"
		tx = "<s>" .. tx .. "</s>"
	end
	-- on stocke
	localdata[ligne['i'] .. "-v-classif-" .. ligne['num']] = tx
	return {
		type = 'row',
		label = rg,
		value = ligne['i'] .. "-v-classif-" .. ligne['num'],
	}
end

local function build_classification2(i)
	local cur = nil
	local titre = 'Classification'
	-- on récupère les propriétés
	local ligne, pos, etat, cat = lire_ligne(i)
	if (not etat) then
		return erreur(titre, ligne, cat) -- erreur de lecture
	end

	-- titre selon l'option
	if (ligne['classification']) then
		if (not data.classifications[mw.ustring.lower(ligne['classification'])]) then
			return erreur(titre, "Classification inconnue (" .. ligne['classification'] .. ")", "")
		else
			titre = data.classifications[mw.ustring.lower(ligne['classification'])]['texte']
		end
	else
		titre = '[[Classification scientifique des espèces|Classification]]'
	end
	
	-- on lit les éléments de classification
	local parts = {}
	-- si règne auto on l'ajoute
	if (data.regnes[localdata['charte']]['règne auto'] ~= nil) then
		local rg = data.regnes[localdata['charte']]['règne auto']
		local tmp
		if (data.rangs[rg[1][1]]) then
			tmp = data.rangs[rg[1][1]]['wikif']
		else
			tmp = data.rangs['inconnu']['wikif']
		end
		localdata[i .. "-v-classif-R"] =
		    	italiques.italique_biologique("[[" .. rg[1][2] .. "]]", rg[1][1], localdata["charte"])
		table.insert(parts, {type = 'row', label = tmp, value = i .. "-v-classif-R" })
	end
	-- boucle sur les rangs
	while (mw.ustring.lower(localdata[pos]) == 'rang') do
		ligne, pos, etat, cat = lire_ligne(pos)
		if (not etat) then
			return erreur(titre, ligne, cat) -- erreur de lecture
		end
		ligne['i'] = i
		ligne['num'] = pos
		local tmp = build_rang(ligne)
		table.insert(parts, tmp)
		-- si plus de données on sort (à cause du 'lower' dans le wile)
		if (not localdata[pos]) then
			break
		end
	end
	cur = {
		type = 'table',
		title = titre,
		titlestyle = {['background-color']='#FFE4C4'},
		rowsize = "40%",
		rows = parts,
	}
	return cur
end

local function build_classification()
	local titre = 'Classification'
	local out = nil
	local cur = nil
	for i, _v in pairs(localdata) do
		if ((type(i) == "number") and (type(_v) == "string")) then
			v = mw.ustring.lower(mw.text.trim(_v))
			if (v == "classification") then
				-- analyse d'une classification
				cur = build_classification2(i)
				-- on l'ajoute
				if (cur ~= nil) then
					if (out == nil) then
						out = { cur }
					else
						table.insert(out, cur)
					end
				end
			end
		end
	end
	return out
end


-- retourne une table de classification à partir des données et d'une position de départ
local function sub_classification(pos)
	local out = {}
	local i = pos
	local dg = "+ "
	
	-- on cherche le premier 'rang'
	while(true) do
		if ((localdata[i] == nil) or (mw.text.trim(localdata[i]) == 'fin classification')) then
			return nil, dg -- pas trouvé !
		end
		if (mw.text.trim(localdata[i]) == 'rang') then
			break -- trouvé
		end
		i = i + 1
	end
	while(true) do
		dg = dg .. " ." .. i
		if ((localdata[i] == nil) or (mw.text.trim(localdata[i]) == 'fin classification')) then
			dg = dg .. " end"
			break
		end
		if (mw.text.trim(localdata[i]) == 'rang') then
			local res = {}
			res['rang'] = mytrim(localdata[i+1]) or "inconnu"
			res['taxon'] = mytrim(localdata[i+2]) or "erreur"
			dg = dg .. " add " .. res['rang'] .. "+" .. res['taxon']
			local buf = mytrim(localdata[i+3])
			if ((buf ~= "") and (buf ~= 'rang') and (buf ~= 'fin classification')) then
				res['affiche'] = buf
				i = i + 1
			dg = dg .. " add r+t+a"
			end
			i = i + 3
			dg = dg .. " insert"
			table.insert(out, res)
		else
			dg = dg .. " !rang (" .. localdata[i] .. ")"
			break
		end
	end
	if (next(out) == nil) then
		return nil, dg
	end
	return out, dg
end

-- parcours les données pour trouver la classification
local function classification(num)
	local out = nil
	local classif = nil
	local tmp = nil
	local dg = "x"
	local ddg = ">> "
	local cur = 0
	num = num or 0 -- num → passe les 'num' premières trouvées
	for i, _v in pairs(localdata) do
		if (type(i) == "number") then
			dg = dg .. " " .. i .. "/" .. _v
			v = mw.text.trim(_v)
			if (v == 'classification') then
				dg = dg .. " =classif"
				if (cur >= num) then
					dg = dg .. " =sub_classif"
					tmp, ddg = sub_classification(i+1)
					cur = cur + 1
				end
				break
			end
		end
	end
	-- mise en forme des données
	if (tmp == nil) then
		return {type='table',titlestyle={['background-color']='#FFE4C4'},
			title='NIL',rows={{type='row1col',value=dg .. "<br/>" .. ddg}}}
	end
	out = {
		type = 'table',
		title = 'Classification',
		titlestyle = {['background-color']='#FFE4C4'},
		rowsize = "40%",
	}
	local cnt = 0
	rows = {}
	-- si besoin : ajout du "règne"
	if (data.regnes[localdata['charte']]['règne auto'] ~= nil) then
		local rg = data.regnes[localdata['charte']]['règne auto']
		local tmp
		if (data.rangs[rg[1][1]]) then
			tmp = data.rangs[rg[1][1]]['wikif']
		else
			tmp = data.rangs['inconnu']['wikif']
		end
		localdata[num .. "-v-classif-R"] =
		    	italiques.italique_biologique("[[" .. rg[1][2] .. "]]", rg[1][1], localdata["charte"])
		table.insert(rows, {type = 'row', label = tmp, value = num .. "-v-classif-R" })
	end
	for i, v in ipairs(tmp) do
		local tmp
		if (data.rangs[v['rang']]) then
			tmp = data.rangs[v['rang']]['wikif']
		else
			tmp = data.rangs['inconnu']['wikif']
		end
		local x = { type = 'row', label = tmp, value = num .. "-v-classif-" .. cnt }
		dg = dg .. " SET(" .. v['rang'] .. ") " .. num .. "-v-classif-" .. cnt .. "=" .. v['taxon']
		if (v['affiche'] == nil) then
		    localdata[num .. "-v-classif-" .. cnt] =
		    	italiques.italique_biologique("[[" .. v['taxon'] .. "]]", v['rang'], localdata["charte"])
			table.insert(rows, x)
		else
		    localdata[num .. "-v-classif-" .. cnt] =
		    	italiques.italique_biologique("[[" .. v['taxon'] .. "|" .. v['affiche'] .. "]]", v['rang'], localdata["charte"])
			table.insert(rows, x)
		end
		cnt = cnt + 1
	end
	-- table.insert(rows, {type='row',label=cnt,value='taxon'})
	-- table.insert(rows, {type='row1col',value=dg .. "<br/>" .. ddg})
	out['rows']	= rows
	return out
end


local function synonymes()
	local out = nil
	
	if (localdata['synonymes']) then
		out = {
			type = 'bloc',
			title = '[[Synonyme (taxinomie)|Synonymes]]',
			titlestyle = {['background-color']='#FFE4C4'},
			text = "\n" .. localdata['synonymes'],
		}
	end
	return out
end

local function build_cites(ligne)
	local tmp
	local titre = 'Statut [[Convention sur le commerce international des espèces de faune et de flore sauvages menacées d\'extinction|CITES]]'
	local cat = "[[Catégorie:CITES annexe " .. ligne['annexe'] .. "]]"
	if ((ligne['annexe'] ~= "I") and (ligne['annexe'] ~= "II") and (ligne['annexe'] ~= "III")) then
		return erreur(titre, "Annexe CITES invalide " .. (ligne['annexe'] or "??") .. ")", "Annexe CITES invalide")
	end
	-- image
	tmp = "<small>[[Image:Cites " .. ligne["annexe"] .. ".svg|link=|alt=Sur l'annexe " .. ligne["annexe"] .. " de la CITES|30px]]"
	tmp = tmp .. "[[Annexe " .. ligne["annexe"] .. " de la CITES|Annexe " .. ligne["annexe"] .. "]], "
	if (ligne["révision"]) then
		tmp = tmp .. "Rév. du " .. ligne["révision"]
	else
		tmp = tmp .. "Date de révision inconnue"
	end
	if (ligne["commentaire"]) then
		tmp = tmp .. "<br/>" .. ligne["commentaire"] .. "</small>"
	else
		tmp = tmp .. "</small>"
	end
	tmp = tmp .. cat -- comment ajouter une catégorie à retourner ?
	return {
		type = 'bloc',
		title = titre,
		titlestyle = {['background-color']='#FFE4C4'},
		style = {['text-align']='center'},
		text = tmp,
	}
end

local function cites()
	local out = nil
	
	i = 1
	while (true) do
		local id = i
		if (id == 1) then
			id = ""
		end
		if (not localdata['cites' .. id]) then
			break  -- pas / plus d'entrées
		end
		local ligne = {}
		ligne['annexe'] = localdata['cites' .. id]
		ligne['révision'] = localdata['cites' .. id .. '-révision']
		ligne['commentaire'] = localdata['cites' .. id .. '-commentaire']
		cur = build_cites(ligne)
		-- on l'ajoute
		if (cur ~= nil) then
			if (out == nil) then
				out = { cur }
			else
				table.insert(out, cur)
			end
		end
		i = i + 1
	end
	return out
end

local function build_iucn(ligne)
	local tmp = ""
	local titre = '[[Statut de conservation]] [[Union internationale pour la conservation de la nature|UICN]]'
	-- validation code
	if (data.uicn[ligne['niveau']] == nil) then
		return erreur(titre, "Niveau IUCN invalide " .. (ligne['niveau'] or "??") .. ")", "Niveau IUCN invalide")
	end
	local niv = ligne['niveau']
	local ntxt = data.uicn[niv]["texte"]
	local cat = "[[Catégorie:Statut UICN " .. ntxt .. "]]"
	local rev = "3.1"
	if (niv == "CD") then
		rev = "2.3"
	end
	-- image
	if (niv ~= "DD") then
		local cible = niv
		if (niv == "jamais") then
			cible = "blank"
		end
		tmp = "[[Image:Status iucn" .. rev .. " " .. cible .. ".svg|alt=" .. niv .. "|link=|244px]]<br/>"
	end
	tmp = tmp .. "'''" .. niv .. "'''"
	if (ligne['critère']) then
		tmp = tmp .. " " .. ligne['critère']
	end
	tmp = tmp .. "&nbsp;: "
	if (niv == "CR") then
		tmp = tmp .. "<br/>"
	end
	tmp = tmp .. "'''" .. ntxt .. "'''"
	if (ligne['commentaire']) then
		tmp = tmp .. "<br/>''" .. ligne['commentaire'] .. "''"
	end
	tmp = tmp .. cat -- comment ajouter une catégorie à retourner ?
	return {
		type = 'bloc',
		title = titre,
		titlestyle = {['background-color']='#FFE4C4'},
		style = {['text-align']='center'},
		text = tmp,
	}
end

function iucn()
	local out = nil
	
	i = 1
	while (true) do
		local id = i
		if (id == 1) then
			id = ""
		end
		if (not localdata['iucn' .. id]) then
			break  -- pas / plus d'entrées
		end
		local ligne = {}
		ligne['niveau'] = localdata['iucn' .. id]
		ligne['critère'] = localdata['iucn' .. id .. '-critère']
		ligne['commentaire'] = localdata['iucn' .. id .. '-commentaire']
		cur = build_iucn(ligne)
		-- on l'ajoute
		if (cur ~= nil) then
			if (out == nil) then
				out = { cur }
			else
				table.insert(out, cur)
			end
		end
		i = i + 1
	end
	return out
end


local function build_image(nb)
	local tmp
	if (localdata['légende' .. nb]) then
		return {
			type = 'images',
			imageparameters =  'image' .. nb,
			captionparameter = 'légende' .. nb,
			separator = 'up',
			['separator style'] = {['background-color']='#FFE4C4'},
		}
	else
		return {
			type = 'images',
			imageparameters =  'image' .. nb,
			separator = 'up',
			['separator style'] = {['background-color']='#FFE4C4'},
		}
	end
end

local function images()
	local out = nil
	
	i = 2
	while (true) do
		if (not localdata['image' .. i]) then
			break  -- pas / plus d'entrées
		end
		cur = build_image(2)
		-- on l'ajoute
		if (cur ~= nil) then
			if (out == nil) then
				out = { cur }
			else
				table.insert(out, cur)
			end
		end
		i = i + 1
	end
	return out
end


local function genere()
	local out = {}
	localdata["taxon-mef"] = italiques.italique_biologique(localdata["taxon"], localdata["rang"], localdata["charte"])
	
	out = {
		maincolor = '#FFA07A',
		-- secondcolor = '#FF0000',
		parts = {
			{
				type = 'title', 
				value = 'taxon-mef',
			},
			{
				type = 'images',
				imageparameters =  'image',
				captionparameter = 'légende',
				defaultimage = 'Defaut 2.svg',
			},
		}
	}
	local tmp = build_classification()
	if (not tmp) then
		table.insert(out['parts'], erreur('Classification', 'Erreur inconnue', 'Erreur de classification'))
	else
		if (tmp[1]) then
			table.insert(out['parts'], tmp[1])
		end
	end
	
	table.insert(out['parts'], 
			{
				type = 'bloc',
				titlestyle={['background-color']='#FFE4C4'},
				title = data.rangs[localdata['rang']]["wikif long"],
				style = {['text-align'] = 'center'},
				text = "<b>" .. localdata["taxon-mef"] .. "</b><br/><b><small>" .. localdata["auteur"] .. "</small></b>",
			})
	table.insert(out['parts'], synonymes())

	-- les autres classifications si présentes
	if (tmp and tmp[2]) then
		table.insert(out['parts'], tmp[2]) -- on n'accepte qu'une seule autre
	end
	tmp = images()
	if (tmp ~= nil) then
		for _, v in pairs(tmp) do
			table.insert(out['parts'], v)
		end
	end
	tmp = cites()
	if (tmp ~= nil) then
		for _, v in pairs(tmp) do
			table.insert(out['parts'], v)
		end
	end
	tmp = iucn()
	if (tmp ~= nil) then
		for _, v in pairs(tmp) do
			table.insert(out['parts'], v)
		end
	end
	
	return out
end


return genere()