Aller au contenu

Module:Affluents

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 19 avril 2020 à 17:21 et modifiée en dernier par Baidax (discuter | contributions) (-commentaires inutiles.). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[créer] [purger]
local linguistic = require 'Module:Linguistique'
local wd = require 'Module:Wikidata'

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

local p = {}

local function getWatercourse(statement)
	local entity = wd.getMainId(statement)
	local label = mw.wikibase.label(entity)
	local name = wd.formatStatement(statement, {showsource = true})
	return {name 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
			if name.default then
				return name.default
			end
		end
	end
	return mw.wikibase.label(qid)
end

local function getCountry(statement)
	local entity = wd.getMainId(statement)
	local country = wd.getClaims({entity = entity, property = 'P17',
		numval=1, atdate = 'today'})
	if country then
		local countryEntity = wd.getMainId(country[1])
		local name = getCountryShortName(countryEntity)
		local nameWithLink = wd.formatEntity(countryEntity,
			{labelformat = function () return name end})
		return {nameWithLink}
	end
end

local function getCoordinates(entity)
	local coord = wd.getClaims({entity = entity, property = 'P625',
		numval=1})
	if not coord then
		return
	end
	
	local snak = coord[1].mainsnak
	if snak.snaktype ~= 'value' then
		return
	end
	local value = snak.datavalue.value
	value.repr = value.latitude .. ', ' .. value.longitude
	return value
end

local function getDefaultTitle(entity)
	local title = 'Affluents'
	local locationName = wd.getLabel(entity)
	if locationName then
		return title .. ' ' .. linguistic.of(locationName) .. '.'
	end
	return title .. '.'
end

local function compareClaimLabels(c1, c2)
	local label1 = mw.ustring.upper(mw.wikibase.label(wd.getMainId(c1)))
	local label2 = mw.ustring.upper(mw.wikibase.label(wd.getMainId(c2)))
	return label1 < label2
end

function p.tableauDesAffluents(frame)
	local args = frame:getParent().args
	
	-- Entité Wikidata
	local entity = wd.getEntity(args.wikidata)
	if not entity then
		error('Pas d\'entité Wikidata pour l\'élément.')
	end
	
	local watercourses = wd.getClaims({entity = entity,
		property = 'P974', rank = 'valid'})
	
	if not watercourses then
		return
	end
	-- Tri par nom d'affluent
	table.sort(watercourses, compareClaimLabels)
	
	local title = args.titre
		or getDefaultTitle(entity)
		
	-- Bouton d'édition Wikidata
	title = wd.addLinkBack(title, entity, 'P974')

	local tab = mw.html.create('table')
		:addClass('wikitable')
		:addClass('centre')
		:addClass('sortable')
		:node('<caption>' .. title .. '</caption>') --titre
	
	local columns = {
		{fn = getWatercourse, colName = 'Ville'},
		{fn = getCountry, colName = 'Pays'}
	}
	
	local data = {}
	for _, statement in ipairs(watercourses) do
		local watercourseData = {}
		for _, column in ipairs(columns) do
			local _, value = pcall(column.fn, statement)
			table.insert(watercourseData, value
				or '')
			if value and not column.hasValues then
				column.hasValues = true
			end
		end
		table.insert(data, watercourseData)
	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)
			tr = tr:node(th:done())
		end
	end
	tab = tab:node(tr:done())
	
end

return p