יחידה:אינפאקעסטל/פערזענלעכקייט

פֿון װיקיפּעדיע

דער מאדול פארברייטערט יחידה:אינפאקעסטל פאר אינפאקעסטלעך פון פערזענלעכקייט. דער מאדול אנטהאלט אויטאמאטישן שטיץ אין די כאראקטעריסטישע פאראמעטערס פאר פערזענלעכקייטן, צום ביישפיל, דאטעס און ערטער פון געבורט/פטירה, צוזאמען מיט דעם עלטער.


local Infobox = require('Module:תבנית מידע').Infobox
local LocationAndCountry = require('Module:LocationAndCountry')
local PropertyLink = require('Module:PropertyLink')
local ageModule = require('Module:עלטער')
local hebrewDateModule = require('Module:יידישע דאטע')
local WikidataCrossValidation = require('Module:WikidataCrossValidation')

--[[ Get general date if known, otherwise guess from hebrew date.
]]
function getSpeculativeDate(generalDate, hebrewDate)
	if generalDate == '-' then return nil end
	if generalDate~=nil and generalDate~='' then return generalDate end
	if hebrewDate~=nil and hebrewDate~='' then
		local success, sepcDate = pcall(hebrewDateModule.pick, mw.getCurrentFrame():newChild{ args = {hebrew=hebrewDate, world='', exact='כן' } })
		if not success or sepcDate=='' then return nil end
		return sepcDate
	end
	return nil
end

local function getDeathLabel(deathCause, entityId)
	entityId = entityId or mw.wikibase.getEntityIdForCurrentPage()
	if deathCause == nil and entityId ~= nil then
		local deathCauseWikidata = mw.wikibase.getBestStatements(entityId, 'P1196')
		deathCauseWikidata = deathCauseWikidata and deathCauseWikidata[1] and deathCauseWikidata[1].mainsnak
		if deathCauseWikidata then
			deathCause = mw.wikibase.renderSnak(deathCauseWikidata)
		end
	end
	local gender = PropertyLink.getPropertyByOptions( 'P21', entityId )
	
	if deathCause and deathCause ~= '' then
		if deathCause == 'נהרג' or deathCause == 'נהרגה' then
			return (gender=='נקיבה' and 'נהרגה') or 'נהרג'
		elseif deathCause == 'מארד' or deathCause == 'נרצח' or deathCause == 'נרצחה' or deathCause == 'רצח' then
			return 'דערמארדעט'
	        elseif deathCause == 'התאבד' or deathCause == 'התאבדה' or deathCause == 'התאבדות' then
			return (gender=='נקיבה' and 'התאבדה') or 'התאבד'
		elseif deathCause == 'הוצא להורג' or deathCause == 'הוצאה להורג' or deathCause == 'עונש מוות' then
			return 'הוצאה להורג'			
		end
	end
	return 'טויט'
end

function getEventDetails(generalDate, hebDate, eventPlace, propDate, propPlace, age, usingWikidata, entityId)
	entityId = entityId or mw.wikibase.getEntityIdForCurrentPage()
	age = age or ''
	local formattedEvent
	if generalDate~=nil and generalDate~='' then
		formattedEvent = generalDate .. age
		if hebDate~=nil and hebDate~='' then 
			formattedEvent = formattedEvent ..'<br/>' ..hebDate
		end
	elseif  hebDate~=nil and hebDate~='' then
		formattedEvent = hebDate .. age
	end
	
	if (eventPlace == nil or eventPlace == '') and entityId then
		local success, res  = pcall(LocationAndCountry.displayFromParams, propPlace, entityId, propDate , 1)
		if success and res and #res>0 then
			usingWikidata = true
			eventPlace = res
		end
	end

	if eventPlace~='' and eventPlace~=nil and eventPlace~='-' then
		formattedEvent = formattedEvent..'<br/>' ..eventPlace
	end
	
	if usingWikidata and entityId then
		formattedEvent = formattedEvent .. ' [[File:Blue pencil RTL.svg|15px|link=https://www.wikidata.org/wiki/'..entityId.. '?uselang=yi#P569|רעדאקטירן דעם פֿאקט ביי וויקידאטן]]'
	end

	return formattedEvent, usingWikidata
end

function addMissingImage(infoObj, birthDate, deathDate)
	if infoObj.args.image ~= nil and #infoObj.args.image > 0 then return end  -- there is already image 
	local latestDate = deathDate or birthDate
	if latestDate == nil then return end
	local dateModule = require('Module:דאטע')
	local success, res = pcall(dateModule.parseDateRange, latestDate, 'raw')
	if success==false then return end -- there is no known VALID date
	local yearsEstimated = res/(60*60*24*365)
	if yearsEstimated>150 then return end 
	
	if infoObj.args.entityId == nil then
		return false -- no wikidata entity
	end

	local gender = PropertyLink.getPropertyByOptions( 'P21', infoObj.args.entityId )
	local frame = mw.getCurrentFrame()
	infoObj.args.image = frame:expandTemplate{ title = 'קיין בילד', args = { gender, ['אויסגלייכונג'] = 'center' } }
end

function fillWikidataParam(templateArgs, paramName, property, infoObj)
	local usingWikidata = false
	if infoObj.args.entityId == nil then
		return false -- no wikidata entity
	end
	if templateArgs[paramName]==nil or templateArgs[paramName]=='' then
		templateArgs[paramName] = PropertyLink.getPropertyByOptions( property, infoObj.args.entityId )
		if templateArgs[paramName] and #templateArgs[paramName]>0 then
			usingWikidata = true
		end
	else
		local matching = WikidataCrossValidation.crossValidate(templateArgs[paramName], property, infoObj.args.entityId )
		if matching then
			table.insert(infoObj.wikidataCats, WikidataCrossValidation.maintainceCategory(matching, property))
		end
	end
	return usingWikidata
end

function addPersonDetails(frame, infoObj)
	local templateArgs = frame:getParent().args
	local usingBirthWikidata = false
	local usingDeathWikidata = false
	local birthLabel = 'געבורט'
	local deathLabel = 'טויט' 
	
	-- fill birth/death dates from wikidata is missing
	usingBirthWikidata = fillWikidataParam(templateArgs, 'געבורט דאטע', 'P569', infoObj)
	usingDeathWikidata = fillWikidataParam(templateArgs, 'טויט דאטע', 'P570', infoObj)
	
	local birthDate = getSpeculativeDate(templateArgs['געבורט דאטע'], templateArgs['יידישע געבורט דאטע'])
	local deathDate = getSpeculativeDate(templateArgs['טויט דאטע'], templateArgs['יידישע טויט דאטע'])
	
	if birthDate~=nil then
		local age = nil
		if deathDate==nil then
			age = ageModule.ageCalc(birthDate, nil, true, false, true) or ''
		end
		local birthDetails, usingBirthWikidataEvent = getEventDetails(templateArgs['געבורט דאטע'], templateArgs['יידישע געבורט דאטע'], templateArgs['געבורט ארט'], 'P569', 'P19', age, usingBirthWikidata, infoObj.args.entityId)
			table.insert(infoObj.templateStructure, 1, {
			label='געבורט',
			data=birthDetails
		})
		usingBirthWikidata = usingBirthWikidata or usingBirthWikidataEvent
	 else
    	if templateArgs['געבורט ארט']~= '-' and templateArgs['געבורט ארט']~= '' and  templateArgs['געבורט ארט']~=nil and #templateArgs['געבורט ארט']>0 then
			table.insert(infoObj.templateStructure, 1, {
	    	label=birthLabel,
	    	data= templateArgs['געבורט ארט']
		}) 
	  end
    end
	
	if deathDate~=nil then
		deathLabel = getDeathLabel(templateArgs['סיבת המוות'], infoObj.args.entityId)
		local age =  ageModule.ageCalc(birthDate, deathDate, false, false, true) or ''
		local deathDetails, usingDeathWikidataEvent = getEventDetails(templateArgs['טויט דאטע'], templateArgs['יידישע טויט דאטע'], templateArgs['טויט ארט'], 'P570', 'P20', age, usingDeathWikidata, infoObj.args.entityId)
			table.insert(infoObj.templateStructure, 2, {
			label=deathLabel,
			data=deathDetails
		})
		usingDeathWikidata = usingDeathWikidata or usingDeathWikidataEvent
	end
	
	if infoObj.isSelfUse then
			table.insert(infoObj.templateStructure, 1, {
			label=birthLabel,
			data='{{{געבורט דאטע}}}<br/>{{{געבורט ארט}}}'
			})
		table.insert(infoObj.templateStructure, 2, {
			label=deathLabel,
			data='{{{טויט דאטע}}} <br/>{{{טויט ארט}}}'
			})
	end	
	
	if usingBirthWikidata or usingDeathWikidata then
		infoObj.args.usingWikidata = true
	end
	
	if frame.args['כותרת0'] then
		table.insert(infoObj.templateStructure, 1, {
			header=1,
			data=frame.args['כותרת0'],
			rowstyle=frame.args['עיצוב-שורה0'],
			rowclass=frame.args['מחלקה-שורה0'],
			headerstyle=frame.args['כותרת0-עיצוב']
		})
	end

	if frame.args['תמונה']~='-' then
		addMissingImage(infoObj, birthDate, deathDate)
	end
end

function infobox(frame)
	local infoObj = Infobox:new()
	frame.args['תמונה-גודל'] = frame.args['תמונה-גודל']  or '220'
	infoObj:parseArgs(frame)
	
	if infoObj.args.title~='-' then
		addPersonDetails(frame, infoObj)
	end
	return infoObj:render()
end


return 	{
	['אינפאקעסטל']=infobox,
	['addPersonDetails'] = addPersonDetails
}