Aller au contenu

Module:Jumelages

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 23 mars 2019 à 23:20 et modifiée en dernier par Alt0160 (discuter | contributions) (Bouton d'édition Wikidata). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[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.
local contries = require 'Module:Country data'
local image = require 'Module:Image'
local linguistic = require 'Module:Linguistique'
local wikidata = require 'Module:Wikidata'

local p = {}

-- TODO: Fix sort
-- TODO: atdate
local function getPlaceNameWithFlag(statement, atdate)
	local name = wikidata.formatStatement(statement)
	local flag = ''
	local entity = wikidata.getMainId(statement)
	local flagOrCOA = wikidata.getClaims({entity = entity,
		property = {'P41', 'P94'}, numval=1, atdate = atdate})  -- drapeau ou blason
	if flagOrCOA then
		mediaName = wikidata.formatStatement(flagOrCOA[1])
		if mediaName then
			flag = tostring(mw.html.create('span')
				:node(image.image({image = mediaName, taille = '20x15',
					border = 1, class = 'noviewer'}))
				:done())
		end
	end
	return {flag, name}
end

local function getCity(statement)
	local nameWithFlag = getPlaceNameWithFlag(statement, 'today')
	local references = wikidata.getReferences(statement)
	-- TODO: Ajouter le drapeau de la ville
	return {nameWithFlag[1],
		nameWithFlag[2] .. (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
		return getPlaceNameWithFlag(country[1], 'today')
	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 addMarker(frame, shapes, entity, coordinates, symbol, color,
		lineSource)
	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
	local marker = frame:expandTemplate{title = 'Carte interactive/Marqueur',
		args = {[1] = coordinates[1] .. ', ' .. coordinates[2],
			title = locationName, symbol = symbol, couleur = color}}
	table.insert(shapes, marker)
	if not lineSource then
		return
	end
	local line = frame:expandTemplate{title = 'Carte interactive/Ligne',
		args = {[1] = coordinates[1] .. ', ' .. coordinates[2],
			[2] = lineSource[1] .. ', ' .. lineSource[2], couleur = '#00bb00',
			opacity = .35
		}}
	table.insert(shapes, line)
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)
		
	-- Bouton d'édition wikidata
	title = wikidata.addLinkBack(title, entity, 'P190')

	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', withFlag = 1},
		{fn = getCountry, colName = 'Pays', withFlag = 1},
    	{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
		addMarker(frame, shapes, entity, mainCoordinates, 'star',
			'#00bb00')
	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)
		addMarker(frame, shapes, twinQid, nil, nil, nil, mainCoordinates)
	end

	local tr = mw.html.create('tr')
	for _, column in pairs(columns) do
		if column.hasValues then
			local th = mw.html.create('th')
				:attr('scope', 'col')
				:wikitext(column.colName)
			if column.withFlag then
				th = th:attr('colspan', 2)
			end
			tr = tr:node(th: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
				local value = cityData[j]
				if column.withFlag then
					value = value or {'', ''}
					tr = tr:node(mw.html.create('td')
						:css('border-right', 0)
						:wikitext(value[1]):done())
					tr = tr:node(mw.html.create('td')
						:css('border-left', 0)
						:wikitext(value[2]):done())
				else
					tr = tr:node(mw.html.create('td'):wikitext(value):done())
				end
			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