Module:Jumelages
Apparence
[voir] [modifier] [historique] [purger]
Module utilisé par le modèle {{Jumelages}}.
Utilisation
Fonctions exportables :
tableauDesJumelages(frame)
– Permet d'obtenir la liste des jumelages d'une ville / d'un village, ainsi qu'une carte interactive des villes jumelées.
La documentation de ce module est générée par le modèle {{Documentation module}}.
Elle est incluse depuis sa sous-page de documentation. Veuillez placer les catégories sur cette page-là.
Les éditeurs peuvent travailler dans le bac à sable (créer).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
local contries = require 'Module:Country data'
local linguistic = require 'Module:Linguistique'
local wikidata = require 'Module:Wikidata'
local p = {}
local function getCity(statement)
local name = wikidata.formatSnak(statement.mainsnak) or ''
local references = wikidata.getReferences(statement)
-- TODO: Ajouter le drapeau de la ville
return name .. (wikidata.sourceStr(references) or '')
end
local function getCountry(statement)
local entity = wikidata.getMainId(statement)
-- TODO: changer atdate pour les jumelages terminés
local country = wikidata.getClaims({entity = entity, property = 'P17',
numval=1, atdate = 'today'})
if country then
local qid = wikidata.getMainId(country[1])
-- TODO personnaliser args.date pour contries.standarddisplay
-- TODO fix sort
return contries.standarddisplay(qid, {})
or wikidata.formatStatement(country[1])
end
end
local function getCoordinates(entity)
local coordinates = wikidata.getClaims({entity = entity, property = 'P625',
numval=1})
if not coordinates then
return
end
local snak = coordinates[1].mainsnak
if snak.snaktype ~= 'value' then
return
end
local value = snak.datavalue.value
return {value.latitude, value.longitude}
end
local function getPeriod(statement)
return wikidata.getFormattedDate(statement)
end
local function getDefaultTitle(entity)
local title = 'Jumelages et partenariats'
local locationName = wikidata.getLabel(entity)
if locationName then
return title .. ' ' .. linguistic.of(locationName) .. '.'
end
return title .. '.'
end
local function getMarker(frame, entity, coordinates, symbol, color)
local coordinates = coordinates or getCoordinates(entity)
if not coordinates then
return
end
local locationName = wikidata.getLabel(entity)
if not symbol and locationName then
symbol = string.lower(string.sub(locationName, 1, 1))
end
return frame:expandTemplate{title = 'Carte interactive/Marqueur', args = {
[1] = coordinates[1] .. ', ' .. coordinates[2], title = locationName,
symbol = symbol, couleur = color
}}
end
function p.tableauDesJumelages(frame)
local templateArgs = frame:getParent().args
-- Entité Wikidata
local entity = wikidata.getEntity(templateArgs.wikidata)
if not entity then
return mw.html.create('span')
:addClass('error')
:wikitext('Pas d\'entité Wikidata pour l\'élément.')
:done()
end
local twinCities = wikidata.getClaims({entity = entity,
property = 'P190', sorttype = 'chronological', rank = 'valid'})
if not twinCities then
return
end
local title = templateArgs.titre
or getDefaultTitle(entity)
local tab = mw.html.create('table')
:addClass('wikitable')
:addClass('centre')
:addClass('sortable')
tab = tab:node(mw.html.create('caption')
:wikitext(title)
:done()) --titre
local columns = {
{fn = getCity, colName = 'Ville'},
{fn = getCountry, colName = 'Pays'},
{fn = getPeriod, colName = 'Période'}
}
local interactiveMapArgs = {latitude = 0, longitude = 0,
zoom = templateArgs.zoom or 0,
largeur = templateArgs['largeur carte'] or 420,
hauteur = templateArgs['hauteur carte'] or 200,
texte = title
}
local shapes = {}
local mainCoordinates = getCoordinates(entity)
if mainCoordinates then
if interactiveMapArgs.zoom ~= 0 then
interactiveMapArgs.latitude = mainCoordinates[1]
interactiveMapArgs.longitude = mainCoordinates[2]
end
local marker = getMarker(frame, entity, mainCoordinates, 'star',
'#00bb00')
table.insert(shapes, marker)
end
local data = {}
for _, statement in ipairs(twinCities) do
local cityData = {}
local twinQid = wikidata.getMainId(statement)
for _, column in ipairs(columns) do
local value = column.fn(statement)
table.insert(cityData, value)
if value and not column.hasValues then
column.hasValues = true
end
end
table.insert(data, cityData)
local marker = getMarker(frame, twinQid)
if marker then
table.insert(shapes, marker)
end
end
local tr = mw.html.create('tr')
for _, column in pairs(columns) do
if column.hasValues then
tr = tr:node(mw.html.create('th')
:attr('scope', 'col')
:wikitext(column.colName)
:done())
end
end
tab = tab:node(tr:done())
for _, cityData in ipairs(data) do
local tr = mw.html.create('tr')
for j, column in pairs(columns) do
if column.hasValues then
tr = tr:node(mw.html.create('td')
:wikitext(cityData[j]))
end
end
tab = tab:node(tr:done())
end
interactiveMapArgs.formes = table.concat(shapes, '\n')
local interactiveMap = frame:expandTemplate{title = 'Carte interactive',
args = interactiveMapArgs}
return interactiveMap .. tostring(tab:done())
end
return p