לדלג לתוכן

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

מתוך צפונות ויקי
מ ביטול גרסה 18237664 של IKhitron (שיחה), בגלל תקציר שגוי אתה מקלקל?
כן, כי מהתקציר נובע שהשארת רווח בטעות. אני לא יכול לנחש שהתקציר שגוי :-) ביטול גרסה 18237991 של חזרתי (שיחה)
שורה 78: שורה 78:
return errDeathDate-- invalid death date
return errDeathDate-- invalid death date
end
end
prefix = 'בגיל '
prefix = 'בגיל '  
dateRangeStr = string.format('%s - %s', BirthDate, DeathDate)
dateRangeStr = string.format('%s - %s', BirthDate, DeathDate)
else
else

גרסה מ־14:14, 6 במרץ 2016

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

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

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

function invalidDate(frame, Date, ApproxDate)
	local isValidDate, diffDate = pcall(dateParser.parseDateRange, Date, 'raw', false)
	if not isValidDate then
		-- invalid date
		return frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=Date,
			['מחרוזת']=Date
			} }
	end
	if tonumber(diffDate)<0 then 
		local title = mw.title.getCurrentTitle()
		if title.namespace==0 then
			return Errors['future-date']-- date is in the future
		else
			return '<!--' .. Errors['future-date'] .. '-->'
		end
	end
	
end

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

	-- validate dates are valid
	if BirthDate==nil or BirthDate=='' then
		return -- missing date, nothing to do
	end
	
	if DeathDate=='' or DeathDate==nil then
		-- pull the death date from wikidata
		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
	if (DisplayForDead~=IsDead) and (DisplayForAlive==IsDead) then
		return ''
	end
	
	local errBirthDate = invalidDate(frame, BirthDate, ApproxDate)
	if errBirthDate  then
		return errBirthDate
	end

	if IsDead then 
		local errDeathDate = invalidDate(frame, DeathDate, ApproxDate)
		if errDeathDate then
			return errDeathDate-- invalid death date
		end
		prefix = 'בגיל ' 
		dateRangeStr = string.format('%s - %s', BirthDate, DeathDate)
	else
		prefix = frame:expandTemplate{ title = 'לפי מגדר/בדוק', args = { 'בן&nbsp;', 'בת&nbsp;' } } 
		dateRangeStr = BirthDate
	end

	local success, result = pcall(dateParser.parseDateRange, dateRangeStr , {'years'}, true)

	if success then
		result= mw.ustring.gsub(result,"כ־(.+) שנים","%1 בערך")
		result = prefix..mw.ustring.gsub(result," שנים","")
		local success, AgeYears = pcall(dateParser.parseDateRange, dateRangeStr , 'raw', true)
		local YearToSeconds = 60*60*24*365
		AgeYears = tonumber(AgeYears)/YearToSeconds
		if AgeYears > 139 then 
			table.insert(warnings, Errors['future-date'])
		elseif AgeYears>109 then 
			table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל 110]]')
		elseif AgeYears>100 then
			table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל מאה]]')
		end
	else

		result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=DeathDate,
			['מחרוזת']=dateRangeStr
			} }
	end

	warnings = table.concat( warnings, '')
	if Brackets then
		result = string.format(' (%s)', result)
	end
	return string.format('%s <span style="white-space: nowrap;">%s</span>', warnings, result)
end


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