Modul:BirthDateAndAge

De la Wikipedia, enciclopedia liberă
Jump to navigation Jump to search

Documentația acestui modul poate fi creată la Modul:BirthDateAndAge/doc

local p = {}
local getArgs = require('Modul:Arguments').getArgs
local wikidata = require('Modul:Wikidata')
local plural = require('Modul:Plural').get_plural
local GregorianDate = require('Modul:GregorianDate')

local computeYearsPastBetween = function(inMomentDate, inReferenceDate)
	local momentDate = GregorianDate.julianToGregorian(inMomentDate)
	local referenceDate = GregorianDate.julianToGregorian(inReferenceDate)
	local yearsDiff = tonumber(momentDate.year) - tonumber(referenceDate.year)
	if tonumber(momentDate.month) < tonumber(referenceDate.month) or (tonumber(momentDate.month) == tonumber(referenceDate.month) and tonumber(momentDate.day) < tonumber(referenceDate.day)) then
		yearsDiff = yearsDiff - 1
	end
	return yearsDiff
end

local computeYearsPastSince = function(referenceDate) 
	return computeYearsPastBetween(os.date("*t"), referenceDate)
end

p._getBdaByWikidata = function(args) 
	local link = false
	if args['link'] and mw.text.trim(args['link']) ~= '' then
		link = true
	end
	local birthDates = wikidata.findDateValues('P569', nil)
	local deathDates = wikidata.findDateValues('P570', nil)
	if birthDates and birthDates[1] and birthDates[1].claim and birthDates[1].claim.type == 'statement' and birthDates[1].claim.mainsnak.snaktype == 'value' then
		local out = GregorianDate.displayDualDateIfInInterval(birthDates[1], link)
		if not deathDates or not deathDates[1] or (deathDates[1].claim and deathDates[1].claim.mainsnak.snaktype == 'novalue') then
			age = computeYearsPastSince(birthDates[1])
			if age < 120 and birthDates[1].precision > 7 then
				out = out .. ' (' .. plural{args={age, 'an', 'ani', 'de ani'}} .. ')'
			else
				out = out .. '[[Categorie:Decese cu dată necunoscută]]'
			end
		end
		if birthDates[1].claim then
			out = out .. wikidata.outputReferences(birthDates[1].claim)
		end
		return out
	end
	return ''
end

p.getBdaByWikidata = function(frame) 
	local args = getArgs(frame)
	return p._getBdaByWikidata(args)
end

p.getBda = function(frame) 
	local args = getArgs(frame)
	local link = false
	if args['link'] and mw.text.trim(args['link']) ~= '' then
		link = true
	end
	local d = {}
	if args[1] then
		d.year = tonumber(args[1])
		d.month = tonumber(args[2] or '1')
		d.day = tonumber(args[3] or '1')
		d.precision = 9
		d.calendar = mw.ustring.lower(args['calendar_birth'] or args['calendar'] or 'gregorian')
		if args[2] then d.precision = 10 end
		if args[3] then d.precision = 11 end
		out = GregorianDate.displayDualDateIfInInterval(d, link)
		if d.precision > 7 then
			out = out .. ' (' .. plural{args={computeYearsPastSince(d), 'an', 'ani', 'de ani'}} .. ')'
		end
		return  out
	else
		return p.getBdaByWikidata(frame)
	end
end

p._getDdaByWikidata = function(args) 
	local birthDates = wikidata.findDateValues('P569', nil)
	local deathDates = wikidata.findDateValues('P570', nil)
	local link = false
	if args['link'] and mw.text.trim(args['link']) ~= '' then
		link = true
	end
	if deathDates and deathDates[1] and (not deathDates[1].claim or deathDates[1].claim.mainsnak.snaktype == 'value') then
		local out = ''
		out = out .. GregorianDate.displayDualDateIfInInterval(deathDates[1], link)
		if birthDates and birthDates[1] and (not birthDates[1].claim or birthDates[1].claim.mainsnak.snaktype == 'value') then
			if deathDates[1].precision > 7 and birthDates[1].precision > 7 then
				out = out .. ' (' .. plural{args={computeYearsPastBetween(deathDates[1], birthDates[1]), 'an', 'ani', 'de ani'}} .. ')'
			end
		end
		if deathDates[1].claim then
			out = out .. wikidata.outputReferences(deathDates[1].claim)
		end
		return out
	end
	return ''
end

p.getDdaByWikidata = function(frame) 
	local args = getArgs(frame)
	return p._getDdaByWikidata(args)
end
p.getDda = function(frame)
	local args = getArgs(frame)
	local link = false
	if args['link'] and mw.text.trim(args['link']) ~= '' then
		link = true
	end
	if args[1] then
		local d = {}
		d.year = tonumber(args[1])
		d.month = tonumber(args[2] or '1')
		d.day = tonumber(args[3] or '1')
		d.precision = 9
		d.calendar = mw.ustring.lower(args['calendar_death'] or 'gregorian')
		if args[2] then d.precision = 10 end
		if args[3] then d.precision = 11 end
		local b = {}
		b.year = tonumber(args[4])
		b.month = tonumber(args[5] or '1')
		b.day = tonumber(args[6] or '1')
		b.precision = 9
		b.calendar = mw.ustring.lower(args['calendar_birth'] or 'gregorian')
		if args[5] then b.precision = 10 end
		if args[6] then b.precision = 11 end
		return GregorianDate.displayDualDateIfInInterval(d, link) .. ' (' ..  plural{args={computeYearsPastBetween(d, b), 'an', 'ani', 'de ani'}} .. ')'
	else
		return p.getDdaByWikidata(frame)
	end
end

return p