יחידה:גיל לערכי אישים: הבדלים בין גרסאות בדף
מראה
מאין תקציר עריכה |
מאין תקציר עריכה |
||
| שורה 6: | שורה 6: | ||
} | } | ||
function | function ToDate(frame, Date, ApproxDate) | ||
local isValidDate, | local isValidDate, res = pcall(dateParser.newFromWikitext, Date) | ||
local errors = '' | |||
if not isValidDate then | if not isValidDate then | ||
-- invalid date | -- invalid date | ||
errors = frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { | |||
['מקורב']=ApproxDate, | ['מקורב']=ApproxDate, | ||
['נוסף']=Date, | ['נוסף']=Date, | ||
['מחרוזת']=Date | ['מחרוזת']=Date | ||
} } | } } | ||
-- validate | |||
elseif dateParser.age(res)<0 then | |||
isValidDate = false | |||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
if title.namespace==0 then | if title.namespace==0 then | ||
errors = Errors['future-date']-- date is in the future | |||
else | else | ||
errors = '<!--' .. Errors['future-date'] .. '-->' | |||
end | end | ||
end | end | ||
return isValidDate, res, errors | |||
end | end | ||
function ageCalc(frame) | function ageCalc(frame) | ||
local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true }) | local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true }) | ||
local | local BirthDateStr = args['תאריך לידה'] | ||
local | local DeathDateStr = args['תאריך פטירה'] | ||
local DisplayForAlive = args['הצג אם בחיים'] | local DisplayForAlive = args['הצג אם בחיים'] | ||
local DisplayForDead = args['הצג אם נפטר'] | local DisplayForDead = args['הצג אם נפטר'] | ||
| שורה 42: | שורה 45: | ||
local entity = mw.wikibase.getEntityObject() | local entity = mw.wikibase.getEntityObject() | ||
BirthDateStr = BirthDateStr or (entity and entity.claims and entity.claims['P569'] and entity:formatPropertyValues( 'P569' ).value) | |||
-- validate dates are valid | -- validate dates are valid | ||
if not | if not BirthDateStr then | ||
return -- missing date, nothing to do | return -- missing date, nothing to do | ||
end | end | ||
DeathDateStr = DeathDateStr or (entity and entity.claims and entity.claims['P570'] and entity:formatPropertyValues( 'P570' ).value) | |||
if DeathDateStr =='' then DeathDateStr = nil end | |||
local IsDead = ( | local IsDead = (DeathDateStr~=nil) | ||
-- early termination | -- early termination | ||
| שורה 57: | שורה 59: | ||
end | end | ||
local | local isValidDate, BirthDate, errDate = ToDate(frame, BirthDateStr, ApproxDate) | ||
if | if not isValidDate then | ||
return | return errDate | ||
end | end | ||
local DeathDate | |||
if IsDead then | if IsDead then | ||
isValidDate, DeathDate, errDate = ToDate(frame, DeathDateStr, ApproxDate) | |||
if | if not isValidDate then | ||
return | return errDate-- invalid death date | ||
end | end | ||
prefix = 'בגיל ' | prefix = 'בגיל ' | ||
else | else | ||
prefix = frame:expandTemplate{ title = 'לפי מגדר/בדוק', args = { 'בן ', 'בת ' } } | prefix = frame:expandTemplate{ title = 'לפי מגדר/בדוק', args = { 'בן ', 'בת ' } } | ||
end | end | ||
local success, | local success, AgeYears = pcall(dateParser.age, BirthDate , DeathDate) | ||
if success then | if success then | ||
result = prefix..tostring(AgeYears) | |||
result = prefix.. | |||
if AgeYears > 139 then | if AgeYears > 139 then | ||
table.insert(warnings, Errors['future-date']) | table.insert(warnings, Errors['future-date']) | ||
| שורה 90: | שורה 87: | ||
end | end | ||
else | else | ||
result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { | result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { | ||
['מקורב']=ApproxDate, | ['מקורב']=ApproxDate, | ||
גרסה מ־22:03, 22 במרץ 2016
ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:גיל לערכי אישים/תיעוד
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)<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 ageCalc(frame)
local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true })
local BirthDateStr = args['תאריך לידה']
local DeathDateStr = 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()
BirthDateStr = BirthDateStr or (entity and entity.claims and entity.claims['P569'] and entity:formatPropertyValues( 'P569' ).value)
-- validate dates are valid
if not BirthDateStr then
return -- missing date, nothing to do
end
DeathDateStr = DeathDateStr or (entity and entity.claims and entity.claims['P570'] and entity:formatPropertyValues( 'P570' ).value)
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
local DeathDate
if IsDead then
isValidDate, DeathDate, errDate = ToDate(frame, DeathDateStr, ApproxDate)
if not isValidDate then
return errDate-- invalid death date
end
prefix = 'בגיל '
else
prefix = frame:expandTemplate{ title = 'לפי מגדר/בדוק', args = { 'בן ', 'בת ' } }
end
local success, AgeYears = pcall(dateParser.age, BirthDate , DeathDate)
if success then
result = prefix..tostring(AgeYears)
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
}