Módulo:NF

De Hispanopedia
Revisión del 21:02 12 may 2019 de es>Leoncastro (Creando módulo de reemplazo para Plantilla:NF)
(difs.) ← Revisión anterior | Revisión actual (difs.) | Revisión siguiente → (difs.)

La documentación para este módulo puede ser creada en Módulo:NF/doc

local getArgs = require('Módulo:Argumentos').obtenerArgumentosConValor
local toRoman = require('Módulo:Números').Romano
local p = {}

function error ( msg )
	return '<strong class="error">Error: ' .. msg .. '</strong>' .. 
		'[[Categoría:Wikipedia:Artículos con plantilla NF mal especificada]]'
end

function p.main ( frame )
	local wd = mw.wikibase.getEntityObject()
	local args = getArgs( frame )
	local result = frame:preprocess('{{Control de autoridades}}\n')
	local categories = ''
	local errors = ''

	--[==[ Sex/Gender ]==]--
	if wd and wd:formatPropertyValues( 'P31' ).value == 'ser humano' then
		-- wd.claims.P31[1].mainsnak.datavalue.value.id == 'Q5'
		local validsexgenders = {
			['femenino'] = 'Mujeres',
			['masculino'] = 'Hombres',
			['intersexual'] = 'Intersexuales',
			['mujer transgénero'] = 'Mujeres transgénero',
			['hombre transgénero'] = 'Hombres transgénero',
		}
		local sexgenderval = wd:formatPropertyValues( 'P21' ).value
		local sexgendercat = validsexgenders[ sexgenderval ] or 'Personas no categorizadas por sexo'
		categories = categories .. '[[Categoría:' .. sexgendercat .. ']]'
	end

	--[==[ Birth date ]==]--
	local birthdate = ''
	local living = false
	if args and args[1] and args[1] ~= '' then
		birthdate = mw.text.trim( args[1] )
	elseif wd and wd:formatPropertyValues( 'P31' ).value == 'ser humano' then
		-- wd.claims.P31[1].mainsnak.datavalue.value.id == 'Q5'
		birthdate = wd:formatPropertyValues( 'P569' ).value
	end

	local property1 = birthdate
	if property1 and property1 ~= '' then
		if mw.ustring.match( mw.ustring.upper( property1 ), "MISSING|UNKNOWN|?" ) then
			birthdate = ''
		elseif mw.ustring.match( property1, "milenio" ) then
			local m = mw.ustring.match( property1, "(%a+) milenio" ) -- 'el mm milenio'
			if m then
				birthdate = 'el ' .. m .. ' milenio'
				-- TODO: optimize
				if m == 'I' or m == 'II' or m == 'III' then
					living = true -- supposed (as it once was)
				end
			else
				m = mw.ustring.match( property1, "(%d+). milenio" ) -- 'mm. milenio'
				if m then
					birthdate = 'el ' .. toRoman(m) .. ' milenio'
					-- TODO: optimize
					if m == 1 or m == 2 or m == 3 then
						living = true -- supposed (as it once was)
					end
				else
					errors = error('fecha de nacimiento no válida')
					birthdate = ''
				end
			end
		elseif mw.ustring.match( property1, "siglo" ) then
			local dcc = math.floor( (os.date( "%Y" ) - 1) / 100 ) + 1 -- decimal current century
			local rc0 = mw.getContentLanguage():formatDate( 'xry', tostring( dcc ) .. '-1-1' ) -- roman current century
			local rc1 = mw.getContentLanguage():formatDate( 'xry', tostring( dcc - 1 ) .. '-1-1' ) -- roman previous century
			local rc2 = mw.getContentLanguage():formatDate( 'xry', tostring( dcc - 2 ) .. '-1-1' ) -- roman before previous century
			local c = mw.ustring.match( property1, "siglo (%a+)" ) -- 'el siglo cc'
			if c then
				birthdate = 'el siglo ' .. c
				-- roman number
				if ( mw.ustring.match(c, rc0) or mw.ustring.match(c, rc1) )
				or ( mw.ustring.match(c, rc2) and os.date( "%y" ) < 30 )
				then
					living = true -- maybe
				else
					living = false -- sure
				end
			else
				c = mw.ustring.match( property1, "(%d+)%. siglo" ) -- 'cc. siglo'
				if c then
					birthdate = 'el siglo ' .. toRoman(c)
					-- decimal number
					if ( c == dcc or c == dcc - 1 )
					or ( c == dcc - 2 and os.date( "%y" ) < 30 )
					then
						living = true -- maybe
					else
						living = false -- sure
					end
				else
					errors = error('fecha de nacimiento no válida')
					birthdate = ''
				end
			end
		elseif mw.ustring.match( property1, "años" ) then
			local dcc = math.floor( (os.date( "%Y" ) - 1) / 100 ) + 1 -- decimal current century
			local d = mw.ustring.match( property1, "años (%d+)" ) -- 'años yyyy'
			if d then
				birthdate = 'los años ' .. d
				local c = math.floor( (d - 1) / 100 ) + 1
				if ( c == dcc or c == dcc - 1 )
				or ( c == dcc - 2 and os.date( "%y" ) < 30 )
				then
					living = true -- maybe
				else
					living = false -- sure
				end
			else
				errors = error('fecha de nacimiento no válida')
				birthdate = ''
			end
		else
			local y = mw.ustring.match( property1, "^%d+ %a+ (%d+)$" ) -- 'dd mmm aaaa'
			if y then
				birthdate = y
			else
				y = mw.ustring.match( property1, "^%a+ (%d+)$" ) -- 'mmm aaaa'
				if y then
					birthdate = y
				else
					y = mw.ustring.match( property1, "^(%d+)$" ) -- 'aaaa'
					if y then
						birthdate = y
					else
						errors = error('fecha de nacimiento no válida')
						birthdate = ''
					end
				end
			end
			if y then
				if os.date( "%Y" ) - y < 130 then
					living = true -- maybe
				end
			end
		end

		if birthdate and mw.ustring.match( property1, "-|a%. C%.|BCE?$" ) then
			living = false -- sure
			birthdate = birthdate .. ' a. C.'
		end
	end

	if birthdate and birthdate ~= '' then
		categories = categories .. '[[Categoría:Nacidos en ' .. birthdate .. ']]'
	else
		categories = categories .. '[[Categoría:Nacidos en año desconocido]]'
	end

	--[==[ Death date ]==]--
	local deathdate = ''
	if args and args[2] and args[2] ~= '' then
		deathdate = mw.text.trim( args[2] )
	elseif wd and wd:formatPropertyValues( 'P31' ).value == 'ser humano' then
		-- wd.claims.P31[1].mainsnak.datavalue.value.id == 'Q5'
		deathdate = wd:formatPropertyValues( 'P570' ).value
	end

	local property2 = deathdate
	if property2 and property2 ~= '' then
		living = false
		if mw.ustring.match( mw.ustring.upper( property2 ), "LIVING|VIVA" ) then
			living = true
			deathdate = ''
		elseif mw.ustring.match( mw.ustring.upper( property2 ), "MISSING|UNKNOWN|?" ) then
			deathdate = ''
		elseif mw.ustring.match( property2, "milenio" ) then
			local m = mw.ustring.match( property2, "(%a+) milenio" ) -- 'el mm milenio'
			if m then
				deathdate = 'el ' .. m .. ' milenio'
			else
				m = mw.ustring.match( property1, "(%d+). milenio" ) -- 'mm. milenio'
				if m then
					deathdate = 'el ' .. toRoman(m) .. ' milenio'
				else
					errors = error('fecha de fallecimiento no válida')
					deathdate = ''
				end
			end
		elseif mw.ustring.match( property2, "siglo" ) then
			local c = mw.ustring.match( property2, "siglo (%a+)" ) -- 'el siglo cc'
			if c then
				deathdate = 'el siglo ' .. c
			else
				c = mw.ustring.match( property2, "(%d+)%. siglo" ) -- 'cc. siglo'
				if c then
					deathdate = 'el siglo ' .. toRoman(c)
				else
					errors = error('fecha de fallecimiento no válida')
					deathdate = ''
				end
			end
		elseif mw.ustring.match( property2, "años" ) then
			local d = mw.ustring.match( property2, "años (%d+)" ) -- 'años yyyy'
			if d then
				deathdate = 'los años ' .. d
			else
				errors = error('fecha de nacimiento no válida')
				birthdate = ''
			end
		else
			local y = mw.ustring.match( property2, "^%d+ %a+ (%d+)$" ) -- 'dd mmm aaaa'
			if y then
				deathdate = y
			else
				y = mw.ustring.match( property2, "^%a+ (%d+)$" ) -- 'mmm aaaa'
				if y then
					deathdate = y
				else
					y = mw.ustring.match( property2, "^(%d+)$" ) -- 'aaaa'
					deathdate = y
				end
			end
		end

		if deathdate and mw.ustring.match( property2, "-|a%. C%.|BCE?$" ) then
			deathdate = deathdate .. ' a. C.'
		end
	end

	if deathdate and deathdate ~= '' then
		categories = categories .. '[[Categoría:Fallecidos en ' .. deathdate .. ']]'
	elseif not living then
		categories = categories .. '[[Categoría:Fallecidos en año desconocido]]'
	else
		categories = categories .. '[[Categoría:Personas vivas]]'
	end

	--[==[ Sort key ]==]--
	local sortkey = ''
	if args and args[3] and args[3] ~= '' then
		sortkey = args[3]
	elseif wd and wd:formatPropertyValues( 'P31' ).value == 'ser humano' then
		-- wd.claims.P31[1].mainsnak.datavalue.value.id == 'Q5'
		local givenname = wd:formatPropertyValues( 'P734' ).value
		local familyname = wd:formatPropertyValues( 'P735' ).value
		local secondfamilyname = wd:formatPropertyValues( 'P1950' ).value
		-- local birthname = wd:formatPropertyValues( 'P1477' ).value
		if familyname and familyname~= '' then
			sortkey = familyname
			if secondfamilyname and secondfamilyname ~= '' then
				sortkey = sortkey .. ' ' .. secondfamilyname
			end
		end
		if sortkey ~= '' and givenname and givenname ~= '' then
			sortkey = sortkey .. ', ' .. givenname
		end
	end
	if sortkey and sortkey ~= '' then
		sortkey = frame:preprocess('{{ORDENAR:' .. sortkey .. '}}\n')
	end

	--[==[ NF ]==]--
	result = result .. sortkey .. categories .. errors
	return result
end

return p