Module:Utilitaire Wikidata

De Lagny-sur-Marne Wiki
Révision datée du 20 mars 2016 à 22:28 par Zolo (discussion) (fonction transférée vers Module:WIkidata)
Aller à : navigation, rechercher

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 wikidata = require "Module:Wikidata"
local linguistic = require "Module:Linguistique"
local formatDate = require "Module:Date complexe"

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

function p.maindate(entity) -- à améliorer en utilisant module:Date complexe comme pour wikidata.getDate
	
	-- essaye P580/P582
	local startpoint = p.wikidatadate('P580', entity, {linkback = "-"})
	local endpoint = p.wikidatadate('P582', entity, {linkback = "-"})
	local str
	if startpoint or endpoint then
		str = (startpoint or '') .. ' - ' .. (endpoint or '')
		str = wikidata.addLinkback(str, entity, 'P582')
	end

	if str then
		return str
	end
	-- défaut : P585
	return p.wikidatadate('P585', entity)
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

return p