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 25 mars 2019 à 05:11 et modifiée en dernier par Alt0160 (discuter | contributions) (local). 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 image = require 'Module:Image'
local linguistic = require 'Module:Linguistique'
local wikidata = require 'Module:Wikidata'

local gdata = mw.loadData('Module:Country data/liste')

local p = {}

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

local function formatEntityWithForcedLabel(entity, label)
	return wikidata.formatEntity(entity,
		{labelformat = function () return label end})
end

local function getCity(statement)
	local entity = wikidata.getMainId(statement)
	local label = wikidata.getLabel(entity)
	local name = formatEntityWithForcedLabel(entity, label)
	local flag = getFlag(entity, 'today', label)
	local references = wikidata.getReferences(statement)
	return {flag, (name or '') .. (wikidata.sourceStr(references) or '')}
end

local function getCountryShortName(qid)
	-- On utilise Country data, qui permet d'obtenir "Chine" au lieu de
	-- "République de Chine" par exemple
	local countryKey = gdata[string.lower(qid)]
	if countryKey then
		local countryTable = require('Module:Country data/' .. countryKey)
		local name = countryTable and countryTable.name
		if name then
			if type(name) == 'string' then
				return name
			end
			-- TODO: Period, utiliser bestfordate de Country data
			if name.default then
				return name.default
			end
		end
	end
	return wikidata.getLabel(qid)
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 countryEntity = wikidata.getMainId(country[1])
		local name = getCountryShortName(countryEntity)
		local nameWithLink = formatEntityWithForcedLabel(entity, name)
		local flag = getFlag(countryEntity, 'today', name)
		return {flag, nameWithLink}
	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

local function compareClaimLabels(c1, c2)
	local label1 = mw.ustring.upper(wikidata.getLabel(wikidata.getMainId(c1)))
	local label2 = mw.ustring.upper(wikidata.getLabel(wikidata.getMainId(c2)))
	return label1 < label2
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 showMap = not templateArgs.carte
		or string.lower(templateArgs.carte) ~= 'non'
	
	local twinCities = wikidata.getClaims({entity = entity,
		property = 'P190', rank = 'valid'})
	
	if not twinCities then
		return
	end
	-- Tri par nom de ville
	table.sort(twinCities, compareClaimLabels)
	
	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 data = {}
	for _, statement in ipairs(twinCities) do
		local cityData = {}
		for _, column in ipairs(columns) do
			local _, value = pcall(column.fn, statement)
			table.insert(cityData, value
				or (column.withFlag and {'', ''})
				or '')
			if value and not column.hasValues then
				column.hasValues = true
			end
		end
		table.insert(data, cityData)
	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
					tr = tr:node(mw.html.create('td')
						:css('border-right', 0)
						:wikitext(value[1] or ''):done())
					tr = tr:node(mw.html.create('td')
						:css('border-left', 0)
						:wikitext(value[2] or ''):done())
				else
					tr = tr:node(mw.html.create('td')
						:wikitext(value or ''):done())
				end
			end
		end
		tab = tab:node(tr:done())
	end
	
	tab = tostring(tab:done())
	
	if not showMap then
		return tab
	end
	
	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
	
	for _, statement in ipairs(twinCities) do
		local twinQid = wikidata.getMainId(statement)
		addMarker(frame, shapes, twinQid, nil, nil, nil, mainCoordinates)
	end
	
	interactiveMapArgs.formes = table.concat(shapes, '\n')

	local interactiveMap = frame:expandTemplate{title = 'Carte interactive',
		args = interactiveMapArgs} 
	
	return interactiveMap .. tab
end

return p