יחידה:גיל לערכי אישים: הבדלים בין גרסאות בדף
מראה
מאין תקציר עריכה |
|||
| שורה 28: | שורה 28: | ||
function ageCalc(frame) | function ageCalc(frame) | ||
local args = Arguments.getArgs(frame) | local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true }) | ||
local BirthDate = args['תאריך לידה'] | local BirthDate = args['תאריך לידה'] | ||
local DeathDate = args['תאריך פטירה'] | local DeathDate = args['תאריך פטירה'] | ||
| שורה 42: | שורה 42: | ||
local entity = mw.wikibase.getEntityObject() | local entity = mw.wikibase.getEntityObject() | ||
BirthDate = BirthDate or (entity and entity.claims and entity.claims['P569'] and entity:formatPropertyValues( 'P569' ).value) | |||
-- validate dates are valid | -- validate dates are valid | ||
if BirthDate | if not BirthDate then | ||
return -- missing date, nothing to do | return -- missing date, nothing to do | ||
end | end | ||
DeathDate = DeathDate or (entity and entity.claims and entity.claims['P570'] and entity:formatPropertyValues( 'P570' ).value) | |||
local IsDead = (DeathDate~=nil and DeathDate~='') | local IsDead = (DeathDate~=nil and DeathDate~='') | ||
גרסה מ־22:31, 15 במרץ 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, { ['trim'] = true, ['removeBlanks'] = true })
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()
BirthDate = BirthDate or (entity and entity.claims and entity.claims['P569'] and entity:formatPropertyValues( 'P569' ).value)
-- validate dates are valid
if not BirthDate then
return -- missing date, nothing to do
end
DeathDate = DeathDate or (entity and entity.claims and entity.claims['P570'] and entity:formatPropertyValues( 'P570' ).value)
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 = { 'בן ', 'בת ' } }
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
}