לדלג לתוכן

יחידה:גיל לערכי אישים: הבדלים בין גרסאות בדף

מתוך צפונות ויקי
מ ביטול גרסה 18237664 של IKhitron (שיחה), בגלל תקציר שגוי אתה מקלקל?
מ 5 גרסאות של הדף wikipedia:he:יחידה:גיל_לערכי_אישים יובאו
 
(44 גרסאות ביניים של 11 משתמשים אינן מוצגות)
שורה 6: שורה 6:
}
}


function invalidDate(frame, Date, ApproxDate)
function ToDate(frame, Date, ApproxDate)
local isValidDate, diffDate = pcall(dateParser.parseDateRange, Date, 'raw', false)
local isValidDate, res = pcall(dateParser.newFromWikitext, Date)
if not isValidDate then
local errors = ''
-- invalid date
if isValidDate and dateParser.age(res).year<0 then
return frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = {  
-- date is in the future
isValidDate = false
local title = mw.title.getCurrentTitle()
if title.namespace==0 then
errors = Errors['future-date']
else
errors = '<!--' .. Errors['future-date'] .. '-->'
end
elseif not isValidDate or res.precision < dateParser.PRECISION.YEAR then
-- date is either invalid or is imprecise
isValidDate = false -- mark as invalid anyway, even if "only" imprecise
errors = frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = {  
['מקורב']=ApproxDate,
['מקורב']=ApproxDate,
['נוסף']=Date,
['נוסף']=Date,
שורה 16: שורה 27:
} }
} }
end
end
if tonumber(diffDate)<0 then  
return isValidDate, res, errors
local title = mw.title.getCurrentTitle()
end
if title.namespace==0 then
 
return Errors['future-date']-- date is in the future
function consistentWithWikidata(localDate, wikidataClaim)
else
local possibleDate, possibleDateValue, isConsistent
return '<!--' .. Errors['future-date'] .. '-->'
if not  wikidataClaim then
end
return true
end
isConsistent = true
for i,possibleDate in ipairs(wikidataClaim) do
if possibleDate.mainsnak and possibleDate.mainsnak.datavalue and possibleDate.mainsnak.datavalue.value then
possibleDateValue = possibleDate.mainsnak.datavalue.value
local iswikidataValid, wikidataDate = pcall(dateParser.newFromWikidataValue, possibleDateValue)
if iswikidataValid and localDate.calendar == wikidataDate.calendar then
if (localDate.year ~= wikidataDate.year and possibleDateValue.precision>=dateParser.PRECISION.YEAR) or
  (localDate.month ~= wikidataDate.month and possibleDateValue.precision>=dateParser.PRECISION.MONTH) or
  (localDate.day ~= wikidataDate.day and possibleDateValue.precision>=dateParser.PRECISION.DAY)  then
isConsistent = false
else
isConsistent = true
break
end
end
end
end
end
 
return isConsistent
end
end


function ageCalc(frame)
function ageCalcFromFrame(frame)
local args = Arguments.getArgs(frame)
local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true })
local BirthDate = args['תאריך לידה']
--local age_string = args['מציין']
local DeathDate = args['תאריך פטירה']
local BirthDateStr = args['תאריך לידה']
local DeathDateStr = args['תאריך פטירה']
local DisplayForAlive = args['הצג אם בחיים']
local DisplayForAlive = args['הצג אם בחיים']
local DisplayForDead = args['הצג אם נפטר']
local DisplayForDead = args['הצג אם נפטר']
local Brackets = args['סוגריים']
local Brackets = args['סוגריים']
local ApproxDate = args['מקורב']
local ApproxDate = args['מקורב']
local warnings = {}
local dateRangeStr
DisplayForAlive = (DisplayForAlive=='כן')
DisplayForAlive = (DisplayForAlive=='כן')
DisplayForDead = (DisplayForDead=='כן')
DisplayForDead = (DisplayForDead=='כן')
Brackets = (Brackets~='לא')
Brackets = (Brackets~='לא')
return ageCalc(BirthDateStr, DeathDateStr, DisplayForAlive, DisplayForDead, Brackets, ApproxDate)
local entity = mw.wikibase.getEntityObject()
end
if BirthDate=='' or BirthDate==nil then
 
-- pull the birth date from wikidata
function ageCalc(BirthDateStr, DeathDateStr, DisplayForAlive, DisplayForDead, Brackets, ApproxDate, gender)
if entity and entity.claims and entity.claims['P569'] then
local frame = mw.getCurrentFrame()
BirthDate=entity:formatPropertyValues( 'P569' ).value
local warnings = {}
end
local dateRangeStr
end
local wikidataBirth, wikidataDeath
if BirthDateStr == '' then BirthDateStr = nil end
if DeathDateStr == '' then DeathDateStr = nil end
local entityId = mw.wikibase.getEntityIdForCurrentPage()
wikidataBirth = entityId and mw.wikibase.getBestStatements( entityId, 'P569' )
wikidataDeath = entityId and wikidataBirth and mw.wikibase.getBestStatements( entityId, 'P570' )


BirthDateStr = BirthDateStr or ((wikidataBirth and #wikidataBirth>0 and mw.wikibase.renderSnak( wikidataBirth[1].mainsnak ) or nil))
-- validate dates are valid
-- validate dates are valid
if BirthDate==nil or BirthDate=='' then
if not BirthDateStr then
return -- missing date, nothing to do
return -- missing date, nothing to do
end
end
DeathDateStr = DeathDateStr or ((wikidataDeath and #wikidataDeath>0 and mw.wikibase.renderSnak( wikidataDeath[1].mainsnak ) or nil))
if DeathDate=='' or DeathDate==nil then
if DeathDateStr =='' then DeathDateStr = nil end
-- pull the death date from wikidata
local IsDead = (DeathDateStr~=nil)
if entity and entity.claims and entity.claims['P570'] then
DeathDate = entity:formatPropertyValues( 'P570' ).value
end
end
local IsDead = (DeathDate~=nil and DeathDate~='')
-- early termination
-- early termination
שורה 67: שורה 96:
return ''
return ''
end
end
local isValidDate, BirthDate, errDate = ToDate(frame, BirthDateStr, ApproxDate)
if not isValidDate  then
return errDate
end
if wikidataBirth and wikidataBirth[1] and wikidataBirth[1].mainsnak  then
if not consistentWithWikidata(BirthDate, wikidataBirth) then
table.insert(warnings, '[[קטגוריה:דפים עם שנות חיים שלא תואמים את ויקינתונים]]')
end
end
-- skip age calculation if we dont have enough precision
if BirthDate.precision < dateParser.PRECISION.YEAR then return nil end
local errBirthDate = invalidDate(frame, BirthDate, ApproxDate)
local DeathDate
if errBirthDate  then
return errBirthDate
end
 
if IsDead then  
if IsDead then  
local errDeathDate = invalidDate(frame, DeathDate, ApproxDate)
isValidDate, DeathDate, errDate = ToDate(frame, DeathDateStr, ApproxDate)
if errDeathDate then
if not isValidDate then
return errDeathDate-- invalid death date
return errDate-- invalid death date
end
end
prefix = 'בגיל '
dateRangeStr = string.format('%s - %s', BirthDate, DeathDate)
if not consistentWithWikidata(DeathDate, wikidataDeath) then
table.insert(warnings, '[[קטגוריה:דפים עם שנות חיים שלא תואמים את ויקינתונים]]')
end
prefix = 'בגיל '  
else
else
prefix = frame:expandTemplate{ title = 'לפי מגדר/בדוק', args = { 'בן&nbsp;', 'בת&nbsp;' } }
if gender == nil and entityId~=nil then
dateRangeStr = BirthDate
local PropertyLink = require('Module:PropertyLink')
gender = PropertyLink.getItem( 'P21', entityId )
if (gender == "Q6581097") then -- male
gender = 'זכר'
elseif (gender == "Q2449503") then -- transgender male
gender = 'זכר'
elseif (gender == "Q15145778") then -- cisgender male
gender = 'זכר'
elseif (gender == "Q6581072") then -- female
gender = 'נקבה'
elseif (gender == "Q1052281") then -- transgender female
gender = 'נקבה'
elseif (gender == "Q15145779") then -- cisgender female
gender = 'נקבה'
end
end
prefix = 'גיל:&nbsp;'
 
if gender == 'נקבה' then
prefix = 'בת&nbsp;'
elseif gender == 'זכר' then
prefix = 'בן&nbsp;'
end
end
end
-- skip age calculation if we dont have enough precision
if DeathDate and DeathDate.precision and DeathDate.precision < dateParser.PRECISION.YEAR then return nil end
local success, Age = pcall(dateParser.age, BirthDate , DeathDate)
if success then
AgeYears = Age.year
if Age.year <= 0 then
if Age.month == 1 then
result = prefix..'חודש'
elseif Age.month == 2 then
result  = prefix..'חודשיים'
else
result = prefix..tostring(Age.month)..' חודשים'
end
if Age.day > 0 then
if Age.day == 1 then
result = result..' ויום'
elseif Age.day == 2 then
result = result..' ויומיים'
else
result = result..'  ו־'..tostring(Age.day)..' ימים'
end
end
elseif Age.year < 3 then
if Age.year == 1 then
result = prefix..'שנה'
elseif Age.year == 2 then
result = prefix..'שנתיים'
end
if Age.month == 0 then
if Age.day > 0 then
if Age.day == 1 then
result = result..' ויום'
elseif Age.day == 2 then
result = result..' ויומיים'
else
result = result..'  ו־'..tostring(Age.day)..' ימים'
end
end
elseif Age.month == 1 then
result = result..' וחודש'
elseif Age.month == 2 then
result  = result..' וחודשיים'
else
result = result..' ו־'..tostring(Age.month)..' חודשים'
end
else
result = prefix..tostring(Age.year)
end


local success, result = pcall(dateParser.parseDateRange, dateRangeStr , {'years'}, true)
 
if BirthDate.precision < dateParser.PRECISION.MONTH or (DeathDate and DeathDate.precision < dateParser.PRECISION.MONTH) then
if success then
result = result .. ' בערך'
result= mw.ustring.gsub(result,"כ־(.+) שנים","%1 בערך")
end
result = prefix..mw.ustring.gsub(result," שנים","")
local success, AgeYears = pcall(dateParser.parseDateRange, dateRangeStr , 'raw', true)
-- ignore warning in specific template
local YearToSeconds = 60*60*24*365
local noAgeWarning = false
AgeYears = tonumber(AgeYears)/YearToSeconds
if mw.getCurrentFrame():getParent():getTitle() == 'תבנית:אישיות תנ"כית' or mw.getCurrentFrame():getParent():getTitle() == 'תבנית:בעל חיים מפורסם' then
if AgeYears > 139 then  
noAgeWarning = true
table.insert(warnings, Errors['future-date'])
end
elseif AgeYears>109 then  
local title = mw.title.getCurrentTitle()
table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל 110]]')
-- ignore warning in other namespace except the main NS
elseif AgeYears>100 then
if title.namespace==0 and not noAgeWarning then
table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל מאה]]')
if AgeYears > 139 then
local instanceOf = entityId and mw.wikibase.getBestStatements( entityId, 'P31' )
local isHuman = instanceOf and instanceOf[1].mainsnak and instanceOf[1].mainsnak.datavalue.value.id=='Q5'
if isHuman then
table.insert(warnings, Errors['future-date'])
end
elseif AgeYears>109 then  
table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל 110]]')
elseif AgeYears>99 then
table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל מאה]]')
end
end
end
else
else
result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = {  
result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = {  
['מקורב']=ApproxDate,
['מקורב']=ApproxDate,
שורה 108: שורה 232:
} }
} }
end
end
 
warnings = table.concat( warnings, '')
warnings = table.concat( warnings, '')
if #warnings > 0 then
warnings = string.format('%s ', warnings)
end
if Brackets then
if Brackets then
result = string.format(' (%s)', result)
result = string.format(' (%s)', result)
end
end
return string.format('%s <span style="white-space: nowrap;">%s</span>', warnings, result)
return string.format('%s<span style="white-space: nowrap;">%s</span>', warnings, result)
end
end




return {
return {
['גיל'] = ageCalc
['גיל'] = ageCalcFromFrame,
ageCalc = ageCalc
}
}

גרסה אחרונה מ־20:14, 18 ביוני 2023

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:גיל לערכי אישים/תיעוד

local dateParser = require( "Module:תאריך" )
local Arguments = require( "Module:Arguments" )

local Errors = {
	['future-date'] = '[[קטגוריה:תאריכי חיים לא נכונים]]'
}

function ToDate(frame, Date, ApproxDate)
	local isValidDate, res = pcall(dateParser.newFromWikitext, Date)
	local errors = ''
	if isValidDate and dateParser.age(res).year<0 then
		-- date is in the future
		isValidDate = false
		local title = mw.title.getCurrentTitle()
		if title.namespace==0 then
			errors = Errors['future-date']
		else
			errors = '<!--' .. Errors['future-date'] .. '-->'
		end
	elseif not isValidDate or res.precision < dateParser.PRECISION.YEAR then
		-- date is either invalid or is imprecise
		isValidDate = false -- mark as invalid anyway, even if "only" imprecise
		errors = frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=Date,
			['מחרוזת']=Date
			} }
	end
	return isValidDate, res, errors
end

function consistentWithWikidata(localDate, wikidataClaim)
	local possibleDate, possibleDateValue, isConsistent
	if not  wikidataClaim then
		return true
	end
	isConsistent = true
	for i,possibleDate in ipairs(wikidataClaim) do
		if possibleDate.mainsnak and possibleDate.mainsnak.datavalue and possibleDate.mainsnak.datavalue.value then
			possibleDateValue = possibleDate.mainsnak.datavalue.value
			local iswikidataValid, wikidataDate = pcall(dateParser.newFromWikidataValue, possibleDateValue)
			if iswikidataValid and localDate.calendar == wikidataDate.calendar then
				if (localDate.year ~= wikidataDate.year and possibleDateValue.precision>=dateParser.PRECISION.YEAR) or 
				   (localDate.month ~= wikidataDate.month and possibleDateValue.precision>=dateParser.PRECISION.MONTH) or 
				   (localDate.day ~= wikidataDate.day and possibleDateValue.precision>=dateParser.PRECISION.DAY)  then
					isConsistent = false
				else
					isConsistent = true
					break
				end
	 		end
 		end
	end

 	return isConsistent
end

function ageCalcFromFrame(frame)
	local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true })
	--local age_string = args['מציין']
	local BirthDateStr = args['תאריך לידה']
	local DeathDateStr = args['תאריך פטירה']
	local DisplayForAlive = args['הצג אם בחיים']
	local DisplayForDead = args['הצג אם נפטר']
	local Brackets = args['סוגריים']
	local ApproxDate = args['מקורב']
	
	DisplayForAlive = (DisplayForAlive=='כן')
	DisplayForDead = (DisplayForDead=='כן')
	Brackets = (Brackets~='לא')
	return ageCalc(BirthDateStr, DeathDateStr, DisplayForAlive, DisplayForDead, Brackets, ApproxDate)
end	

function ageCalc(BirthDateStr, DeathDateStr, DisplayForAlive, DisplayForDead, Brackets, ApproxDate, gender)
	local frame = mw.getCurrentFrame()
	local warnings = {}
	local dateRangeStr
	local wikidataBirth, wikidataDeath
	if BirthDateStr == '' then BirthDateStr = nil end
	if DeathDateStr == '' then DeathDateStr = nil end	
	local entityId = mw.wikibase.getEntityIdForCurrentPage()
	wikidataBirth = entityId and mw.wikibase.getBestStatements( entityId, 'P569' )
	wikidataDeath = entityId and wikidataBirth and mw.wikibase.getBestStatements( entityId, 'P570' )

	BirthDateStr = BirthDateStr or ((wikidataBirth and #wikidataBirth>0 and mw.wikibase.renderSnak( wikidataBirth[1].mainsnak ) or nil))
	-- validate dates are valid
	if not BirthDateStr then
		return -- missing date, nothing to do
	end
	DeathDateStr = DeathDateStr or ((wikidataDeath and #wikidataDeath>0 and mw.wikibase.renderSnak( wikidataDeath[1].mainsnak ) or nil))
	if DeathDateStr =='' then DeathDateStr = nil end
	local IsDead = (DeathDateStr~=nil)
	
	-- early termination
	if (DisplayForDead~=IsDead) and (DisplayForAlive==IsDead) then
		return ''
	end

	local isValidDate, BirthDate, errDate = ToDate(frame, BirthDateStr, ApproxDate)
	if not isValidDate  then
		return errDate
	end
	if wikidataBirth and wikidataBirth[1] and wikidataBirth[1].mainsnak  then
		if not consistentWithWikidata(BirthDate, wikidataBirth) then
				table.insert(warnings, '[[קטגוריה:דפים עם שנות חיים שלא תואמים את ויקינתונים]]')
		 end
	end
	
	-- skip age calculation if we dont have enough precision
	if BirthDate.precision < dateParser.PRECISION.YEAR then return nil end
	
	local DeathDate
	if IsDead then 
		isValidDate, DeathDate, errDate =  ToDate(frame, DeathDateStr, ApproxDate)
		if not isValidDate then
			return errDate-- invalid death date
		end
	
		if not consistentWithWikidata(DeathDate, wikidataDeath) then
				table.insert(warnings, '[[קטגוריה:דפים עם שנות חיים שלא תואמים את ויקינתונים]]')
		 end
		prefix = 'בגיל ' 
		
	else
		if gender == nil and entityId~=nil then
			local PropertyLink = require('Module:PropertyLink')
			gender = PropertyLink.getItem( 'P21', entityId )
			if (gender == "Q6581097") then		-- male
				gender = 'זכר'
			elseif (gender == "Q2449503") then	-- transgender male
				gender = 'זכר'
			elseif (gender == "Q15145778") then	-- cisgender male
				gender = 'זכר'
			elseif (gender == "Q6581072") then	-- female
				gender = 'נקבה'
			elseif (gender == "Q1052281") then	-- transgender female
				gender = 'נקבה'
			elseif (gender == "Q15145779") then	-- cisgender female
				gender = 'נקבה'
			end
		end
		
		prefix = 'גיל:&nbsp;'

		if gender == 'נקבה' then
			prefix = 'בת&nbsp;'
		elseif gender == 'זכר' then
			prefix = 'בן&nbsp;'
		end
	end
	-- skip age calculation if we dont have enough precision
	if DeathDate and DeathDate.precision and DeathDate.precision < dateParser.PRECISION.YEAR then return nil end
	
	local success, Age	 = pcall(dateParser.age, BirthDate , DeathDate)
	
	if success then
	AgeYears = Age.year
		if Age.year <= 0 then
			if Age.month == 1 then
				result = prefix..'חודש'
			elseif Age.month == 2 then
				result  = prefix..'חודשיים'
			else
				result = prefix..tostring(Age.month)..' חודשים'
			end
			if Age.day > 0 then
				if Age.day == 1 then
					result = result..' ויום'
				elseif Age.day == 2 then
					result = result..' ויומיים'
				else
					result = result..'  ו־'..tostring(Age.day)..' ימים'
				end
			end
		elseif Age.year < 3 then
			if Age.year == 1 then
				result = prefix..'שנה'
			elseif Age.year == 2 then
				result = prefix..'שנתיים'
			end
			if Age.month == 0 then
				if Age.day > 0 then
					if Age.day == 1 then
						result = result..' ויום'
					elseif Age.day == 2 then
						result = result..' ויומיים'
					else
						result = result..'  ו־'..tostring(Age.day)..' ימים'
					end
				end
			elseif Age.month == 1 then
				result = result..' וחודש'
			elseif Age.month == 2 then
				result  = result..' וחודשיים'
			else
				result = result..' ו־'..tostring(Age.month)..' חודשים'
			end
		else
			result = prefix..tostring(Age.year)
		end

		
		if BirthDate.precision < dateParser.PRECISION.MONTH or (DeathDate and DeathDate.precision < dateParser.PRECISION.MONTH) then
			result = result .. ' בערך'
		end
		
		-- ignore warning in specific template
		local noAgeWarning = false
		if mw.getCurrentFrame():getParent():getTitle() == 'תבנית:אישיות תנ"כית' or mw.getCurrentFrame():getParent():getTitle() == 'תבנית:בעל חיים מפורסם' then
			noAgeWarning = true
		end
		local title = mw.title.getCurrentTitle()
		-- ignore warning in other namespace except the main NS
		if title.namespace==0 and not noAgeWarning then
			if AgeYears > 139 then 
				local instanceOf = entityId and mw.wikibase.getBestStatements( entityId, 'P31' )
				local isHuman = instanceOf and instanceOf[1].mainsnak and instanceOf[1].mainsnak.datavalue.value.id=='Q5'
				if isHuman then
					table.insert(warnings, Errors['future-date'])
				end
			elseif AgeYears>109 then 
				table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל 110]]')
			elseif AgeYears>99 then
				table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל מאה]]')
			end
		end
	else
		result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=DeathDate,
			['מחרוזת']=dateRangeStr
			} }
	end
	
	warnings = table.concat( warnings, '')
	if #warnings > 0 then
		warnings = string.format('%s ', warnings)
	end
	if Brackets then
		result = string.format(' (%s)', result)
	end
	return string.format('%s<span style="white-space: nowrap;">%s</span>', warnings, result)
end


return {
	['גיל'] = ageCalcFromFrame,
	ageCalc = ageCalc
}