Aller au contenu

Module:Tableau qualificateurs

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 24 octobre 2015 à 16:27 et modifiée en dernier par Tpt (discuter | contributions) (affichage de toutes les valeurs pour le même qualifier). 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 contenant le code de Modèle:Tableau qualificateurs.

local wikidata = require 'Module:Wikidata'
local recup = require 'Module:Wikidata/Récup'

local p = {}


function propertyLabel(propertyId, options)
	local i = 0
	while options[propertyId] ~= nil and i < 10 do
		propertyId = options[propertyId]
		i = i + 1
	end
	if string.match(propertyId, 'P%d+') then
		return mw.wikibase.label(propertyId)
	else
		return propertyId
	end
end

function claimsToArray(claims, options)
	local headers = {}
	local rows = {{}}
	local newCol = 1

	for i, claim in pairs(claims) do
		rows[i + 1] = {}

		property = propertyLabel(claim.mainsnak.property, options)
		if headers[property] == nil then
			headers[property] = newCol
			newCol = newCol + 1
		end
		rows[i + 1][headers[property]] = wikidata.formatSnak(claim.mainsnak, {})

		if claim.qualifiers ~= nil then
			for _, qs in pairs(claim.qualifiers) do
				for _, qualifier in pairs(qs) do
					property = propertyLabel(qualifier.property, options)
					if headers[property] == nil then
						headers[property] = newCol
						newCol = newCol + 1
					end
					if rows[i + 1][headers[property]] ~= nil then
						rows[i + 1][headers[property]] = rows[i + 1][headers[property]] .. '<br />' .. wikidata.formatSnak(qualifier, {})
					else
						rows[i + 1][headers[property]] = wikidata.formatSnak(qualifier, {})
					end
				end
			end
		end
	end

	for property, i in pairs(headers) do
		rows[1][i] = property
	end

	return rows
end

function formatArray(rows)
	local str = '{| class="wikitable sortable"'

	for i,row in pairs(rows) do
		str = str .. '\n|-'

		for j,_ in pairs(rows[1]) do
			if row[j] == nil then
				str = str .. '\n|'
			else
				if i == 1 or j == 1 then
					str = str .. '\n!'
				else
					str = str .. '\n|'
				end
				str = str .. ' ' .. row[j]
			end
		end
	end

	return str .. '\n|}\n'
end

function p.tableau(frame)
	local args = frame:getParent().args
	local claims = recup.getClaims(args)
	local rows = claimsToArray(claims, args)
	return formatArray(rows)
end

return p