Módulo:Infobox/Location

Wikipediamanta

La documentación para este módulo puede ser creada en Módulo:Infobox/Location/doc

local localdata = require 'Módulo:Infobox / Localdata'
local general = require "Módulo:Infobox / Functions"
local building = require "Módulo:Infobox / Functions/B"
local wikidata = require "Módulo:Wikidata"
local linguistic = require "Módulo:Simi-yachay"
local convert = require "Módulo: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'absence d'article Qichwa simipi 
	Q17 = 'jawiki', -- Nihun
	Q182 = 'dewiki', -- Alemanya
	Q40 = 'dewiki',	-- Ostriya
	Q38 = 'itwiki', -- Italya
	Q29 = 'eswiki', -- Kastilla
	Q96 = 'eswiki', -- Mishiku
	Q142 = 'frwiki', -- Ransiya
	Q148 = 'zhwiki', -- Chunwa
}
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 Módulo: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 = 2
	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, 'qu', 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
		
		if rowlabelid then
			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
		end
		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

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

local natureformats = {
	--{élément Wikidata, nom charte, couleur titre, couleur sous-titre, couleur texte, icône)  doivent être classés du plus spécifique au plus général
    {'Q23442' , 'île', '#7793E0', '' , '#FFFFFF', 'map'},
    {'Q123705', 'quartier' , '#ECE5CA' , '' , '' , 'map'},
    {'Q8502' , 'montagne' , '#996633', '' , '#FFFFFF' , 'map'},
    {'Q12280' , 'pont', '#D4D0BA' , '' , '' , 'map' },
}

local function setformat() --retourne une table contenant la couleur principale de l'infobox et l'icône de titre
	local natures = wikidata.getIds(localdata.item, {property = 'P31'}) -- récupère les Qid des natures

	if not natures or (#natures > 1) then -- si plusieurs natures, on ne sait pas lequel choisir
		return  {'Q', '', '#DDFFDD', '', '', 'map'}
	end
	natures = wikidata.addVals(natures, {property = 'P279'}, 2) -- nombre d'étages de sous-classes à remonter
	for i, j in pairs(natures) do
		for k, l in pairs(natureformats) do
			if l[1] == j then
				return l
			end
		end
	end
	return {'Q', '', '#DDFFDD', '', '' , 'map'}
end
local function setcharte()
	if localdata['charte'] then
		charte = localdata['charte']
		for i, j in pairs(natureformats) do
			if j[2] == charte then
				return j
			end
		end
	end
	return setformat()
end
local natureformat = setcharte()
return {
	maincolor = natureformat[3],
	secondcolor = natureformat[4],
	thirdcolor = natureformat[5],
	parts =
    	{
			general.title(natureformat[6]),
			{
			type = 'images',
			imageparameters =  {'blason', 'drapeau'},
			wikidata = {property = {'P94', 'P41'}},
			numval = 2,
			defaultupright = '0.8'
			},	
			general.logo(),
			general.mainimage{ 
				cat = "Subdivision administrative illustrée par defaut.svg", 
				defaultimage = "Defaut 2.svg", 
				wikidata = {property = {"P2716", "P18", "P5252", "P3451", "P1766"}, numval = 1} 
			},
			{type = "table", rows = {
				{type = "row", label = "Nom officiel", plurallabel = "Noms officiels", value = "nom officiel", wikidata = {property = "P1448", showlang = true, showdate = true, precision = 'year', sorttype = 'chronological', conjtype = 'new line'} },
				{type = "row", label = "Nom local", plurallabel = "Noms locaux", value = "nom local", wikidata = {property = "P1705", showlang = true} },
				{type = 'row', label = 'Surnom', plurallabel = "Surnoms", value = "surnom", property = 'P1449'}, -- autres noms
				}
			},
			{type = "table", title = "Géographie", rows = {
				{type = 'row', label = 'Astre', value = 'astre', wikidata = { property = 'P376', excludevalue = 'Q2' }}, -- corps astronomique où se trouve le lieu dont il est question (utile pour les lieux de fictions)
				general.country(),
				{type = "multi", rows = localDivRows() or wikidataDivRows() or {}},	
				{type = "row", label = "Île", value = "île", property = "P5130"},
				{type = "row", label = "Localisation géographique", value = "localisation géographique", property = "P706"},
				{type = "row", label = "Partie de", value = "partie de", property = "P361"},
				{type = "row", label = "[[Chef-lieu]]", value = "chef-lieu", property = "P36"},
				{type = "row", label = "Capitale de", value = "capitale de", property = "P1376"},
				{type = "row", label = "Exclave de", value = "exclave de", property = "P500"},
				{type = "row", label = "Enclavé dans", value = "enclavé dans", property = "P501"},
				{type = "row", label = "Revendiqué par", value = "revendiqué par", property = "P1336"},
				{type = "row", label = "Baigné par", value = "baigné par", property = "P206"},
				{type = "row", label = "Superficie", value = function() return formatArea() end },
				{type = "row", label = "Surface en eau", value = "surface en eau", property = "P2927"},
				{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 = "Point le plus bas", plurallabel = "Points les plus bas", value = "point le plus bas", property = "P1589"},
				{type = "row", label = "Point culminant", plurallabel = "Points culminants", value = "point culminant", property = "P610"},
				{type = "row", label = "Altitude", value = "altitude", wikidata = {property = "P2044", targetunit = "metre", rounding = "0"} },
				building.mountainrange(),
				{type = "row", label = "Aire protégée", plurallabel = "Aires protégées", value = "aire protégée", wikidata = {property = "P3018", showqualifiers = "P518"}},
				{type = "row", label = "Géographie", value = "géographie", property = "P2633"},
				general.coordinates(),
				}
			},
			general.locationmap(),
			{type = "table", title = "Démographie", rows = {
				{type = "row", label = "Population", value = function() return formatPop() end },
				{type = "row", label = "Nombre de ménages", value = "nombre de ménages", wikidata = {property = "P1538", showqualifiers = {"P585"} , sorttype="inverted", numval = 1}},
				{type = "row", label = "Densité", value = function() return density() end},
				{type = "row", label = "Gentilé", value = "gentilé", property = "P1549"},
				{type = "row", label = "Taux de chômage", value = "taux de chômage", property = "P1198"},
				{type = "row", label = "Électorat", value = "électorat", property = "P1831"},
				}
			},
			{type = "table", title = "Fonctionnement", rows = {
				{type = "row", label = "Statut", value = "statut", wikidata = {property = "P31", excludevalues = {"Q515", "Q532", "Q486972", "Q123705", "Q3957", "Q3257686", "Q23413"}, defaultlinkquery = {property = "P2354"}, showdate = true}}, -- exclut les valeurs qui correspondent pas à un statut administratif
				{type = 'row', label = 'Constitution', value = 'constitution'},
				{type = "row", label = localdata["libellé chef de l'exécutif"] or "Chef de l'exécutif", value = "chef de l'exécutif", wikidata = {property = "P6", showdate = true, textformat = 'long', sorttype="inverted", numval = 1}}, -- pour le libellé : utiliser P1313 poste occupé par le chef de l'exécutif" ? il faudrait voir comment retraiter les cas comme Q31388880 "maire de Brême"
				{type = "row", label = "Assemblée délibérante", value = "assemblée", wikidata = {property = "P194", defaultlink = defaultlink}},
				{type = "row", label = "Monnaie", plurallabel = "Monnaies", value = "monnaie", property = "P38"},
				{type = "row", label = "Équipement", plurallabel = "Équipements", value = "équipement", property = "P912"},
				{type = "row", label = "Employés", value = "employés", property = "P1128"},
				{type = "row", label = "Géographie", value = "géographie", property = "P2633"},
				{type = "row", label = "Économie", value = "économie", property = "P8744"},
				building.protection(),
				{type = "row", label = "[[Propriété contributrice à un district historique|Propriété contributrice]]", plurallabel = "[[Propriété contributrice à un district historique|Propriétés contributrices]]", value = "propriété contributrice", wikidata = {property = "P527", numval = "5", showdate = true, textformat = "minimum", precision = "year", sorttype = "chronological", qualifier = "P3831", qualifiervalue = "Q1129142"}},
				{type = "row", label = "Contient la localité", plurallabel = "Contient les localités",value = "contient la localité", wikidata = {property = "P1383", showdate = true}},
				{type = "row", label = "Membre de", value = "membre de", wikidata = {property = "P463", showdate = true}},
				{type = "row", label = "Jumelage", plurallabel = "Jumelages", value = "jumelage", wikidata = {property = "P190", showdate = true}},
				{type = 'row', label = "[[Office de tourisme|Informations]]", value = "informations", wikidata = {property = 'P2872', numval = '1'}},
				}
			},
			{type = "table", title = "Histoire", rows = {
				{type = "row", label = "Origine du nom", value = "origine du nom", property = "P138"},
				{type = "row", label = "Concepteur", plurallabel = "Concepteurs", value = "concepteur", property = "P287"},
				{type = "row", label = "Fondation", value = "fondation", property = "P571"},
				{type = "row", label = "Fondateur", plurallabel = "Fondateurs", value = "fondateur", property = "P112"},
				{type = "row", label = "Architecte", plurallabel = "Architectes", value = "architecte", property = "P84"},
				building.archistyle(),
				{type = "row", label = "Remplace", value = "remplace", property = "P1365"},
				{type = "row", label = "Remplacé par", value = "remplace", property = "P1366"},
				{type = "row", label = "Événement clé", plurallabel = "Événements clés", value = "événement clé", wikidata = {property = "P793", showdate = true}},
				{type = 'row', label = 'Histoire', value = 'histoire', wikidata = {property = 'P2184', withlink = 'quwiki', numval = '1'}},
				{type = "row", value = "Dissolution", property = "P576" , label = function() return localdata['intitulé dissolution'] or 'Dissolution' end, },
				{type = "row", label = "Direction des fouilles", value = "fouilles", property = "P4345"},
				}
			},
			{type = "table", title = "Identité", rows = {
				{type = "row", label = "ISO 3166-2", value = "iso", property = "P300"},
				{type = "row", label = "Langue officielle", plurallabel = "Langues officielles", value = "langue officielle", property = "P37"},
				{type = "row", label = "Devise", value = "devise", property = "P1451"},
				{type = "row", label = "Blasonnement", value = "blasonnement", property = "P237"},
				{type = "row", label = "Drapeau", value = "description drapeau", property = "P163"},
				{type = 'row', label = 'Hymne', plurallabel = 'Hymnes', value = 'hymne', property = 'P85'},
				{type = "row", label = "Symbole", plurallabel = "Symboles", value = "symbole", property = "P2238"},
				{type = "row", label = "Fête", plurallabel = "Fêtes", value = "fête", property = "P841"},
				{type = "row", label = "Saint patron", plurallabel = "Saints patrons", value = "saint patron", property = "P417"},
				{type = "row", label = "Plat traditionnel", plurallabel = "Plats traditionnels", value = "plat traditionnel", property = "P868"},
				}
			},
			{type = "table", title = "Identifiants", rows = {
				{type = "row", label = "Code postal", value = "code postal", property = "P281"},
				{type = 'row', label = "[[Statistisches Bundesamt|AGS]]", value = "AGS",Iproperty = 'P439'},
				{type = 'row', label = "[[Statistique Indonésie|Code de desa]]", value = "BPS", property = 'P1588'},
				{type = "row", label = "[[Federal Information Processing Standard|Code FIPS]]", value = "FIPS", property = "P774"},
				{type = "row", label = "[[Geographic Names Information System|GNIS]]", value = "GNIS", wikidata = {property = "P590", urlpattern= "https://geonames.usgs.gov/pls/gnispublic/f?p=gnispq:3:::NO::P3_FID:$1"}},
				{type = 'row', label = "[[Institut national de la statistique (Espagne)|INE]]", value = "INE municipalités", property = 'P772'},
				{type = 'row', label = "[[Code Insee|INSEE]]", value = 'INSEE', property = 'P374'},
				{type = 'row', label = "[[Istituto nazionale di statistica|ISTAT]]", value = "ISTAT", property = 'P635'},
				{type = 'row', label = "Code [[Departamento Administrativo Nacional de Estadística|DANE]]", value = "DANE", property = 'P7325'},
				{type = 'row', label = "[[OKATO]]", value = "OKATO", property = 'P721'},
				{type = 'row', label = "[[Office for National Statistics|Code ONS]]", value = "ONS"},
				{type = 'row', label = "TGN", value = "TGN", wikidata = {property = "P1667", urlpattern= "http://vocab.getty.edu/page/tgn/$1"}},
				{type = "row", label = "Indicatif téléphonique", value = "indicatif téléphonique", property = "P473"},
				general.website(),
				{type = "row", label = "Représenté par", value = "représenté par", property = "P1875"},
				{type = "row", label = "Distinction", plurallabel = "Distinctions", value = "distinction", wikidata = {property = "P166", showdate = true}},
				}
			},
			general.geoloc({marker = 'village'}),
			general.prononciation(),
	}
}
"https://qu.wikipedia.org/w/index.php?title=Módulo:Infobox/Location&oldid=646871" p'anqamanta chaskisqa (Wikipedia, Qhichwa / Quechua)