יחידה:קטגוריות לתאריך משולב

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:קטגוריות לתאריך משולב/תיעוד

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 not isValidDate then
		-- invalid date
		errors = frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=Date,
			['מחרוזת']=Date
			} }
	
	-- validate
	elseif dateParser.age(res).year<0 then 
		isValidDate = false
		local title = mw.title.getCurrentTitle()
		if title.namespace==0 then
			errors = Errors['future-date']-- date is in the future
		else
			errors = '<!--' .. Errors['future-date'] .. '-->'
		end
	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 ageCalc(frame)
	local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true })
	local BirthDateStr = entity and entity.claims and entity.claims['P569'] and entity:getBestStatements('P569')
	local DeathDateStr =  entity and entity.claims and entity.claims['P569'] and entity:getBestStatements('P570')
	local DisplayForAlive = args['הצג אם בחיים']
	local DisplayForDead = args['הצג אם נפטר']
	local Brackets = args['סוגריים']
	local ApproxDate = args['מקורב']
	local warnings = {}
	local dateRangeStr
	local wikidataBirth, wikidataDeath
	DisplayForAlive = (DisplayForAlive=='כן')
	DisplayForDead = (DisplayForDead=='כן')
	Brackets = (Brackets~='לא')
	
	local entity = mw.wikibase.getEntityObject()
	wikidataBirth = entity and entity.claims and entity.claims['P569'] and entity:getBestStatements('P569')
	wikidataDeath = entity and entity.claims and entity.claims['P569'] and entity:getBestStatements('P570')

	BirthDateStr = BirthDateStr or (wikidataBirth and entity:formatPropertyValues( 'P569' ).value)
	BirthDateStr = BirthDateStr and mw.ustring.gsub(BirthDateStr, "(.-),.*", "%1") -- use only the first value returned
	-- validate dates are valid
	if not BirthDateStr then
		return -- missing date, nothing to do
	end
	
	DeathDateStr = DeathDateStr or (wikidataDeath and entity:formatPropertyValues( 'P570' ).value)
	DeathDateStr = mw.ustring.gsub(DeathDateStr, "(.-),.*", "%1") -- use only the first value returned
	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
	
	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
		prefix = frame:expandTemplate{ title = 'לפי מגדר/בדוק', args = { 'בן&nbsp;', 'בת&nbsp;' } } 
		
	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 == 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
		
		local title = mw.title.getCurrentTitle()
		-- ignore warning in other namespace except the main NS
		if title.namespace==0 then
			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
		end
	else
		result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=DeathDate,
			['מחרוזת']=dateRangeStr
			} }
	end

	warnings = table.concat(warnings,'')
	

	return string.format('%s', warnings)
end


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