Module:Utilitaire Wikidata : Différence entre versions

De Lagny-sur-Marne Wiki
Aller à : navigation, rechercher
(bugfix !)
m (A changé le niveau de protection pour « Module:Utilitaire Wikidata » ([Modifier=Autoriser uniquement les utilisateurs autopatrolled] (infini) [Renommer=Autoriser uniquement les administrateurs] (infini)))
 
(10 révisions intermédiaires par 2 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
 
-- Module de requêtes Wikidata de plus haut niveau que Module:Wikidata
 
-- Module de requêtes Wikidata de plus haut niveau que Module:Wikidata
 
local p = {}
 
local p = {}
local wikidata = require "Module:Wikidata"
+
local wd = require "Module:Wikidata"
local linguistic = require "Module:Linguistique"
+
 
local function keydate (event)
+
function p.keydate (event, item)
if type(event) ~= 'string' then
+
if type(event) == 'table' then
return error('event should be a string starting with a P or S, datatype is ' .. type(event))
+
for i, j in pairs(event) do
end
+
local s = p.keydate(j)
if string.sub(event, 1, 1) == 'Q' then -- on demande un élément utilisé dans P:P793 (événement clé)
+
if s then
return  wikidata.getTheDate{property = 'P793', targetvalue = event, addcat = true, entity = item}
+
return s
 +
end
 +
end
 +
elseif type(event) ~= 'string' then
 +
return error('event should be a string starting with a P or S, datatype is ' .. type(event))
 +
elseif string.sub(event, 1, 1) == 'Q' then -- on demande un élément utilisé dans P:P793 (événement clé)
 +
return  wd.getTheDate{property = 'P793', targetvalue = event, addcat = true, entity = item}
 
elseif string.sub(event, 1, 1) == 'P'  then -- on demande une propriété
 
elseif string.sub(event, 1, 1) == 'P'  then -- on demande une propriété
return wikidata.formatAndCat{property = event, entity = item}
+
return wd.formatAndCat{property = event, entity = item}
 
else
 
else
 
return error('event string should start with P or S, is "' .. event .. '"')
 
return error('event string should start with P or S, is "' .. event .. '"')
Ligne 16 : Ligne 22 :
 
end
 
end
  
function p.wikidatadate(prop, item, options) -- formate un statmement de type date en prenant en compte certains qualificatifs
+
function p.maindate(entity)
options = options or {}
 
local vals = wikidata.getClaims{entity = item, property = prop}
 
if not vals then
 
return nil
 
end
 
local newvals = {}
 
for i, val in pairs(vals) do
 
 
--cherche la date avec les qualifs P580/P582
 
local v = wikidata.getFormattedDate(val, options)
 
 
-- puis limite intérieur / supérieur
 
if not v then
 
local start = wikidata.getFormattedQualifiers(val, {'P1319'})
 
local ending = wikidata.getFormattedQualifiers(val, {'P1326'})
 
if start then
 
if ending then
 
v = 'entre ' .. start .. ' et ' .. ending
 
else
 
v = 'après ' .. start
 
end
 
else
 
if ending then
 
v = 'avant ' .. ending
 
end
 
end
 
--if start then
 
-- start = start .. ', au plus tôt'
 
--end
 
--if ending then
 
-- ending = ending .. ', au plus tard'
 
--end
 
--v = linguistic.conj({start, ending}, 'comma')
 
end
 
 
-- sinon, le mainsnak
 
if not v then
 
if val.mainsnak.snaktype ~= 'value' then
 
v = wikidata.formatStatement(val, options)
 
elseif val.mainsnak.datavalue.value.precision > 7 then
 
v = wikidata.formatStatement(val, options)
 
end
 
end
 
 
-- ajouter le qualificatifs "environ"
 
if v and val.qualifiers and val.qualifiers.P1480 then
 
local q = wikidata.getid( val.qualifiers.P1480[1])
 
if q == 'Q5727902' then
 
v = 'vers ' .. v
 
end
 
end
 
if v then
 
table.insert(newvals, v)
 
end
 
end
 
local str = linguistic.conj(newvals, 'or')
 
if str then
 
if options.addcat ~= '-' then
 
str = str .. wikidata.addtrackingcat(prop)
 
end
 
if options.linkback ~= '-' then
 
str = wikidata.addLinkback(str, item, prop)
 
end
 
return str
 
end
 
end
 
 
 
function p.maindate(entity) -- à améliorer en utilisant module:Date complexe comme pour wikidata.getDate
 
 
 
-- essaye P580/P582
 
-- essaye P580/P582
local startpoint = p.wikidatadate('P580', entity, {linkback = "-"})
+
local startpoint = wd.wikidataDate('P580', entity, {linkback = "-"})
local endpoint = p.wikidatadate('P582', entity, {linkback = "-"})
+
local endpoint = wd.wikidataDate('P582', entity, {linkback = "-"})
 
local str
 
local str
 
if startpoint or endpoint then
 
if startpoint or endpoint then
 
str = (startpoint or '') .. ' - ' .. (endpoint or '')
 
str = (startpoint or '') .. ' - ' .. (endpoint or '')
str = wikidata.addLinkback(str, entity, 'P582')
+
str = wd.addLinkback(str, entity, 'P582')
 
end
 
end
  
Ligne 99 : Ligne 36 :
 
end
 
end
 
-- défaut : P585
 
-- défaut : P585
return p.wikidatadate('P585', entity)
+
return wd.wikidataDate('P585', entity)
end
 
 
 
function p.addLinkback(str, entity, property)
 
return wikidata.addLinkback(str, entity, property)
 
end
 
 
 
function p.addtrackingcat(prop, cat)
 
return wikidata.addtrackingcat(prop, cat)
 
end
 
 
 
function p.keydate(events)
 
if type(events) == 'nil' then
 
return nil
 
end
 
if type(events) == 'string' then
 
return keydate(events)
 
elseif type(events) == 'table' then
 
for i, j in pairs(events) do
 
local val = keydate(j)
 
if val then
 
return val
 
end
 
end
 
end
 
end
 
function p.getClaims(args)
 
return wikidata.getClaims(args)
 
end
 
function p._formatStatements(args)
 
return wikidata._formatStatements(args)
 
end
 
function p.formatAndCat(args)
 
return wikidata.formatAndCat(args)
 
 
end
 
end
  
 
return p
 
return p

Version actuelle datée du 17 mai 2017 à 13:46

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

-- Module de requêtes Wikidata de plus haut niveau que Module:Wikidata
local p = {}
local wd = require "Module:Wikidata"

function p.keydate (event, item)
	if type(event) == 'table' then
		for i, j in pairs(event) do
			local s = p.keydate(j)
			if s then
				return s
			end
		end
	elseif type(event) ~= 'string' then
		 return error('event should be a string starting with a P or S, datatype is ' .. type(event))
	elseif string.sub(event, 1, 1) == 'Q' then -- on demande un élément utilisé dans P:P793 (événement clé)
		return  wd.getTheDate{property = 'P793', targetvalue = event, addcat = true, entity = item}
	elseif string.sub(event, 1, 1) == 'P'  then -- on demande une propriété
		return wd.formatAndCat{property = event, entity = item}
	else
		return error('event string should start with P or S, is "' .. event .. '"')
	end
end

function p.maindate(entity)	
	-- essaye P580/P582
	local startpoint = wd.wikidataDate('P580', entity, {linkback = "-"})
	local endpoint = wd.wikidataDate('P582', entity, {linkback = "-"})
	local str
	if startpoint or endpoint then
		str = (startpoint or '') .. ' - ' .. (endpoint or '')
		str = wd.addLinkback(str, entity, 'P582')
	end

	if str then
		return str
	end
	-- défaut : P585
	return wd.wikidataDate('P585', entity)
end

return p