אונטערשייד צווישן ווערסיעס פון "יחידה:תבנית מידע/אישיות"

פון המכלול
קפיצה לניווט קפיצה לחיפוש
(דא איז דער לאך. קרויז אויס אידישע דאטום צו רעכענען אויך בעפאר קביעת הלוח)
ק (הגהה)
שורה 212: שורה 212:
local genBirthDate = templateArgs['תאריך לידה'] or templateArgs['געבורט דאטום']
local genBirthDate = templateArgs['תאריך לידה'] or templateArgs['געבורט דאטום']
if genBirthDate == nil and templateArgs['תאריך לידה עברי'] ~= nil then
if genBirthDate == nil and templateArgs['תאריך לידה עברי'] ~= nil then
local l = frame:preprocess(args['תאריך לידה עברי']) -- preprocess, to handle {{לינק צו אידיש יאר}}
local l = frame:preprocess(templateArgs['תאריך לידה עברי']) -- preprocess, to handle {{לינק צו אידיש יאר}}
if (#mw.text.split(l, '%s' ) == 1 or count(l, "%[%[") == 1) then --most probably just year or yearlink
if (#mw.text.split(l, '%s' ) == 1 or count(l, "%[%[") == 1) then --most probably just year or yearlink
if #mw.text.split(l, '%s' ) > 1 then
if #mw.text.split(l, '%s' ) > 1 then
שורה 231: שורה 231:
local genDeathDate = templateArgs['תאריך פטירה'] or templateArgs['פטירה דאטום']
local genDeathDate = templateArgs['תאריך פטירה'] or templateArgs['פטירה דאטום']
if genDeathDate == nil and templateArgs['תאריך פטירה עברי'] ~= nil then
if genDeathDate == nil and templateArgs['תאריך פטירה עברי'] ~= nil then
local p = frame:preprocess(args['תאריך פטירה עברי']) -- preprocess, to handle {{לינק צו אידיש יאר}}
local p = frame:preprocess(templateArgs['תאריך פטירה עברי']) -- preprocess, to handle {{לינק צו אידיש יאר}}
if (#mw.text.split(p, '%s' ) == 1 or count(p, "%[%[") == 1) then --most probably just year or yearlink
if (#mw.text.split(p, '%s' ) == 1 or count(p, "%[%[") == 1) then --most probably just year or yearlink
if #mw.text.split(p, '%s' ) > 1 then
if #mw.text.split(p, '%s' ) > 1 then

רעוויזיע פון 15:56, 20 נאוועמבער 2023

מען קען שאפן דאקומענטאציע פאר דעם מאדול ביי יחידה:תבנית מידע/אישיות/דאק

local Infobox = require('Module:תבנית מידע').Infobox
local LocationAndCountry = require('Module:LocationAndCountry')
local PropertyLink = require('Module:PropertyLink')
local ageModule = require('Module:עלטער')
local Date = require('Module:דאטום')
local hebrewDateModule = require('Module:אידישע דאטום')
local WikidataCrossValidation = require('Module:WikidataCrossValidation')
local gim = require( "Module:גימטריה" )

function count(str, substring)
    local count = 0
    local _, occurrences = string.gsub(str, substring, function()
        count = count + 1
    end)
    return count
end

--[[ 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, gender)
	local result=''
	local fromWikidata=nil
	entityId = entityId
	if ( deathCause == nil or deathCause =='' ) 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)
			fromWikidata=true
		end
	end

	if deathCause and deathCause ~= '' then
		if deathCause == 'נהרג' or deathCause == 'נהרגה' or deathCause == 'מוות בקרב' or deathCause == 'נהרג בקרב' or deathCause == 'נהרגה בקרב' or deathCause == 'תאונה' or deathCause == 'טביעה' then
			return (gender=='נקבה' and 'נהרגה') or 'נהרג'
		elseif deathCause == 'נעדר' or deathCause == 'נעדרת' then
			return (gender=='נקבה' and 'נעדרת') or 'נעדר'
		elseif deathCause == 'נרצח' or deathCause == 'נרצחה' or deathCause == 'רצח' or deathCause == 'רצח המוני' or deathCause == 'רעל' then
			return (gender=='נקבה' and 'נרצחה') or 'נרצח'
	    elseif deathCause == 'התאבד' or deathCause == 'התאבדה' or deathCause == 'התאבדות' then
			return (gender=='נקבה' and 'התאבדה') or 'התאבד'
		elseif deathCause == 'הוצא להורג' or deathCause == 'הוצאה להורג' or deathCause == 'עונש מוות' or deathCause == 'תלייה' then
			return 'הוצאה להורג'
		elseif not fromWikidata then result='[[קאטעגאריע:ערכים עם סיבת מוות שגויה בתבנית]]'
		end
	end
	return result .. 'פטירה'
end

function getEventDetails(generalDate, hebDate, eventPlace, propDate, propPlace, age, usingWikidata, infoObj)
	entityId = infoObj.args.entityId
	age = age or ''
	local formattedEvent = ''
	local placeDate = propDate
	if generalDate~=nil and generalDate~='' then
		local success, placeDateParsed  = pcall(Date.newFromWikitext, generalDate)
		if success then
			placeDate = placeDateParsed
		end
		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, placeDate , 1)
		if success and res and #res>0 then
			usingWikidata = true
			eventPlace = res
		end
	else
		local pageNs = mw.title.getCurrentTitle().namespace
		if pageNs==0 and not infoObj.isNonPerson then
			local matching = WikidataCrossValidation.crossValidate(eventPlace, propPlace, entityId )
			if matching then
				table.insert(infoObj.wikidataCats, WikidataCrossValidation.maintainceCategory(matching, propPlace))
			end
		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=he#P569|עריכת הנתון בוויקינתונים]]'
	end

	return formattedEvent, usingWikidata
end

-- Split the comma-separated lists into tables and sort them
local function splitString(inputstr, sep)
    local sep, fields = sep or ",", {}
    local pattern = string.format("([^%s]+)", sep)
    inputstr:gsub(pattern, function(c) fields[#fields + 1] = c end)
    return fields
end

function addMissingImage(infoObj, birthDate, deathDate, gender)
	return
end

function fillWikidataParam(templateArgs, paramName, property, infoObj)
	local usingWikidata = false
	if infoObj.args.entityId == nil then
		local matching = WikidataCrossValidation.crossValidate(templateArgs[paramName], property, infoObj.args.entityId )
		if matching then
			table.insert(infoObj.wikidataCats, WikidataCrossValidation.maintainceCategory(matching, property))
		end
		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 pageNs = mw.title.getCurrentTitle().namespace
		if pageNs==0 and not infoObj.isNonPerson then
			local matching = WikidataCrossValidation.crossValidate(templateArgs[paramName], property, infoObj.args.entityId )
			if matching then
				table.insert(infoObj.wikidataCats, WikidataCrossValidation.maintainceCategory(matching, property))
			end
		end
	end
	return usingWikidata
end

local function instanceOfViolation(infoObj)
	local pageNs = mw.title.getCurrentTitle().namespace
	if pageNs~=0 then
		return true
	end
	
	local wikidataInstanceOf = nil
	if infoObj.args.entityId ~= nil then
		wikidataInstanceOf = mw.wikibase.getBestStatements(infoObj.args.entityId, 'P31')
		wikidataInstanceOf = wikidataInstanceOf and wikidataInstanceOf[1] and wikidataInstanceOf[1].mainsnak
	end
	
	if wikidataInstanceOf == nil then
		local matching = WikidataCrossValidation.crossValidate('אדם', 'P31', infoObj.args.entityId )
		if matching then
			mw.logObject(matching)
			table.insert(infoObj.wikidataCats, WikidataCrossValidation.maintainceCategory(matching, 'P31'))
		end
	else
		wikidataInstanceOf = wikidataInstanceOf.datavalue and wikidataInstanceOf.datavalue.value and wikidataInstanceOf.datavalue.value.id
		if wikidataInstanceOf ~= 'Q5' then
			table.insert(infoObj.wikidataCats, '')
			return true
		end
	end
	return false
end

function addPersonDetails(frame, infoObj)
	local templateArgs = frame:getParent().args
	local usingBirthWikidata = false
	local usingDeathWikidata = false
	local birthLabel = 'געבורט'
	local deathLabel = 'פטירה' 
	
	infoObj.isNonPerson = instanceOfViolation(infoObj)
	
	local wikidataGender = nil
	if not infoObj.isNonPerson then
		wikidataGender = PropertyLink.getItem( 'P21', infoObj.args.entityId )
		if wikidataGender == nil then
			table.insert(infoObj.wikidataCats, '')
		else
			if (wikidataGender == "Q6581097") then		-- male
				wikidataGender = 'זכר'
			elseif (wikidataGender == "Q2449503") then	-- transgender male
				wikidataGender = 'זכר'
			elseif (wikidataGender == "Q15145778") then	-- cisgender male
				wikidataGender = 'זכר'
			elseif (wikidataGender == "Q6581072") then	-- female
				wikidataGender = 'נקבה'
			elseif (wikidataGender == "Q1052281") then	-- transgender female
				wikidataGender = 'נקבה'
			elseif (wikidataGender == "Q15145779") then	-- cisgender female
				wikidataGender = 'נקבה'
			elseif wikidataGender == 'Q1097630' -- intersex
					or wikidataGender == 'Q48270' -- non binary
				then wikidataGender = '' -- עד שתחליטו
			end
		end
	end
	-- fill birth/death dates from wikidata if missing and the person is not a human biblical figure
	if PropertyLink.getItem( 'P31', infoObj.args.entityId )~="Q20643955" then
		usingBirthWikidata = fillWikidataParam(templateArgs, 'תאריך לידה', 'P569', infoObj)
		usingDeathWikidata = fillWikidataParam(templateArgs, 'תאריך פטירה', 'P570', infoObj)
	end
	
	-- יחידה:אידישע דאטום ווערט פארלוירן מיט יארן בעפאר ד'ק"כ
	local genBirthDate = templateArgs['תאריך לידה'] or templateArgs['געבורט דאטום']
	if genBirthDate == nil and templateArgs['תאריך לידה עברי'] ~= nil then
		local l = frame:preprocess(templateArgs['תאריך לידה עברי']) -- preprocess, to handle {{לינק צו אידיש יאר}}
		if (#mw.text.split(l, '%s' ) == 1 or count(l, "%[%[") == 1) then --most probably just year or yearlink
			if #mw.text.split(l, '%s' ) > 1 then
				l = l:match("%[%[.-|(.*)%]%]")
			end
			local birthParts = mw.text.split( l, "'", true )
			if #birthParts == 1 then
				table.insert(birthParts, 1, 0) -- אדער אלף החמישי?
			end
			local BirthDateNum = gim.gimatria(birthParts[1])*1000 + gim.gimatria(birthParts[2]) - 3760
			if BirthDateNum < 1 then
				genBirthDate = tostring(math.abs(BirthDateNum)) .. " לפנה\"ס"
			elseif BirthDateNum < 4120 then
				genBirthDate = tostring(BirthDateNum)
			end
		end
	end
	local genDeathDate = templateArgs['תאריך פטירה'] or templateArgs['פטירה דאטום']
	if genDeathDate == nil and templateArgs['תאריך פטירה עברי'] ~= nil then
		local p = frame:preprocess(templateArgs['תאריך פטירה עברי']) -- preprocess, to handle {{לינק צו אידיש יאר}}
		if (#mw.text.split(p, '%s' ) == 1 or count(p, "%[%[") == 1) then --most probably just year or yearlink
			if #mw.text.split(p, '%s' ) > 1 then
				p = p:match("%[%[.-|(.*)%]%]")
			end
			local deathParts = mw.text.split( p, "'", true )
			if #deathParts == 1 then
				table.insert(deathParts, 1, 0) -- אדער אלף החמישי?
			end
			local DeathDateNum = gim.gimatria(deathParts[1])*1000 + gim.gimatria(deathParts[2]) - 3760
			if DeathDateNum < 1 then
				genDeathDate = tostring(math.abs(DeathDateNum)) .. " לפנה\"ס"
			elseif DeathDateNum < 4120 then
				genDeathDate = tostring(DeathDateNum)
			end
		end
	end
	
	local birthDate = getSpeculativeDate(genBirthDate, templateArgs['תאריך לידה עברי'])
	local deathDate = getSpeculativeDate(genDeathDate, templateArgs['תאריך פטירה עברי'])
	
	if birthDate~=nil then
		local age = nil
		if deathDate==nil then
			age = ageModule.ageCalc(birthDate, nil, true, false, true, nil, wikidataGender) or ''
		end
		local birthDetails, usingBirthWikidataEvent = getEventDetails(templateArgs['תאריך לידה'], templateArgs['תאריך לידה עברי'], templateArgs['געבורט ארט'], 'P569', 'P19', age, usingBirthWikidata, infoObj)
			table.insert(infoObj.templateStructure, 1, {
			label=birthLabel,
			data=birthDetails
		})
		usingBirthWikidata = usingBirthWikidata or usingBirthWikidataEvent
	elseif templateArgs['געבורט ארט']~= '-' then
		fillWikidataParam(templateArgs, 'געבורט ארט', 'P19', infoObj)
		if  templateArgs['געבורט ארט']~= nil and templateArgs['געבורט ארט']~= '' then -- only if we fill the parameter eventually - add to template
			table.insert(infoObj.templateStructure, 1, {
				label=birthLabel,
				data= templateArgs['געבורט ארט']
			})
		end
	end

	deathLabel = getDeathLabel(templateArgs['סיבת המוות'], infoObj.args.entityId, wikidataGender)
	if deathDate~=nil then
		local age =  ageModule.ageCalc(birthDate, deathDate, false, false, true) or ''
		local deathDetails, usingDeathWikidataEvent = getEventDetails(templateArgs['תאריך פטירה'], templateArgs['תאריך פטירה עברי'], templateArgs['פטירה ארט'], 'P570', 'P20', age, usingDeathWikidata, infoObj)
			table.insert(infoObj.templateStructure, 2, {
			label=deathLabel,
			data=deathDetails
		})
		usingDeathWikidata = usingDeathWikidata or usingDeathWikidataEvent
	elseif templateArgs['פטירה ארט']~= '-' then
		fillWikidataParam(templateArgs, 'פטירה ארט', 'P20', infoObj)
		if templateArgs['פטירה ארט']~= nil and templateArgs['פטירה ארט']~= '' then -- only if we fill the parameter eventually - add to template
			table.insert(infoObj.templateStructure, 2, {
				label=deathLabel,
				data= templateArgs['פטירה ארט']
			})
		end
	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, wikidataGender)
	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
}