Module:Populacion de France/Balyês

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

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

--[[
  Sous-module de Module:Tableau population d'article de commune de France contenant
  les fonctions de génération des notes associées à des données de population
--]]

local p = {}

p.outils = require("Module:Populacion de France/Outils")
p.constantes = require("Module:Populacion de France/Constantes")


--[[
  fonction de chargement des données, via module de données
  retourne la structure de données (ou nil si échec de chargement)
  et en seconde valeur true si les données sont complètes, un numéro d'erreur sinon
--]]
function p.charge_balyes(nom)
	-- vérification
	if ((nom == nil) or (nom == "")) then
		return nil, 1
	end
	local balyes = "Module:Balyês/" .. nom .. "/èvolucion populacion"
	-- données présentes ?
	local etat, data = pcall(require, balyes)
	if (etat == false) then
		return nil, 2
	end
	data.module = balyes

	-- on vérifie la présence des données nécessaires
	-- données communes
	if ((data["nom"] == nil) or (data["division"] == nil)) then
		return data, 3
	end
	-- division supportée ?
	if (p.constantes.divisions[data["division"]] == nil) then
		return data, 4
	end
	-- superficie présente ?
	if ((data["superficie"] == nil) or (tonumber(data["superficie"]) == nil)) then
		return data, 4
	end
	data["superficie"] = tonumber(data["superficie"])

	-- données spécifiques (selon la division)
	-- spécifiques aux communes
	if ((data["division"] == "comena") or (data["division"] == "comena en DROM") or
		(data["division"] == "comena en COM1") or (data["division"] == "comena novèla") or
		(data["division"] == "comena en COM2") or (data["division"] == "comena en COM3") or
		(data["division"] == "comena dèlègâye") or (data["division"] == "vielye comena") or
		(data["division"] == "comena associyêe")) then
		if ((data["nom-dep"] == nil) and (data["division"] ~= "comena en DROM")) then
			return data, 5  -- obligatoire, sauf pour DROM
		end
		if (data["insee"] == nil) then
			return data, 5
		end
		if (data["recens-prem"] == nil) then
			return data, 5
		end
		if (data["recens-prem"] ~= "anuâl") then
			if (tonumber(data["recens-prem"]) == nil) then
				return data, 5  -- vaut annuel ou une année
			end
			data["recens-prem"] = tonumber(data["recens-prem"]) -- en cas de "
		end
	else
		-- pour les autres il faut un recens-prem = annuel
		data["recens-prem"] = "anuâl"
	end
	-- À FAIRE pour les autres
	
	-- on "calcule" les données qui peuvent être reconstruites
	if (data["nom-wp"] == nil) then
		-- reconstruction du wikilien
		local nom = data.nom:gsub( ' %(.+', '' )
		if nom == data.nom then
			data["nom-wp"] = '[[' .. nom .. ']]'
		else
			data["nom-wp"] = '[[' .. data.nom .. '|' .. nom .. ']]'
		end
	end
	-- le type (permet de regrouper toutes les communes)
	data["tipo"] = p.constantes.divisions[data["division"]]
	-- données calculées
	local nb = 0  -- nombre d'années dans les données
	local mmin = -1  -- min/max des années avec référence
	local mmax = -1
	local pmin = -1  -- population min/max
	local pmax = -1
	local premier = -1  -- première et dernière année
	local derrier = -1
	-- on reconstruit les valeurs min/max/nb
	for an, pop in pairs(data) do
		if (type(an) == "number") then
			nb = nb + 1
			if (pop["ref"] ~= nil) then
				if (an > mmax) then
					mmax = an
				end
				if ((mmin == -1) or (an < mmin)) then
					mmin = an
				end
			end
			vpop = tonumber(mw.text.trim(pop["pop"]))
			if (vpop > pmax) then
				pmax = vpop
			end
			if (vpop > 0) then -- on ne compte pas "-1" qui indique "pas de valeur"
				if ((pmin == -1) or (vpop < pmin)) then
					pmin = vpop
				end
			end
			-- idem premier/dernier
			if (an > derrier) then
				derrier = an
			end
			if ((premier == -1) or (an < premier)) then
				premier = an
			end
		end
	end
	if (mmin ~= -1) then
		data["minref"] = mmin
	end
	if (mmax ~= -1) then
		data["maxref"] = mmax
	end
	if (pmin ~= -1) then
		data["popmin"] = pmin
	end
	if (pmax ~= -1) then
		data["popmax"] = pmax
	end
	if (premier ~= -1) then
		data["premier"] = premier
	end
	if (pmax ~= -1) then
		data["derrier"] = derrier
	end
	data["nombro"] = nb-1

	-- on retourne les données
	return data, true
end

--[[
  Calcule la variation de population (en %, signé, arrondi à 2 digits)
  sur les 5 dernières années, ou nil si pas trouvé / problème
--]]
function p.variacion(data)
	-- vérifications
	if (data == nil) then
		return nil
	end
	if ((data["derrier"] == nil) or (data[data["derrier"]] == nil)) then
		return nil
	end
	-- s'il n'y a pas 5 ans, on prend le plus ancien
	local ciba = data["derrier"]-5
	if (data[ciba] == nil) then
		return nil
		-- désactivé
		-- ciba = data["premier"]
	end
	return p.outils.round(100*(data[data["derrier"]]["pop"] - data[ciba]["pop"]) /
		                  data[ciba]["pop"], 2), ciba
end

--[[
  Utilise la fonction précédente pour créer la phrase "en augmentation/diminution de X % par rapport à NNN"
  retourne en second paramètre +1 si augmentation, -1 si réduction, 0 si stagnation
--]]
function p.variacion_texto(data)
	local delta, ciba = p.variacion(data)
	if (delta ~= nil) then
		if (delta < 0) then
			return "en diminucion de " .. mw.language.getContentLanguage():formatNum(-delta) .. " % per rapôrt a " .. ciba, -1
		elseif (delta > 0) then
			return "en ôgmentacion de " .. mw.language.getContentLanguage():formatNum(delta) .. " % per rapôrt a " .. ciba, 1
		else -- égal
			return "en stagnacion per rapôrt a " .. ciba, 0
		end
	else
		return nil
	end
end

--[[
  Retourne une table contenant { année, population } correspondant
  aux données les plus récentes pour cette commune
--]]
function p.valor_novela(data)
	if ((data["derrier"] == nil) or (data[data["derrier"]]["pop"] == nil)) then
		return nil
	end
	return { data["derrier"], data[data["derrier"]]["pop"] }
end


return p