Module:Wikidata/Nommage

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

-- Module Wikidata d’aide à la gestion du nommage des entités, en particulier le nommage des entités à une date donnée
-- Work In Progress version « marche pas »

local wikidata = require "Module:Wikidata"
local modules = {}
modules.formatDate = require "Module:Date complexe"
local fun = require "Module:Luafun"

local PofficialName = "P1448"
local PDateNaissance = "P569"

local p = {}
p.wd = wikidata

-------------------------------------------------------------
-- copied from Module:Wikidata as these are not local functions for now
local function timeFromSnak(snak)
	if snak and snak.snaktype == "value" then
		return snak.datavalue.value.time
	end
end

local function timeFromQualifs(claim, qualifs)
	local claimqualifs = claim.qualifiers
	if not claimqualifs then
		return nil
	end
	for i, qualif in pairs(qualifs or timequalifiers) do
		local vals = claimqualifs[qualif] or {}
		local cand = timeFromSnak(vals[1])
		if cand then return cand end
	end
end

local function atDate(claim, mydate)
	if mydate == "today" then
		mydate = os.date("!%Y-%m-%dT%TZ")
	end	
	mw.log("testdate")
	local newclaims = {}
	local mindate = timeFromQualifs(claim, {'P580'}) 
	local maxdate = timeFromQualifs(claim, {'P582'})
	mw.logObject(mydate)
	if modules.formatDate.before(mydate, mindate) and  modules.formatDate.before(maxdate, mydate) then
		return true
	end
end
-------------------------------------------------------------------------------------------

local function stmtMeetsOneTrueIn(condition, values)
	return function(stmt)
		return values:any(
			function(date) return condition(stmt, date) end
		)
	end
end

local function mainSnak(stmt)
	return stmt.mainsnak
end

p.nameAtDate = function (place_qid, date_claims)
	local names = mw.wikibase.getAllStatements(place_qid, "P1448")
	mw.log(#date_claims)
	local date_vals = fun.iter(date_claims):map(mainSnak):map(timeFromSnak)
	date_vals:each(mw.log)
	
	local atOneOfTheDates = stmtMeetsOneTrueIn(atDate, date_vals)
	
	names = wikidata.filterClaims(
		names, 
		{
			['condition'] = atOneOfTheDates, 
			['isinlang'] = 'ht', 
			['rank'] = 'valid'
		}
	)
	
	return names
end

-- =p.nameAtPropValue("Q192393", "P569" ,"Q2977199")
p.nameAtPropValue = function(toNameEntity, atDateProperty, forItem)
	local dates
	if type(forItem) == "table" then
		dates = forItem:getBestStatements(atDateProperty)
	else
	    dates = mw.wikibase.getBestStatements(forItem, atDateProperty)
	end
	local names = p.nameAtDate(toNameEntity, dates)
	return names
end

--[[test 
=p.wd.formatAndCat(
					{
						entity = "Q2977199", 
						property= 'P19', 
						rank = 'best', 
						conjtype= ' ou ', 
						unknownlabel = "lieu inconnu",
						statementformat = p.nameAtDatePropStatementFormatter("P569", "Q2977199")
					})
--]]

local const_format = function(label)
	return function()
		return label
	end
end

local function modify_parameters(orig, delta)
	local modified = orig
	for k, v in pairs(delta) do
		modified[k] = v
	end
	return modified
end

--[[ function generator (closures) for the « statementformat » parameter of 
     Module:Wikidata to format statements according to the date given by another property
     
     (conj is optional)
     
     When called, the return function will use the values for the property 
     « dateOfProperty » of the reference item to search for the names of an item at those time(s)
--]]
p.nameAtDatePropStatementFormatter = function(dateOfProperty, item, conj)
	conj = conj or " ou "
	return function(stmt, params)
		local snak = stmt.mainsnak
		if snak.snaktype ~= 'value' then
			return nil
		end
		local labels = p.nameAtPropValue(wikidata.getId(snak), "P569", item)
		if labels then
			local label = wikidata.formatStatements{
				claims=labels, conj = conj
			}
			return wikidata.formatEntity(wikidata.getId(snak), modify_parameters(params, {labelformat=const_format(label)}))
		else
			return wikidata.formatEntity(wikidata.getId(snak), params)
		end
	end
end

-- interface for a test template
p.nameForItem = function(frame)
	local item = frame.args["item"]
	local atDateProperty = frame.args["atDateProperty"]
	local toNameEntity = frame.args["toNameEntity"]
	local res = p.nameAtPropValue(toNameEntity, atDateProperty, item)
	if res then
		return wikidata.formatStatements{
			claims=res, conj=" ou "
		}
	else
		return mw.wikibase.label(toNameEntity)
	end
end

return p