« Module:Matériau » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
mAucun résumé des modifications
0x010D (discussion | contributions)
correction bug, commentaires
Ligne 1 : Ligne 1 :
local p = {}
local p = {}
local wikidata = require('Module:Wikidata')
local wikidata = require 'Module:Interface Wikidata'.fromLua
local linguistic = require('Module:Linguistique')
local linguistic = require 'Module:Linguistique'


local speciallabels = { -- libellés définis localement, qui priment sur ceux de Wikidata
local speciallabels = { -- libellés définis localement, qui priment sur ceux de Wikidata
Ligne 16 : Ligne 16 :
return nil
return nil
end
end
local vals = {} -- table des valeurs formatées
 
vals.main = {} -- valeurs sans qualficatif P518
-- créé une table de chaînes avec les différents matériaux
local withP518, withoutP518 = {}, {}
 
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
table.insert(withoutP518, str)
return
end
for _, qualif in pairs(statement.qualifiers.P518) do
local key = wikidata.getid(qualif)
withP518[key] = withP518[key] or {}
table.insert(withP518[key], mainid)
end
end
for i, j in pairs(materials) do
for i, j in pairs(materials) do
local val = wikidata.formatSnak(j.mainsnak, {speciallabels = speciallabels})
addStatement(j)
if not j.qualifiers or not j.qualifiers.P518 then
table.insert(vals.main, val)
else for k, qualif in pairs(j.qualifiers.P518) do
local key = wikidata.getid(qualif)
if not vals[key] then
vals[key] = {}
end
table.insert(vals[key], val)
end end
end
end


local mainstr, supportstr, withpartstr -- supportstr pour le support de peintrue, withpartstr, pour celle avec un autre qualificateur P518
-- concaténation des différentes tables de valeur
mainstr = linguistic.conj(vals.main)
--- valeurs sans qualificatifs
vals.main = nil
local mainstr = linguistic.conj(withoutP518)
if vals['Q861259'] then
 
supportstr = ' sur ' .. linguistic.conj(vals['Q861259'])
--- valeurs avec P518 utilisant la préposition "sur"
local supportstr
if withP518['Q861259'] then
local v = withP518['Q861259']
supportstr = ' sur ' .. linguistic.conj(v)
vals['Q861259'] = nil
vals['Q861259'] = nil
end
end
local withpart = {} -- table des données contenant un qualificatif P518, sauf "support de peinture"
for part, materials in pairs(vals) do
table.insert(withpart, wikidata._getLabel(part) .. ' en ' .. linguistic.conj(materials))
end
withpartstr = linguistic.conj(withpart, 'comma')
if mainstr or supportstr then
if mainstr or supportstr then
mainstr = (mainstr or '').. (supportstr or '')
mainstr = (mainstr or '').. (supportstr or '')
end
end
--- autres qualificatifs : préposition "en"
local withpartstr
local strtable = {}
for part, materials in pairs(withP518) do
local partstr = wikidata.getLabel(part)
local v = {}
for i, j in pairs(materials) do
table.insert(v, wikidata.formatEntity(j))
end
local str = partstr .. ' en ' .. linguistic.conj(v)
table.insert(strtable, str)
end
withpartstr = linguistic.conj(strtable, 'comma')
-- concaténation finale
mainstr = linguistic.conj({mainstr, withpartstr}, 'comma')
mainstr = linguistic.conj({mainstr, withpartstr}, 'comma')
if (args.linkback ~= '-') then
 
mainstr = wikidata.addLinkback(mainstr, item, 'P186')
return mainstr
end
return mainstr .. wikidata.addtrackingcat('P186')
end
end
return p
return p

Version du 27 août 2015 à 06:03

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 = 'huile', Q22731 = 'pierre', Q4259259 = 'toile', }

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

-- créé une table de chaînes avec les différents matériaux local withP518, withoutP518 = {}, {}

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 table.insert(withoutP518, str) return end

for _, qualif in pairs(statement.qualifiers.P518) do local key = wikidata.getid(qualif) withP518[key] = withP518[key] or {} table.insert(withP518[key], mainid) end end for i, j in pairs(materials) do addStatement(j) end

-- concaténation des différentes tables de valeur

--- valeurs sans qualificatifs local mainstr = linguistic.conj(withoutP518)

--- valeurs avec P518 utilisant la préposition "sur" local supportstr if withP518['Q861259'] then local v = withP518['Q861259'] supportstr = ' sur ' .. linguistic.conj(v) vals['Q861259'] = nil end if mainstr or supportstr then mainstr = (mainstr or ).. (supportstr or ) end

--- autres qualificatifs : préposition "en" local withpartstr local strtable = {} for part, materials in pairs(withP518) do local partstr = wikidata.getLabel(part) local v = {} for i, j in pairs(materials) do table.insert(v, wikidata.formatEntity(j)) end local str = partstr .. ' en ' .. linguistic.conj(v) table.insert(strtable, str) end withpartstr = linguistic.conj(strtable, 'comma')

-- concaténation finale mainstr = linguistic.conj({mainstr, withpartstr}, 'comma')

return mainstr end return p