Aller au contenu

« Module:Adresse » : différence entre les versions

De Wreck
Modèle:Infobox>Zolo
Aucun résumé des modifications
m 59 versions importées
 
(59 versions intermédiaires par 8 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
local formats = require "Module:Adresse/Formats"
local p = {}
local p = {}
local wikidata = require "Module:Wikidata"
local linguistic = require "Module:Linguistique"
local countrymodule = require "Module:Country data"


local wikidata =require('Module:Wikidata')
local function getCountry(item) -- get country id for formatting
local country = wikidata.getIds(item, {property = "P17"})
if (not country) then
return nil
end
if #country > 1 then
return nil
end
return country[1]
end


local function formatStreet(streetname, housenumber, area) -- à faire
local function numberFromClaim(claim) -- récupère le numéro de l'immeuble à partir d'un qualificatif P670 d'une affirmation Wikidata
local numberafter = 'DEU AUT' --pays dans lesquels le numéro de rue est avant le num
if not claim.qualifiers or not claim.qualifiers.P670 then
if area and string.find(numberafter, area .. ' ') then
return nil
return streetname .. ' ' .. housenumber
end
else
local vals = {}
return housenumber .. ' ' .. streetname
for i, j in pairs(claim.qualifiers.P670) do
table.insert(vals, wikidata.formatSnak(j))
end
end
return table.concat(vals, '-')
end
end


function p.formatAddress(streets, postcode, city, formattingarea)
local function directionFromClaim(claim, area) -- par exemple rue Sherbrooke Ouest
-- streets est une table de {streetname, number, }
if not claim.qualifiers or not claim.qualifiers.P560 then
-- formatting area est le lieu, normalement plus large que "city" qui détermine le formatage de la rue et du code postal
if (not streets or #streets == 0) and (not postcode) and (not city) then  
return nil
return nil
end
end
local streetstring = ''
local str = ''
for i, j in pairs(streets) do
for i, snak in pairs(claim.qualifiers.P560) do
if j.number then
local directionlabels = area.directions or formats.default.directions
j = formatStreet(j.name, j.number)
str = str .. wikidata.formatSnak(snak,  {speciallabels = directionlabels})
end
return str
end
 
local function streetFromClaims(claim) -- réupère le nom de la rue à partir d'une affirmation P669 Wikidata
return wikidata.formatStatement(claim)
end
 
local function formatStreet(streetname, housenumber, direction, displayformat)
local val = displayformat.streetline or formats.default.streetline
val = mw.ustring.gsub(val, '$number', housenumber or '')
val = mw.ustring.gsub(val, '$street', streetname or '')
val = mw.ustring.gsub(val, '$direction', direction or '')
return val
end
 
local function wikidatastreet(claim, area) --formate des données sur la rue à partir d'une affirmation Wikidata
local streetname = streetFromClaims(claim, area)
local housenumber =  numberFromClaim(claim, area)
local direction = directionFromClaim(claim, area)
return formatStreet(streetname, housenumber, direction, area)
end
 
function p.streetAddress(item, area) -- formate la ligne concernant la rue et le numéro de rue
local streets -- châine contenant le ou les rues et numéros d'immeuble
if not area then
local country = getCountry(item)
if country and formats[country] then
area = formats[country]
else
else
j = j.name
area = formats.default
end
end
streetstring = streetstring .. j .. '<br />'
end
end
if (not postcode) and (not area) then
return streetstring
-- essaye de remplir street, en priorité avec P669, type : élément
local streetclaims = wikidata.getClaims{entity = item, property = 'P669'}
if streetclaims then
for i, j in pairs(streetclaims) do
streetclaims[i] = wikidatastreet(j, area)
end
streets = mw.text.listToText(streetclaims)
streets = wikidata.formatAndCat{value = streets, entity = item, property = 'P669'}
end
end
return streetstring .. (postcode or '') .. ' ' .. (area or '')
-- sinon : P6375, type : string -- remplace P969
if not streets then
streets = wikidata.formatAndCat{linkback = true, property = 'P6375', entity = item, isinlang = 'fr'}
end
-- sinon : P969, type : string -- propriété obsolète
if not streets then
streets =  wikidata.formatAndCat{linkback = true, property = 'P969', entity = item}
end
return streets
end
end


local function numberFromClaim(claim)
function p.adminDivList(item, country) -- returns a list of admin divisions matching the criteria defined in Module:Adresse/Formats
if not claim.qualifiers or not claim.qualifiers.P670 then
country = country or getCountry(item)
local query = {property = 'P131'}
local divs = wikidata.transitiveVals(item, query, 0, 10, country)
local validDivs = {}
-- solution 1: looks for divs of a certain type
local function setValue(targetclasses, depth)
local test = {}
for _, d in pairs(divs) do
for j, divtype in pairs(targetclasses) do
if (divtype == '-') then
if #validDivs > 0 then
divs = wikidata.addVals(divs, query, 0, 1, country)
end
if divs[#divs] == country then
return nil
end
return divs[#divs]
end
if wikidata.isInstance(divtype, d, 3) then
-- restrain list to new value, will be expanded only if needed
divs = {d}
return d
end
end
end
if depth >= 0 then
local num = #divs
divs = wikidata.addVals(divs, query, 0, 10, country)
if #divs > num then return setValue(targetclasses, depth) end
end
end
 
-- solution2: looks for divs that are part of a closed list (way more efficient for big items)
local function findInList(list, depth)
for i, j in pairs(divs) do
for k, l in pairs(list) do
if j == l then
divs = {l}
return l
end
end
end
if depth >= 0 then
local num = #divs
divs = wikidata.addVals(divs, query, 0, 10, country)
if #divs > num then return findInList(list, depth) end
end
end
local displayformat = formats[country] or formats.default
local maxdepth = 3
if not divs then
return nil
return nil
end
end
local vals = {}
 
for i, j in pairs(claim.qualifiers.P670) do
if displayformat.div1 then
table.insert(vals, wikidata.formatSnak(j))
local val = setValue(displayformat.div1, maxdepth)
if val and val ~= validDivs[#validDivs] then
table.insert(validDivs, val)
end
end
 
if displayformat.div2 then
local val = setValue(displayformat.div2, maxdepth)
if val and val ~= validDivs[#validDivs] then
table.insert(validDivs, val)
end
elseif displayformat.div2vals then
local val =  findInList(displayformat.div2vals, 1)
if val and val ~= validDivs[#validDivs] then
table.insert(validDivs, val)
end
end
if displayformat.div3 then
local val = setValue(displayformat.div3, maxdepth)
if val and val ~= validDivs[#validDivs] then
table.insert(validDivs, val)
end
elseif displayformat.div3vals then
local val =  findInList(displayformat.div3vals, 0)
if val and val ~= validDivs[#validDivs] then
table.insert(validDivs, val)
end
end
end
return table.concat(vals)
return validDivs
end
end


local function streetFromClaims(claim)
function p.cityLine(item, country, divlist, postcode) -- line with list of admin divisions + optional postcode
if not claim.mainsnak.snaktype == 'value' then
country = country or getCountry(item)
return nil
local postcode = postcode or wikidata.formatStatements{entity = item, property = 'P281'} or ''
if postcode == '-' then -- can be disabled when it is not useful
postcode = ''
end
local divstr = ''
if (not divlist) or (#divlist == 0) then
divlist = p.adminDivList(item, country)
end
if (not divlist) or (#divlist == 0) then
return -- add a maintenance category ?
end
for i, j in pairs(divlist) do
if string.find(j, '^Q[0-9]+$') then
divlist[i] = wikidata.formatEntity(j)
end
end
end
return wikidata.formatSnak(claim.mainsnak)
local divstr = linguistic.conj(divlist, 'comma')
 
local formatting = formats[country] or formats.default
local str = formatting.cityline or formats.default.cityline
str = str:gsub("$postcode", postcode or '')
str = str:gsub("$admindivs", divstr or '')
return str
end
end


function p.wikidataAddress(item)
function p.fullAddress(item, country, divs, streetstr, divstr, showcountry, postcode)
-- en priorité la propriété P669, datatype: item
-- country id used for formatting
local streetclaims = wikidata.getClaims{item=item, property = 'P669'}
country = country or getCountry(item)
-- sinon, p969: datatype: string
local displayformat = formats[country] or formats.default
if not streetclaims then
 
streetclaims = wikidata.getClaims{item=item, property = 'P669'}
-- line 1 street
local streetline = streetstr or p.streetAddress(item, country)
-- line 2: administrative divisions, postcodes
local cityline = divstr
if not cityline then cityline = p.cityLine(item, country, divs, postcode) end
if (not cityline) or mw.text.trim(cityline) == '' then
cityline = nil
end
end
local streets = {}
 
if streetclaims then
-- line 3: country
for i, j in pairs(streetclaims) do
local countryline, success
local streetname = streetFromClaims(j)
if (showcountry ~= '-') then
if streetname then
countryline, success = countrymodule.standarddisplay(country)
local number = numberFromClaim(j)
if not success then
table.insert(streets, {name = streetname, number = number })
if (type(country) == 'string') and string.find(country, "^Q[0-9]+$") then
countryline = wikidata.formatEntity(country)
else
countryline = country or wikidata.formatStatements{entity = item, property = 'P17'}
end
end
end
end
end
end
local postcode = wikidata.getClaims{item=item, property= 'P281'}
local str = linguistic.conj({streetline, cityline, countryline}, '<br />')
if postcode and #postcode > 1 then -- s'il y a plusieurs codes postaux, il y a sans doute plusieurs adresses différentes, c'est difficile de savoir lequel correspond à laquelle
if str and (not streetstr) and (not divstr) then -- à peu près
postcode = nil
str = str .. '[[Category:Page utilisant une adresse fournie par Wikidata]]'
elseif postcode then
end
postcode = wikidata.formatSnak(postcode[1].mainsnak)
local hasstreetline
if streetline then hasstreetline = true else hasstreetline = false end
 
return str, hasstreetline
end
 
function p.wikidataAddress(item, country) -- fonction de transition
local area = formats[country] or formats.default
local val = p.streetAddress(item, area)
if val then
return val .. '[[Category:Page utilisant une adresse fournie par Wikidata]]'
end
end
-- faaut-il essayer d'ajouter la ville par P131 ? Souvent on en veut pas (par exemple en infobox)
return p.formatAddress(streets, postcode)
end
end
return p
return p

Dernière version du 22 février 2026 à 00:17

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

local formats = require "Module:Adresse/Formats"

local p = {}
local wikidata = require "Module:Wikidata"
local linguistic = require "Module:Linguistique"
local countrymodule = require "Module:Country data"

local function getCountry(item) -- get country id for formatting
	local country = wikidata.getIds(item, {property = "P17"})
	if (not country) then
		return nil
	end
	if #country > 1 then
		return nil
	end
	return country[1] 
end

local function numberFromClaim(claim) -- récupère le numéro de l'immeuble à partir d'un qualificatif P670 d'une affirmation Wikidata
	if not claim.qualifiers or not claim.qualifiers.P670 then
		return nil
	end
	local vals = {}
	for i, j in pairs(claim.qualifiers.P670) do
		table.insert(vals, wikidata.formatSnak(j))
	end
	return table.concat(vals, '-')
end

local function directionFromClaim(claim, area) -- par exemple rue Sherbrooke Ouest
	if not claim.qualifiers or not claim.qualifiers.P560 then
		return nil
	end
	local str = ''
	for i, snak in pairs(claim.qualifiers.P560) do
		local directionlabels = area.directions or formats.default.directions
		str = str .. wikidata.formatSnak(snak,  {speciallabels = directionlabels})
	end
	return str
end

local function streetFromClaims(claim) -- réupère le nom de la rue à partir d'une affirmation P669 Wikidata
	return wikidata.formatStatement(claim)
end

local function formatStreet(streetname, housenumber, direction, displayformat)
	local val = displayformat.streetline or formats.default.streetline
	val = mw.ustring.gsub(val, '$number', housenumber or '')
	val = mw.ustring.gsub(val, '$street', streetname or '')
	val = mw.ustring.gsub(val, '$direction', direction or '')
	return val
end

local function wikidatastreet(claim, area) --formate des données sur la rue à partir d'une affirmation Wikidata
	local streetname = streetFromClaims(claim, area)
	local housenumber =  numberFromClaim(claim, area)
	local direction = directionFromClaim(claim, area)
	return formatStreet(streetname, housenumber, direction, area)
end

function p.streetAddress(item, area) -- formate la ligne concernant la rue et le numéro de rue
	local streets -- châine contenant le ou les rues et numéros d'immeuble
	
	if not area then
		local country = getCountry(item)
		if country and formats[country] then
			area = formats[country]
		else
			area = formats.default
		end
	end
	
	-- essaye de remplir street, en priorité avec P669, type : élément
	local streetclaims = wikidata.getClaims{entity = item, property = 'P669'}
	if streetclaims then
		for i, j in pairs(streetclaims) do
			streetclaims[i] = wikidatastreet(j, area)
		end
		streets = mw.text.listToText(streetclaims)
		streets = wikidata.formatAndCat{value = streets, entity = item, property = 'P669'}
	end
	
	-- sinon : P6375, type : string -- remplace P969
	if not streets then
		streets = wikidata.formatAndCat{linkback = true, property = 'P6375', entity = item, isinlang = 'fr'}
	end
	
	-- sinon : P969, type : string -- propriété obsolète
	if not streets then 
		streets =  wikidata.formatAndCat{linkback = true, property = 'P969', entity = item}
	end
	
	return streets
end

function p.adminDivList(item, country) -- returns a list of admin divisions matching the criteria defined in Module:Adresse/Formats
	country = country or getCountry(item)
	local query = {property = 'P131'}
	local divs = wikidata.transitiveVals(item, query, 0, 10, country)
	local validDivs = {}	
	
	-- solution 1: looks for divs of a certain type
	local function setValue(targetclasses, depth)
		local test = {}
		for _, d in pairs(divs) do
			for j, divtype in pairs(targetclasses) do
				if (divtype == '-') then
					if #validDivs > 0 then
						divs = wikidata.addVals(divs, query, 0, 1, country)
					end
					if divs[#divs] == country then
						return nil
					end
					return divs[#divs]
				end
				if wikidata.isInstance(divtype, d, 3) then
					 -- restrain list to new value, will be expanded only if needed
					divs = {d}
					return d
				end
			end
		end
		if depth >= 0 then
			local num = #divs
			divs = wikidata.addVals(divs, query, 0, 10, country)
			if #divs > num then return setValue(targetclasses, depth) end
		end
	end

	-- solution2: looks for divs that are part of a closed list (way more efficient for big items)
	local function findInList(list, depth) 
		for i, j in pairs(divs) do
			for k, l in pairs(list) do
				if j == l then
					divs = {l}
					return l
				end
			end
		end
		if depth >= 0 then
			local num = #divs
			divs = wikidata.addVals(divs, query, 0, 10, country)
			if #divs > num then return findInList(list, depth) end
		end
	end
					
	local displayformat = formats[country] or formats.default
	local maxdepth = 3
	if not divs then
		return nil
	end

	if displayformat.div1 then
		local val = setValue(displayformat.div1, maxdepth)
		if val and val ~= validDivs[#validDivs] then
			table.insert(validDivs, val)
		end
	end

	if displayformat.div2 then
		local val = setValue(displayformat.div2, maxdepth)
		if val and val ~= validDivs[#validDivs] then
			table.insert(validDivs, val)
		end
	elseif displayformat.div2vals then
		local val =  findInList(displayformat.div2vals, 1)
		if val and val ~= validDivs[#validDivs] then
			table.insert(validDivs, val)
		end
	end
	
	if displayformat.div3 then
		local val = setValue(displayformat.div3, maxdepth)
		if val and val ~= validDivs[#validDivs] then
			table.insert(validDivs, val)
		end
	elseif displayformat.div3vals then
		local val =  findInList(displayformat.div3vals, 0)
		if val and val ~= validDivs[#validDivs] then
			table.insert(validDivs, val)
		end
	end
	
	return validDivs
end

function p.cityLine(item, country, divlist, postcode) -- line with list of admin divisions + optional postcode
	country = country or getCountry(item)
	
	local postcode = postcode or wikidata.formatStatements{entity = item, property = 'P281'} or ''
	if postcode == '-' then -- can be disabled when it is not useful
		postcode = ''
	end
	local divstr = ''
	
	if (not divlist) or (#divlist == 0) then
		divlist = p.adminDivList(item, country)
	end
	if (not divlist) or (#divlist == 0) then
		return -- add a maintenance category ?
	end
	for i, j in pairs(divlist) do
		if string.find(j, '^Q[0-9]+$') then
			divlist[i] = wikidata.formatEntity(j)
		end
	end
	local divstr = linguistic.conj(divlist, 'comma')

	local formatting = formats[country] or formats.default
	local str = formatting.cityline or formats.default.cityline
	str = str:gsub("$postcode", postcode or '')
	str = str:gsub("$admindivs", divstr or '')
	return str
end

function p.fullAddress(item, country, divs, streetstr, divstr, showcountry, postcode)
	 -- country id used for formatting
	country = country or getCountry(item)
	local displayformat = formats[country] or formats.default

	-- line 1 street
	local streetline = streetstr or p.streetAddress(item, country)
	
	-- line 2: administrative divisions, postcodes
	local cityline = divstr
	if not cityline then cityline = p.cityLine(item, country, divs, postcode) end
	if (not cityline) or mw.text.trim(cityline) == '' then
		cityline = nil
	end

	-- line 3: country
	local countryline, success
	if (showcountry ~= '-') then
		countryline, success = countrymodule.standarddisplay(country)
		if not success then
			if (type(country) == 'string') and string.find(country, "^Q[0-9]+$") then
				countryline = wikidata.formatEntity(country)
			else
				countryline = country or wikidata.formatStatements{entity = item, property = 'P17'}
			end
		end
	end
	local str = linguistic.conj({streetline, cityline, countryline}, '<br />')
	if str and (not streetstr) and (not divstr) then -- à peu près
		str = str .. '[[Category:Page utilisant une adresse fournie par Wikidata]]'
	end
	
	local hasstreetline
	if streetline then hasstreetline = true else hasstreetline = false end

	return str, hasstreetline
end

function p.wikidataAddress(item, country) -- fonction de transition
	local area = formats[country] or formats.default
	local val = p.streetAddress(item, area)
	if val then
		return val .. '[[Category:Page utilisant une adresse fournie par Wikidata]]'
	end
end
return p