Module:Populacion de France

De Vouiquipèdia, l’enciclopèdia abada.

La documentation pour ce module peut être créée à Module:Populacion de France/doc

local p = {}


--
-- Los modulos et sot-modulos qu’ils aront fôta
--

-- lo modulo dèmografia
p.demografia = require("Module:Dèmografia")
-- lo modulo chârtes
p.chartes = require("Module:Chârtes")

-- charjont los sot-modulos
p.constantes = require("Module:Populacion de France/Constantes")
p.sorses = require("Module:Populacion de France/Sôrses")
p.notes = require("Module:Populacion de France/Notes")
p.balyes = require("Module:Populacion de France/Balyês")
p.outils = require("Module:Populacion de France/Outils")
p.entro = require("Module:Populacion de France/Entroduccions")


--[[
  fonction récupérant les différentes données et appelant
  le module démographie pour l'affichage effectif
--]]
function p.grelye_m(param)
	-- le nom de la cible (param. nom ou titre page)
	local ttl = mw.title.getCurrentTitle().subpageText
	local wlm = false
	local vnom = param["nom"] or ttl
	local balyes = "Module:Balyês/" .. vnom .. "/èvolucion populacion"
	
	-- on charge les données
	local data, wlm = p.balyes.charge_balyes(vnom, false)
	if (type(wlm) == "number") then
		return p.outils.err_nodata("grelye", balyes, wlm)
	end

	-- on prépare les paramètres à fournir au module Démographie
	local dest = {}
	
	-- éléments constants
	dest["largior_grelye"] = "48em"
	dest["talye_police"] = "95%"
	dest["mârge_entèrlegnes"] = "10px"
	dest["hôtior_legnes"] = "110%"
	dest["populacion_fond"] = "#F9F9F9"
	dest["stilo_notes"] = "centrâ"
	
	-- couleur (on passe par Chartes avec un "truc" pour commune nouvelle)
	if (data["division"] == "comena novèla") then
		dest["ans_fond"] = p.chartes.charta_m("g·eografia", "secondera", "comena", true)
	else
		dest["ans_fond"] = p.chartes.charta_m("g·eografia", "secondera", data["division"], true)
	end

	-- le titre du tableau
	dest["titro"] = (param["titro"] or p.constantes.titro_grelye)
		.. " &nbsp;<small>[&#8202;[[" .. balyes .. "|changiér]]&#8202;]</small>"
	
	-- notes
	local tmp = p.notes.notes(data, vnom)
	if ((tmp ~= nil) and (mw.text.trim(tmp) ~= "")) then
		dest["notes"] = tmp
	end
	
	-- sources
	tmp = p.sorses.sorses(data)
	if ((tmp ~= nil) and (mw.text.trim(tmp) ~= "")) then
		dest["sôrses"] = tmp
	end
	
	-- maintenant on parcours les données elles-mêmes
	for an, pop in pairs(data) do
		-- les années sont les éléments numériques du tableau
		if (type(an) == "number") then
			-- est-ce qu'on garde l'année ?
			if (p.outils.test_an(an, data)) then
				vpop = tonumber(mw.text.trim(pop["pop"]))
				if (vpop > 0) then
					dest[an] = vpop  -- on stocke
				else
					dest[an] = "-"
				end
			end
		end
	end

	-- on effectue l'appel terminal et on retourne (et on ajoute la catégorie)
	return p.demografia.demografia_m(dest) .. p.constantes.cat_usajo
end

-- fonction "wrapper" pour la précédente, pour appel depuis un modèle
function p.grelye(frame)
	local param = {}
	-- deux paramètres : nom et titre
	param["nom"] = p.outils.neteye(frame.args["nom"] or frame:getParent().args["nom"] or nil)
	param["titro"] = p.outils.neteye(frame.args["titro"] or frame:getParent().args["titro"] or nil)

	-- on appelle la vraie fonction
	return p.grelye_m(param)
end

--[[
  Fonction créant une introduction de population de commune
--]]
function p.entroduccion_m(param)
	local resu
	local nom = param["nom"] or mw.title.getCurrentTitle().subpageText
	local balyes = "Module:Balyês/" .. nom .. "/èvolucion populacion"

	-- on charge les données associées à la commune
	local data, wlm = p.balyes.charge_balyes(nom)
	if (type(wlm) == "number") then
		return p.outils.err_nodata("entroduccion", balyes, wlm)
	end
	if (data["division"] == "fraccion cantonâla") then
		return p.outils.err_nodata("entroduccion", balyes, 6) -- pas d'intro pour ça
	end
	
	resu = p.entro.entroduccion(data)

	return resu
end

-- fonction "wrapper" pour la fonction précédente, pour appel depuis un modèle
function p.entroduccion(frame)
	local pframe = frame:getParent()
	local param = {}
	-- récupération de l'unique paramètre (nom)
	param["nom"] = p.outils.neteye(frame.args["nom"] or pframe.args["nom"] or nil)
	return p.entroduccion_m(param)	
end


-- fonction de tri utilisée par la fonction suivante
-- tri sur la valeur du 1er élément des éléments
function p.sort_per_an(el1, el2)
	if (el1[1] < el2[1]) then
		return true
	end
	return false
end

--[[
  Fonction créant un graphique de population de commune
--]]
function p.grafico_m(param)
	local resu
	local nom = param["nom"] or mw.title.getCurrentTitle().subpageText
	local balyes = "Module:Balyês/" .. nom .. "/èvolucion populacion"

	-- on charge les données associées à la commune
	local data, wlm = p.balyes.charge_balyes(nom)
	if (type(wlm) == "number") then
		return p.outils.err_nodata("grafico", balyes, wlm)
	end
	if (data["division"] == "fraccion cantonâla") then
		return p.outils.err_nodata("grafico", balyes, 6) -- pas d'intro pour ça
	end

	-- seuil (nombre de données) pour afficher
	if ((param["sol"] == nil) or (param["sol"] <= 0)) then
		param["sol"] = 5 -- valeur par défaut
	end

	-- "Histogramme population manuel" étant un modèle, on fabrique la structure d'appel
	-- ici il faudrait modifier "couleur-barres" qui devrait dépendre de la charte…
	prm = { ["largior"] = 710, ["hotior"] = 340, ["color-fond"] = "rgb(1,1,1)" }
	local cb
	if (data["division"] == "comena novèla") then
		cb = p.chartes.charta_m("g·eografia", "primera", "comena", true)
	else
		cb = p.chartes.charta_m("g·eografia", "primera", data["division"], true)
	end
	cb = p.outils.color2rgb(cb)
	if (cb == nil) then
		prm["color-barres"] = "rgb(0.7,0.9,0.7)" -- couleur "par défaut"
	else
		prm["color-barres"] = "rgb(" .. cb .. ")"
	end

	prm["nom"] = param["nom"]
	prm["titro"] = param["titro"] or p.constantes.titro_grafico
	prm["max"] = data["popmax"]
	-- parcours des données
	local tmp = {}
	for an, pop in pairs(data) do
		if (type(an) == "number") then
			-- est-ce qu'on garde l'année ?
			if (p.outils.test_an(an, data) and (type(pop["pop"]) == "number") and (pop["pop"] > 0)) then
				table.insert(tmp, { an, pop["pop"] })
			end
		end
	end

	-- tri des éléments
	table.sort(tmp, p.sort_per_an)
	-- on insert année / valeur dans l'ordre (histogramme pop… a besoin des données dans l'ordre)
	local nb = 1
	local nbp = 0
	for k, v in pairs(tmp) do
		if (v[2] > 0) then
			prm[nb] = v[1]
			prm[nb+1] = v[2]
			nb = nb + 2
			nbp = nbp + 1  -- nombre de barres (réel)
		end
	end
	-- si pas assez de données on ne traite pas
	if (nbp < param["sol"]) then
		return ""  -- on retourne "rien"
	end

	prm["nombro"] = nbp
	prm["sorses"] = p.sorses.sorses_grafico(data)

	-- précaution
	if (nbp == 0) then
		p.outils.err_nodata("grafico", balyes, 7, true)
	end

	-- on "appelle" le modèle
	return mw.getCurrentFrame():expandTemplate{ title = 'Histogramo populacion manuâl', args = prm }
end

-- fonction "wrapper" pour la fonction précédente, pour appel depuis un modèle
function p.grafico(frame)
	local pframe = frame:getParent()
	local param = {}
	-- récupération des paramètre (nom et titre)
	param["nom"] = p.outils.neteye(frame.args["nom"] or pframe.args["nom"] or nil)
	param["titro"] = p.outils.neteye(frame.args["titro"] or pframe.args["titro"] or nil)
	param["sol"] = p.outils.neteye(frame.args["sol"] or pframe.args["sol"] or nil)
	return p.grafico_m(param)	
end


--[[
  Retourne la dernière population (ou la dernière date) pour la commune "nom" (ou courante)
  Si prm[1] est vide retourne la population, sans formatage
  Si prm[1] vaut "date", retourne la date
  Si prm[1] vaut "nombre", idem premier cas mais avec formatage du nombre
--]]
function p.derriere_populacion_m(prm)
	local nom = prm["nom"] or mw.title.getCurrentTitle().subpageText
	local balyes = "Module:Balyês/" .. nom .. "/èvolucion populacion"

	-- on charge les données associées à la commune
	local data, wlm = p.balyes.charge_balyes(nom)
	if (type(wlm) == "number") then
		return p.outils.err_nodata("derriere_populacion", balyes, wlm, true)
	end
	
	local tmp = p.balyes.valor_novela(data)
	if (tmp == nil) then
		return p.outils.err_nodata("derriere_populacion", balyes, 7, true)
	end
	
	if (prm["tipo"] == "dâta") then
		return tmp[1]
	end
	if ((prm["tipo"] == nil) or (mw.text.trim(prm["tipo"]) == "")) then
		return tmp[2]
	end
	return mw.language.getContentLanguage():formatNum(tmp[2])
end

-- fonction "wrapper" pour la fonction précédente, pour appel depuis un modèle
function p.derriere_populacion(frame)
	local pframe = frame:getParent()
	local param = {}
	-- récupération des paramètre (nom et titre)
	param["nom"] = p.outils.neteye(frame.args[2] or pframe.args[2] or nil)
	param["tipo"] = frame.args[1] or pframe.args[1] or nil
	return p.derriere_populacion_m(param)	
end


--[[
  Retourne une phrase décrivant la variation de population de la commune
    courante (ou "nom") ou un pictograme si "type" = "picto"
--]]
function p.variacion_texto_m(prm)
	local nom = prm["nom"] or mw.title.getCurrentTitle().subpageText
	local balyes = "Module:Balyês/" .. nom .. "/èvolucion populacion"

	-- on charge les données associées à la commune
	local data, wlm = p.balyes.charge_balyes(nom)
	if (type(wlm) == "number") then
		return p.outils.err_nodata("variacion_texto", balyes, wlm, true)
	end
	
	local txt, sens = p.balyes.variacion_texto(data)
	if (txt == nil) then
		return p.outils.err_nodata("variacion_texto", balyes, 7, true)
	end
	if (prm["tipo"] ~= "pictô") then
		return txt
	end
	-- on crée le pictogramme
	if (sens > 0) then
		return "[[Fichiér:Increase2.svg|11px|" .. txt .. "]]"
	elseif (sens < 0) then
		return "[[Fichiér:Decrease2.svg|11px|" .. txt .. "]]"
	else -- égal
		return "[[Fichiér:Steady.svg|10px|" .. txt .. "]]"
	end
end

-- fonction "wrapper" pour la fonction précédente, pour appel depuis un modèle
function p.variacion_texto(frame)
	local pframe = frame:getParent()
	local param = {}
	-- récupération des paramètre (nom et titre)
	param["nom"] = p.outils.neteye(frame.args["nom"] or pframe.args["nom"] or nil)
	param["tipo"] = frame.args["tipo"] or pframe.args["tipo"] or nil
	return p.variacion_texto_m(param)	
end


--[[
  Retourne la densité de la commune. Si "précision" indiquée utilise celle-ci,
  sinon précision 1 par défaut
--]]
function p.densitat_m(prm)
	local nom = prm["nom"] or mw.title.getCurrentTitle().subpageText
	local balyes = "Module:Balyês/" .. nom .. "/èvolucion populacion"

	-- on charge les données associées à la commune
	local data, wlm = p.balyes.charge_balyes(nom)
	if (tipo(wlm) == "number") then
		return p.outils.err_nodata("densitat", balyes, wlm, true)
	end

	local prc = 1
	if (type(prm["prècision"]) == "number") then
		prc = prm["prècision"]
	end
	if ((data[data["derrier"]]["pop"] == nil) or (data["superficie"] == nil)) then
		return p.outils.err_nodata("densitat", balyes, 7, true)
	end
	
	if (prm["format"] ~= nil) then
		return mw.language.getContentLanguage():formatNum(
			p.outils.round(data[data["derrier"]]["pop"] / data["superficie"], prc))
	else
		return p.outils.round(data[data["derrier"]]["pop"] / data["superficie"], prc)
	end
end

-- fonction "wrapper" pour la fonction précédente, pour appel depuis un modèle
function p.densitat(frame)
	local pframe = frame:getParent()
	local param = {}
	-- récupération des paramètre (nom et titre)
	param["nom"] = p.outils.neteye(frame.args["nom"] or pframe.args["nom"] or nil)
	param["prècision"] = tonumber(frame.args["prècision"] or pframe.args["prècision"] or nil)
	param["format"] = frame.args["format"] or pframe.args["format"] or nil
	return p.densitat_m(param)	
end

return p