Module:Infobox/Plan d'eau/Bac à sable

Une page de Wikipédia, l'encyclopédie libre.

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

Cette page définit un module d'infobox. Pour les conseils sur l'usage de ce module, voyez Modèle:Infobox Plan d'eau/Bac à sable.


local localdata = require 'Module:Infobox/Localdata'
local building = require "Module:Infobox/Fonctions/Bâtiment"
local wikidata = require "Module:Wikidata"
-- local general = require "Module:Infobox/Fonctions"
local general = require "Module:Infobox/Fonctions/Bac à sable"
local l = require "Module:Infobox/Fonctions/Localité"
local linguistic = require "Module:Linguistique"
local convert = require "Module:Conversion"
local country = wikidata.stringTable{entity = localdata.item, property = "P17", displayformat = "raw"}
local countryid
if country then
	countryid = country[1]
end

local defaultlinks = { -- version de Wikipédia à utiliser pour les liens en l'asence d'article français
	Q17 = 'jawiki', -- Japon
	Q182 = 'dewiki', -- Allemagne
	Q40 = 'dewiki',	-- Autrich
	Q38 = 'itwiki', -- Italie
}
local defaultlink = {'enwiki'}
if defaultlinks[countryid] then
	table.insert(defaultlink, defaultlinks[countryid])
end


local function getVal(addargs) -- pour une propriété donnée, retourne la meilleure valeur, ainsi que sa date, sa référence, et si ça vient ou non de Wikidata
	local args = {numval = 1, entity = localdata.item, showunit = '-', displayformat = 'raw', precision = 'year'}
	for i, j in pairs(addargs or {}) do
		args[i] = j
	end
	local statements = wikidata.getClaims(args)
	if not statements then
		return nil
	end
	local val = statements[1]
	local v = wikidata.formatStatement(val, args)
	local period = wikidata.getFormattedDate(val, args)
 	local ref = nil -- TODO : fonction dans Module:Wikidata pour récupérer les refs
 	
 	return v, period, ref, args.property
end

local function getVal(addargs) -- pour une propriété donnée, retourne la meilleure valeur, ainsi que sa date, sa référence, et si ça vient ou non de Wikidata
	local args = {numval = 1, entity = localdata.item, showunit = '-', displayformat = 'raw', precision = 'year'}
	for i, j in pairs(addargs or {}) do
		args[i] = j
	end
	local statements = wikidata.getClaims(args)
	if not statements then
		return nil
	end
	local val = statements[1]
	local v = wikidata.formatStatement(val, args)
	local period = wikidata.getFormattedDate(val, args)
 	local ref = nil -- TODO : fonction dans Module:Wikidata pour récupérer les refs
 	
 	return v, period, ref, args.property
end



-- Fonctions de récupération de données valables dans plusieurs champs (et appelées plusieurs fois)

local pop, popdate, popref, popprop = localdata["population"], localdata["année pop"], localdata["population notes"], nil
if not pop then
	pop, popdate, popref, popprop = getVal({property = "P1082", sorttype="inverted"})
end

local area, areadate, arearef, areaprop = localdata["superficie"], localdata["année superficie"], localdata["superficie notes"], nil
if not area then
	area, areadate, arearef, areaprop = getVal({property = "P2046", targetunit = "square kilometer", sorttype="inverted"})
end

if pop then pop = tonumber(pop) end
if area then area = tonumber(area) end

-- Fonctions de mise en forme

local function formatVal(val, period, ref, prop)
	local s = tostring(val)
	if period then
		s = s .. "<small>" .. linguistic.inparentheses(period) .. "</small>"
	end
	if ref then
		s = s .. "<ref>" .. ref .. "</ref>"
	end
	
	if prop then
		s = wikidata.formatAndCat{entity = localdata.item, property = prop, value = s} -- permet de mettre le rétrolien à la fin
	end
	return s
end

local function formatPop()
	if not pop then
		return nil
	end
	
	local popstr = convert.displayvalue(pop)
	popstr = string.gsub(popstr, "+", "") -- devrait être fait en amont
	popstr = popstr .. " hab."
	
	return formatVal(popstr, popdate, popref, popprop)
end

local function formatArea()
	if not area then
		return nil
	end

	-- arrondi
	local rounding = 1
	if area < 1 then
		rounding = 2
	end
	
	-- conversion en hectares si trop petit ?
	
	local areastr = convert.displayvalue(area, 'square kilometer', {showunit = true, rounding = rounding})
	return formatVal(areastr, areadate, arearef, areaprop)
end

local function density()
	-- TODO : vérifier que la date de la population correspond à celle de la superficie, pour les quelque cas de changements de frontière

	if not (pop and area) then
		return nil
	end
	local density = pop / area
	local rounding = 1
	if density < 0.1 then
		rounding = 2
	end
	local s = convert.displayvalue(density, nil, {rounding = rounding})
	s = s .. " hab./km<sup>2</sup>"
	
	return formatVal(s, popdate)
end

-- divisions amdinistratives

--- par paramètres locaux
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 division" .. i --"nom de division" .. i
		if localdata[param] then
			hasrows = true
			local label = localdata[labelparam] or "Subdivision"
			table.insert(rows, {type = "row", label = label, value = function() return localdata[param] end})
		end
	end
	if hasrows then
		return rows
	end
end

--- par Wikidata
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
	-- récupération des valeurs
	local list = wikidata.transitiveVals(localdata.item, {property = "P131", atdate = "today", excludespecial = true}, 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 rowlabel, rowlabelid, rowlabeltext, rowlabellink, rowvalue
		rowlabelid = wikidata.formatStatements{entity= div, property = 'P31', numval = 1, displayformat = 'raw', excludespecial = true}
		rowlabeltext = wikidata.getLabel(rowlabelid, 'fr', function(id) 
			local str = wikidata.getLabel(id)
			if str then
				return linguistic.ucfirst(linguistic.removecomplement(str))  -- imposé par les libellés pas très logiques sur Wikidata du genre "canton de Suisse" plutôt que "canton"		else
			end
				return ''
			end
		)
		if (not rowlabeltext) then
			rowlabeltext = 'Division'
		end
		rowlabellink = wikidata.siteLink(rowlabelid, {defaultlink = '-', defaultlinkquery= {property = "P2354"}} ) -- si pas de lien, lier vers la page de liste e.g liste des Etats de Californie

		if rowlabellink then
			rowlabel = "[[" .. rowlabellink .. "|" .. rowlabeltext .. "]]"
		else
			rowlabel = rowlabeltext
		end
		rowvalue = wikidata.formatEntity(div, {labelformat = function(id) 
			local str = wikidata.getLabel(id) or ""
			if mw.ustring.match(rowlabel, str) then
				str = linguistic.keepcomplement(str)-- si le nom de la division administrative affiché est présent, ne pas le répéter
			end
			return str
			end})
		
		table.insert(rows, 1, {type = "row", label = rowlabel, value = function() return rowvalue end})
	end
	return rows
end

--========================================

return {
	maincolor = 'D0E0F0',
	parts = {
		
		general.title( "map"),
		l.mainimage(),
		{type = 'table', rows = {
			{type = 'row', label = 'Nom local', value = {'nom local', 'nom dans la langue originale'}, wikidata = {property = 'P1705', showlang = true} },
			{type = 'row', label = 'Nom officiel', plurallabel = 'Noms officiels', value = 'nom officiel', property = 'P1448'},
		}},
		
		{type = 'table', title = 'Localisation', rows = {
			{type = 'row', label = 'Continent', plurallabel = 'Continents', value = 'continent', property = 'P30'},
			general.country(),
			{type = "multi", rows = localDivRows() or wikidataDivRows() or {}},	
			{type = 'row', label = 'Partie de', value = 'partie de', property = 'P361'},
			building.protectedarea(),
			{type = 'row', label = 'Altitude', value = 'altitude', wikidata = {property = 'P2044', targetunit = 'metre', conjtype = ' ou ', rounding = '0'} },
			{type = 'row', label = 'Climat', plurallabel = 'Climats', value = 'climat', property = 'P2564'},
			building.coordinates(),
		}},
		{type = 'table', title = 'Dimensions', rows = {
			{type = 'row', label = 'Longueur', value = 'longueur', wikidata = {property = 'P2043', targetunit = 'km', rounding = '2'}},
			{type = 'row', label = 'Largeur', value = 'largeur', wikidata = {property = 'P2049', targetunit = 'km', rounding = '2'}},
			{type = 'row', label = 'Périmètre', value = 'périmètre', wikidata = {property = 'P2547', targetunit = 'km', rounding = '2'}},
			{type = 'row', label = 'Superficie', value = 'superficie', wikidata = {property = 'P2046', targetunit = 'sqkm', rounding = '0'} },
			{type = 'row', label = 'Profondeur', value = 'profondeur', wikidata = {property = 'P2610', targetunit = 'm', rounding = '0'} },
			{type = 'row', label = 'Volume', value = 'volume', wikidata = {property = 'P2234', targetunit = 'km3', rounding = '3'}},
			{type = "row", label = "Subdivision", plurallabel = "Subdivisions", value = "subdivision", wikidata =
					function(item)
					local vals = wikidata.getClaims{entity = item, property = 'P150', atdate = 'today'}
					if (not vals) or (#vals > 5) then -- si trop de subdivisions, il faut me mettre dans le corps du texte, c'est pas lisible en infobox
						return nil
					end
					return wikidata.formatAndCat{claims = vals, property = 'P150', defaultlink = defaultlink, conjtype = 'new line'}, #vals
					end
				},
			{
				type = "row",
				label = "Îles",
				value = "nombre d'îles",
				wikidata = {
					property = 'P2670',
					targetvalue = 'Q23442',
					qualifiers = 'P1114',
					showonlyqualifier = {'P1114'}
					}
			},
			general.bathymetricmap(),
		}},
		{type = 'table', title = 'Hydrographie', rows = {
			{type = 'row', label = 'Type', value = 'type', property = 'P31'},
			{type = 'row', label = 'Bassin versant', value = {'bassin versant', 'superficie du bassin versant'}, wikidata = {property = 'P2053', targetunit = 'sqkm', rounding = '0'} },
			{type = 'row', label = 'Pays du bassin', value = {'pays du bassin', 'pays du bassin versant', 'pays souverain sur le bassin versant'}, property = 'P205'},
			{type = 'row', label = 'Source', value = 'source', property = 'P885'},
			{type = 'row', label = 'Alimentation', value = 'alimentation', property = 'P200'},
			{type = 'row', label = 'Affluent', plurallabel = 'Affluents',  value = 'affluent', property = 'P974'},
			{type = 'row', label = 'Débit', value = 'débit', wikidata = {property = 'P2225', targetunit = 'm3/s', rounding = '0'}},
			{type = 'row', label = 'Rétention', value = {'rétention', 'durée de rétention'}, property = 'P3020'},
			{type = 'row', label = 'Se jette dans', value = 'se jette dans', property = 'P403'},
			{type = 'row', label = 'Émissaire', plurallabel='Émissaires', value = 'émissaire', property = 'P201'},
		}},
		
		{type = 'table', title = 'Histoire', rows = {
			{type = 'row', label = 'Origine du nom', value = 'origine du nom', property = 'P138'},
			{type = 'row', label = 'Événement clé', plurallabel = 'Événements clés', value = 'événement clé', property = 'P793'},
			building.owner(),
			{type = 'row', label = 'Équipement', plurallabel = 'Équipements', value = 'équipement', property = 'P912'},
			{type = 'row', label = 'Problème environnemental', plurallabel = 'Problèmes environnementaux', value = 'problème environnemental', property = 'P3643'},
			{type = 'row', label = 'Statut patrimonial', value = 'statut patrimonial', property = 'P1435'},
			{type = 'row', label = 'Distinction', plurallabel = 'Distinctions', value = 'distinction', property = 'P166'},
		}},
		building.geoloc({pointtype = 'lac', maptype = 'relief', marker = 'wetland', markercolor = '#778BA5'}),
	}
}