Modul:Biodata

De la Wikipedia, enciclopedia liberă

Afișează datele biografice elementare (data și locul nașterii, data și locul decesului) unei persoane. Dacă nu sunt prezente, ele se extrag de la Wikidata.


local p = {}
local getArgs = require('Modul:Arguments').getArgs
local wikidata = require('Modul:Wikidata')
local lc = require('Modul:LocationAndCountry')
local join = require('Modul:Separated entries')._main
local StringUtils = require('Modul:StringUtils')
local GregorianDate = require('Modul:GregorianDate')
local DateUtils = require('Modul:DateUtils')

local function processDateDateRefAndPlace(iDate, iDateRefs, iPlace, prefix, refsEnabled)
	local out = ''
	if not StringUtils.__isEmpty(iDate) or not StringUtils.__isEmpty(iPlaced) then
		out = out .. prefix
	end
	if not StringUtils.__isEmpty(iDate) then
		out = out .. iDate
		if StringUtils.__isEmpty(iPlace) and not StringUtils.__isEmpty(iDateRefs) then
			out = out .. iDateRefs
		end
	end
	if not StringUtils.__isEmpty(iPlace) then
		out = out .. ',' .. (refsEnabled and not StringUtils.__isEmpty(iDateRefs) and iDateRefs or '') .. ' ' .. iPlace
	end
	if out == '' then return nil end
	return out
end

p.fromParams = function(birthDate, birthDateRefs, birthPlace, deathDate, deathDateRefs, deathPlace, deathIntro, refsEnabled)
	local birthInfo = processDateDateRefAndPlace(birthDate, birthDateRefs, birthPlace, 'n. ', refsEnabled)
	local deathInfo = processDateDateRefAndPlace(deathDate, deathDateRefs, deathPlace, deathIntro or 'd. ', refsEnabled)
	return join({birthInfo, deathInfo, separator = mw.text.decode(' – ', true)})
end

p.fromArray = function(args)
	local birthDate = args[1]
	local birthDateRefs = nil
	local wikidataBDate = nil
	local birthTs = nil
	local deathTs = nil
	local q = args['q']
	local refsEnabled = args['ref'] == 'y' or args['ref'] == 'd' or args['ref'] == 'yes' or args['ref'] == 'da'
	
	if not args[1] then
		wikidataBDate = wikidata.findDateValues('P569', q)
		if wikidataBDate and wikidataBDate[1] then
			birthDate = GregorianDate.displayDualDateIfInInterval(wikidataBDate[1], true)
			birthDateRefs = wikidata.outputReferences(wikidataBDate[1].claim)
			birthTs = DateUtils.parseWikidataDate(wikidataBDate[1].claim.mainsnak.datavalue.value.time, wikidataBDate[1].claim.mainsnak.datavalue.value.precision)
		else
			birthDate = "[[Categorie:Nașteri cu dată necunoscută]] ?"
		end
	end

	local birthPlace = args[2] or lc.displayFromParams('P19', q, birthTs or 'P569', 1, nil, refsEnabled)
	local deathDate = args[3]
	local deathIntro = 'd. '
	if not args[3] then
		local wikidataDDate = wikidata.findDateValues('P570', q)
		if wikidataDDate then
			for _,eachDateCandidate in ipairs(wikidataDDate) do
				if eachDateCandidate and eachDateCandidate.claim.mainsnak.snaktype == 'value' then
					deathDate = GregorianDate.displayDualDateIfInInterval(eachDateCandidate, true)
					deathDateRefs = wikidata.outputReferences(eachDateCandidate.claim)
					deathTs = DateUtils.parseWikidataDate(eachDateCandidate.claim.mainsnak.datavalue.value.time, eachDateCandidate.claim.mainsnak.datavalue.value.precision)
					break
				elseif eachDateCandidate.claim.mainsnak.snaktype == 'somevalue' then
					deathDate = "[[Categorie:Decese cu dată necunoscută]] ?"
					break
				end
			end
		end
		if not deathDate then
			local wikidataDisappDate = wikidata.findDateValues('P746', q)
			if wikidataDisappDate then
				for _,eachDateCandidate in ipairs(wikidataDisappDate) do
					if eachDateCandidate and eachDateCandidate.claim.mainsnak.snaktype == 'value' then
						--mw.logObject(eachDateCandidate, '[Biodata] disapp date')
						deathDate = GregorianDate.displayDualDateIfInInterval(eachDateCandidate, true)
						deathDateRefs = wikidata.outputReferences(eachDateCandidate.claim)
						deathTs = DateUtils.parseWikidataDate(eachDateCandidate.claim.mainsnak.datavalue.value.time, eachDateCandidate.claim.mainsnak.datavalue.value.precision)
						deathIntro = 'disp. '
						break
					end
				end
			end
		end
	
		if not deathDate and wikidataDDate and wikidataDDate[1] and wikidataDDate[1].year < (os.date("*t").year - 120) then
			deathDate = "[[Categorie:Decese cu dată necunoscută]] ?"
		end
	end
	local deathPlace = args[4] or lc.displayFromParams('P20', q, deathTs or 'P570', 1, nil, refsEnabled)
	return p.fromParams(birthDate, birthDateRefs, birthPlace, deathDate, deathDateRefs, deathPlace, deathIntro)
end

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

return p