Module:Matériau : Différence entre versions

De Lagny-sur-Marne Wiki
Aller à : navigation, rechercher
m (speciallabel pour gros oeuvre)
 
(4 révisions intermédiaires par le même utilisateur non affichées)
Ligne 7 : Ligne 7 :
 
Q22731 = '[[Pierre naturelle|pierre]]',
 
Q22731 = '[[Pierre naturelle|pierre]]',
 
Q4259259 = '[[Toile (peinture)|toile]]',
 
Q4259259 = '[[Toile (peinture)|toile]]',
 +
Q2448108 = 'structure', --QID de "gros oeuvre" mais parait un peu bizarre dans des contexte comme "gros oeuvre en acier"
 
}
 
}
  
function p.formatFromItem(item, args)
+
function p.formatFromItem(item, params) -- affiche le matériau en fonction de Property:P186, et de son qualificatif P518 ('s'applique à')
args = args or {}
+
params = params or {}
-- affiche le matériau en fonction de Property:P186, et de son qualificatif P518 ('s'applique à')
+
params.speciallabels = speciallabels
local materials = wikidata.getClaims{entity = item, property = 'P186'}
+
if not materials then
+
local claims = wikidata.getClaims{entity = item, property = 'P186'}
 +
if not claims then
 
return nil
 
return nil
 
end
 
end
  
-- créé une table de chaînes avec les différents matériaux
+
-- crée trois tables : main pour celle sans P518, support pour celle avec qualif "s'applique à "support de peinture" et "parts" pour les autres
local withP518, withoutP518 = {}, {}
+
local main, support, parts = {}, {}, {}
  
local function addStatement(statement) -- ajout d'un chaîne à la table des valeurs
 
-- valeur de base
 
local mainid = wikidata.getmainid(statement)
 
local str = wikidata.formatEntity(mainid, {speciallabels = speciallabels})
 
  
if (not statement.qualifiers) or (not statement.qualifiers.P518) then
+
local function assignClaim(claim) -- met une claims dans la bonne table
table.insert(withoutP518, str)
+
local str = wikidata.formatStatement(claim, params)
return
+
if (not claim.qualifiers) or (not claim.qualifiers.P518) then
end
+
table.insert(main, str)
+
else
for _, qualif in pairs(statement.qualifiers.P518) do
+
for _, val in pairs(claim.qualifiers.P518) do
local key = wikidata.getid(qualif)
+
local key = wikidata.getid(val)
withP518[key] = withP518[key] or {}
+
if key == 'Q861259' then
table.insert(withP518[key], mainid)
+
table.insert(support, str)
 +
else
 +
parts[key] = parts[key] or {}
 +
table.insert(parts[key], str)
 +
end
 +
end
 
end
 
end
 
end
 
end
for i, j in pairs(materials) do
+
addStatement(j)
+
for i, j in pairs(claims) do
end
+
assignClaim(j)
 
 
-- concaténation des différentes tables de valeur
 
local function formatVals(ids, args)
 
local t = {}
 
for i, id in pairs(ids) do
 
table.insert(t, wikidata.formatEntity(id, args))
 
end
 
return linguistic.conj(t)
 
 
end
 
end
 
 
--- valeurs sans qualificatifs
+
-- transforme en chaîne la table des valeurs sans qualificatif
local mainstr = linguistic.conj(withoutP518)
+
mainstr = linguistic.conj(main)
 
 
--- valeurs avec P518 utilisant la préposition "sur"
 
local supportstr
 
if withP518['Q861259'] then
 
supportstr = ' sur ' .. formatVals(withP518['Q861259'])
 
withP518['Q861259'] = nil
 
end
 
 
 
if mainstr or supportstr then
+
-- ajoute le support de peinture (sur toile)
mainstr = (mainstr or '').. (supportstr or '')
+
local supportstr = linguistic.conj(support)
end
+
mainstr = linguistic.conj({mainstr, supportstr}, ' sur ')
 
 
--- autres qualificatifs : préposition "en"
+
-- chaîne des autres valeurs avec qualifs: [piédestal] = {marbre, bronze} => 'piédestal en marbre et bronze'
local withpartstr
+
local formattedparts = {}
local strtable = {}
+
for part, materials in pairs(parts) do
for part, materials in pairs(withP518) do
+
local str = wikidata.formatEntity(part, {link = '-', speciallabels = speciallabels}) .. ' en ' .. linguistic.conj(materials)
local str = wikidata.formatEntity(part, {link = '-'})
+
table.insert(formattedparts, str)
str = str .. ' en ' .. formatVals(materials)
 
table.insert(strtable, str)
 
 
end
 
end
withpartstr = linguistic.conj(strtable, 'comma')
+
local partsStr = linguistic.conj(formattedparts, 'comma')
 
 
-- concaténation finale
+
mainstr = linguistic.conj({mainstr, partsStr}, 'comma')
mainstr = linguistic.conj({mainstr, withpartstr}, 'comma')
 
  
return mainstr
+
if (params.linkback ~= '-') then
 +
mainstr = wikidata.addLinkback(mainstr, item, 'P186')
 +
end
 +
return mainstr .. wikidata.addtrackingcat('P186')
 
end
 
end
 +
 
return p
 
return p

Version actuelle datée du 27 août 2015 à 16:46

La documentation pour ce module peut être créée à Module:Matériau/doc

local p = {}
local wikidata = require 'Module:Interface Wikidata'.fromLua
local linguistic = require 'Module:Linguistique'

local speciallabels = { -- libellés définis localement, qui priment sur ceux de Wikidata
	Q296955 = '[[Peinture à l\'huile|huile]]',
	Q22731 = '[[Pierre naturelle|pierre]]',
	Q4259259 = '[[Toile (peinture)|toile]]',
	Q2448108 = 'structure', --QID de "gros oeuvre" mais parait un peu bizarre dans des contexte comme "gros oeuvre en acier"
}

function p.formatFromItem(item, params)	-- affiche le matériau en fonction de Property:P186, et de son qualificatif P518 ('s'applique à')
	params = params or {}
	params.speciallabels = speciallabels
	
	local claims = wikidata.getClaims{entity = item, property = 'P186'}
	if not claims then
		return nil
	end

	-- crée trois tables : main pour celle sans P518, support pour celle avec qualif "s'applique à "support de peinture" et "parts" pour les autres
	local main, support, parts = {}, {}, {}


	local function assignClaim(claim) -- met une claims dans la bonne table
		local str = wikidata.formatStatement(claim, params)
		if (not claim.qualifiers) or (not claim.qualifiers.P518) then
			table.insert(main, str)
		else
			for _, val in pairs(claim.qualifiers.P518) do
				local key = wikidata.getid(val)
				if key == 'Q861259' then
				table.insert(support, str)
				else
					parts[key] = parts[key] or {}
					table.insert(parts[key], str)
				end
			end	
		end
	end
	
	for i, j in pairs(claims) do
		assignClaim(j)
	end
	
	-- transforme en chaîne la table des valeurs sans qualificatif
	mainstr = linguistic.conj(main)
	
	-- ajoute le support de peinture (sur toile)
	local supportstr = linguistic.conj(support)
	mainstr = linguistic.conj({mainstr, supportstr}, ' sur ')
	
	-- chaîne des autres valeurs avec qualifs: [piédestal] = {marbre, bronze} => 'piédestal en marbre et bronze'
	local formattedparts = {}
	for part, materials in pairs(parts) do
		local str = wikidata.formatEntity(part, {link = '-', speciallabels = speciallabels}) .. '  en ' .. linguistic.conj(materials)
		table.insert(formattedparts, str)
	end
	local partsStr = linguistic.conj(formattedparts, 'comma')
	
	mainstr = linguistic.conj({mainstr, partsStr}, 'comma')

	if (params.linkback ~= '-') then
		mainstr = wikidata.addLinkback(mainstr, item, 'P186')
	end
	return  mainstr .. wikidata.addtrackingcat('P186')
end

return p