Module:Infobox/Fonctions/Division administrative/Bac à sable

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

Cette page définit un module d'infobox.


local wikidata = require "Module:Interface Wikidata".fromLua
local localdata = require "Module:Infobox/Localdata"
local linguistic = require "Module:Linguistique"
local p = {}
local formatnum = require "Module:Format"

function p.population() -- retourne la table de données population / densité
	local function popnum(localdata)
		local pop, popdate
		if localdata['population'] then 
			pop = localdata['population']
			popdate = localdata['année_pop'] or ''
		elseif localdata.item then
			local popdata = wikidata.getClaims{entity= localdata.item, property = 'P1082'}
			if popdata and popdata[1].mainsnak.snaktype == 'value' then
				pop = popdata[1].mainsnak.datavalue.value.amount
				if popdata[1].qualifiers 
					and popdata[1].qualifiers.P585 
					and popdata[1].qualifiers.P585[1].snaktype == 'value' 
				then
					popdate = popdata[1].qualifiers.P585[1].datavalue.value.time
					popdate = popdate:sub( 2, 5 )
				end
			end
		end
		return tonumber(pop), popdate
	end
	
	local function population(localdata)
		local pop, date = popnum(localdata)
		if pop then
			if date then
				return formatnum.do_formatnum{pop} .. ' (' .. date .. ')'
			else
				return formatnum.do_formatnum{pop}
			end
		end
	end

	local function density(localdata)
		local area = tonumber(localdata.superficie)
		local pop = popnum(localdata)
		if area and pop then
			return pop / area
		end
	end
	
	return {
		type = 'table', 
		title = 'Population', 
		rows = {
			{type='mixed', label = 'Population', value = population},
			{type='mixed', label = label, value = density},
		}
	}
end


function p.adminDivLong() -- list de divisions administrative en affichant le nom des types de division (ex. Commune : Orléans, Département : Loiret..)
	local function localDivRows() -- liste de lignes de divisions administrative basée sur des données locales
		local rows = {}
		local hasrows = false -- devient vraie si une valeur est non null
		for i = 1, 10 do
			i = tostring(i)
			if i == "1" then
				i = ""
			end
			local param, labelparam = "division" .. i --"nom de division" .. i
			local label = localdata[labelparam] or "Subdivision"
			if localdata[param] then
				hasrows = true
				table.insert(rows, {type = "row", label = label, param = param})
			end
		end
		if hasrows then
			return rows
		end
	end	

	local function wikidataDivRows()  -- liste de lignes de divisions administrative basée sur des données de Wikidata
		if not localdata.item then
			return nil
		end
		-- on cherche le pays pour être sûr de s'arrêter avant
		local country = wikidata.stringTable{entity = localdata.item, property = "P17", displayformat = "raw"}
		local countryid
		if country then
			countryid = country[1]
		end

		-- récupération des valeurs
		local list = wikidata.transitiveVals(localdata.item, {property = "P131"}, 3, 5, countryid)
		if not list then
			return nil
		end

		-- création des colonnes
		local rows = {}
		for i, div in pairs(list) do
			if (div == countryid) then
				break
			end

			local val = wikidata.formatEntity(div) -- le nom de l'entité administrative
			local label = wikidata.formatStatements{ -- l'intitulé du type de division administrative affiché en en-tête
				entity = div,
				property =  "P31",
				numval = 1,
				defaultlinkquery = {property = "P2354"},-- si pas de lien, lier vers la page de liste e.g liste des Etats de Califorie 
				defaultlink = "-", -- si aucun lien Wikipédia, pas de lien Wikidata
				labelformat = function(id)
					local str = wikidata.getLabel(id)
					str = mw.ustring.gsub(str or "", "%(.*", "")
					return linguistic.removecomplement(str)
				end
		}
		-- bricolage pour remplacer "Commune : X | Commune : Y" par "Commune : X, Y"
			local alreadythistype = false
			for i, row in pairs(rows) do
				if row.label == label then -- si le libellé est déjà utilisé
					local s = row.value() -- on récupère la valeur
					row.value = function() return s .. ", " .. val end -- et on y ajoute la nouvelle
					alreadythistype = true
					break
				end
			end
			if not alreadythistype then
				table.insert(rows, 1, {type = "row", label = label, value = function() return val end})
			end
		end
		return rows
	end

	local rows = localDivRows() or wikidataDivRows() or {}

	table.insert(rows, 
		{
			type = 'row',
			value = 'adresse',
			label = 'Adresse',
			wikidata = require('Module:Adresse').wikidataAddress(localdata.item),
		}
	)
	return {type = "multi", rows = rows}	
end


return p