Modul:WikidataUtils/Time/Interval

Implementează {{Interval de timp de la Wikidata}}


local Wikidata = require('Modul:Wikidata')
local getArgs = require('Modul:Arguments').getArgs
local DateUtils = require('Modul:DateUtils')
local p = {}
local endash = mw.text.decode('–', true)

local printDateFromSnak = function(snak, dateFormat)
	return DateUtils.formatDate(DateUtils.extractDateFromWikidataSnak(snak), false, false, dateFormat)
end

p.getMomentsFromClaim = function(claim, startProp, endProp, instantProp, dateFormat)
	local start = {prop = startProp or 'P580'}
	local endd = {prop = endProp or 'P582'}
	local instant = {prop = instantProp or 'P585'}
	local moments = {start, endd, instant}

	if Wikidata.isClaimTrue(claim) then
		if claim.qualifiers then
			for __,eachMoment in ipairs(moments) do
				if claim.qualifiers[eachMoment.prop] then for ___,eachQual in ipairs(claim.qualifiers[eachMoment.prop]) do
					if Wikidata.isValueSnak(eachQual) then
						eachMoment.snak = eachQual
						break
					end
				end end
			end
		end
	end
	return moments
end

p.printMoment = function(moments, dateFormat)
	local start, endd, instant = moments[1], moments[2], moments[3]
	if start.snak and endd.snak then
		return printDateFromSnak(start.snak, dateFormat) .. endash .. printDateFromSnak(endd.snak, dateFormat), property or start.prop
	elseif start.snak then
		return printDateFromSnak(start.snak, dateFormat) .. endash .. "''prezent''", property or start.prop
	elseif endd.snak then
		return 'până la ' .. printDateFromSnak(endd.snak, dateFormat), property or endd.prop
	elseif instant.snak then
		return printDateFromSnak(instant.snak, dateFormat), property or instant.prop
	end
	return nil
end

p.printTimestampForClaim = function(claim, dateFormat, startProp, endProp, instantProp)
	local moments = p.getMomentsFromClaim(claim, startProp, endProp, instantProp, dateFormat)
	return p.printMoment(moments)
end

p.fromArgs = function(property, q, startProp, endProp, instantProp, dateFormat)
	local start = {prop = startProp or 'P580'}
	local endd = {prop = endProp or 'P582'}
	local instant = {prop = instantProp or 'P585'}
	local moments = {start, endd, instant}
	if property then 
		local claims = Wikidata.findBestClaimsForProperty(q, property)
		if claims then
			for _,eachClaim in ipairs(claims) do
				moments = p.getMomentsFromClaim(eachClaim, startProp, endProp, instantProp, dateFormat)
			end
		end
	else
		for _,eachMoment in ipairs(moments) do
			local momentClaims = Wikidata.findBestClaimsForProperty(q, eachMoment.prop)
			if momentClaims then for _,eachMomentClaim in ipairs(momentClaims) do
				if Wikidata.hasValueSnak(eachMomentClaim) and Wikidata.isClaimTrue(eachMomentClaim) then
					eachMoment.snak = eachMomentClaim.mainsnak
				end
			end end
		end
	end
	return p.printMoment(moments, dateFormat)
end

p.fromArray = function(array)
	local q = array.q or mw.wikibase.getEntityIdForCurrentPage()
	local property = array.p
	local startProp = array[1] or 'P580'
	local endProp = array[2] or 'P582'
	local instantProp = array[3] or 'P585'
	local dateFormat = array.dateFormat
	
	local out, actualProperty = p.fromArgs(property, q, startProp, endProp, instantProp, dateFormat)
	return out, actualProperty
end

p.fromFrame = function(frame)
	local args = getArgs(frame)
	local out = p.fromArray(args)
	return out
end
return p