Module:Age/sandbox: Difference between revisions

Content deleted Content added
m Protected "Module:Age/sandbox": High-risk template or module: 1196 transclusions (more info) ([Edit=Require autoconfirmed or confirmed access] (indefinite))
strict checking of df=xxx parameter value to reduce wikitext junk; see Module talk:Age#df and mf with bad values
 
(2 intermediate revisions by 2 users not shown)
Line 4:
-- Message and other text that should be localized.
-- Also need to localize text in table names in function dateDifference.
['mt-bad-param1'] = 'Invalid parameter $1',
['mt-bad-param2'] = 'Parameter $1=$2 is invalid',
['mt-bad-show'] = 'Parameter show=$1 is not supported here',
Line 26 ⟶ 25:
['mt-template-bad-name'] = 'The specified template name is not valid',
['mt-template-x'] = 'The template invoking this must have "|template=x" where x is the wanted operation',
['mt-badwarn-param1'] = 'Invalid parameter $1',
['mt-warn-param2'] = 'Parameter $1=$2 is invalid',
['txt-affirmative'] = { y = true, yes = true, Y = true, Yes = true, YES = true }, -- valid values for df + mf parameters
['txt-yes'] = { y = true, yes = true, on = true }, -- valid values for parameters introduced with this module
['txt-and'] = ' and ',
['txt-or'] = ' or ',
Line 44 ⟶ 47:
 
local isWarning = {
['mt-badwarn-param1'] = true,
['mt-warn-param2'] = true,
}
 
-- Returnyes[parameter] is true if parameter should be interpreted as "yes".
-- Do not want to accept mixed upper/lowercase unless done by currentpreviously used templates.
-- Need to accept "on" because "round=on" is wanted.
local defaultyes = mtext['txt-format-defaultyes']
 
local translate, from_en, to_en, isZero
Line 116 ⟶ 125:
end
return text
end
 
local function dateFormat(args)
-- Return string for wanted date format.
local default = mtext['txt-format-default']
local other = default == 'df' and 'mf' or 'df'
local wanted = stripToNil(args[other]) and other or default
return wanted == 'df' and mtext['txt-dmy'] or mtext['txt-mdy']
end
 
Line 129 ⟶ 130:
-- Return text after substituting any given parameters for $1, $2, etc.
return mw.message.newRawMessage(text, ...):plain()
end
 
local function yes(parameter)
-- Return true if parameter should be interpreted as "yes".
-- Do not want to accept mixed upper/lowercase unless done by current templates.
-- Need to accept "on" because "round=on" is wanted.
return ({ y = true, yes = true, on = true })[parameter]
end
 
Line 167 ⟶ 161:
b ..
(category or '')
end
 
local function dateFormat(args)
-- Return
-- nil, f if parameter is valid
-- m, f otherwise
-- where
-- m = string for warning message with category
-- Return f = string for wanted date format.
local problem
local wanted = mtext['txt-format-default']
local other = defaultwanted == 'df' and 'mf' or 'df'
local wantedparm = stripToNil(args[other]) and other or default''
if mtext['txt-affirmative'][parm] then
wanted = other
elseif parm ~= '' then
problem = message('mt-warn-param2', other, parm)
end
return problem, wanted == 'df' and mtext['txt-dmy'] or mtext['txt-mdy']
end
 
Line 331 ⟶ 344:
parms[i] = v
end
if yes([args.fix)] then
table.insert(parms, 'fix')
end
if yes([args.partial)] then
table.insert(parms, 'partial')
end
Line 473 ⟶ 486:
},
abbr_on = {
sep = ' ',
y = 'y',
m = 'm',
Line 754 ⟶ 766:
age_infant = { -- {{age for infant}}
-- Do not set show because special processing is done later.
abbr = yes([args.abbr)] and 'abbr_infant' or 'abbr_off',
disp = 'disp_age',
sep = 'sep_space',
Line 817 ⟶ 829:
-- ("on" is equivalent to "yes", and "off" is equivalent to "no").
-- "|range=OTHER" sets range = nil and rejects partial dates.
range = ({ dash = 'dash', off = 'no', no = 'no', [true] = true })[range] or yes([range)]
if range then
partial = true -- accept partial dates with a possible age range for the result
Line 827 ⟶ 839:
end
local getopt = {
fix = yes([args.fix)],
flag = stripToNil(args.flag) or spec.flag,
omitZero = spec.omitZero,
Line 846 ⟶ 858:
local parms = {
diff = date2:subtract(date1, { fill = autofill }),
wantDuration = spec.duration or yes([args.duration)],
range = range,
wantSc = yes([args.sc)],
show = args.show == 'hide' and 'hide' or spec.show,
abbr = spec.abbr,
Line 854 ⟶ 866:
extra = makeExtra(args, getopt.usesCurrent and format ~= 'format_raw'),
format = format or spec.format,
round = yes([args.round)],
sep = spec.sep,
sortable = translateParameters.sortable[args.sortable or spec.sortable],
Line 901 ⟶ 913:
end
end
local problem, format = dateFormat(args)
local result = substituteParameters(
mtext['txt-bda'],
date:text('%-Y-%m-%d'),
from_en(date:text(dateFormat(args)format)),
from_en(dateDifference({
diff = diff,
Line 912 ⟶ 925:
sep = 'sep_space',
}))
) .. (problem or '')
)
local warnings = tonumber(frame.args.warnings)
if warnings and warnings > 0 then
Line 941 ⟶ 954:
end
if invalid then
result = result .. message('mt-badwarn-param1', invalid)
end
end
Line 982 ⟶ 995:
return message('mt-invalid-dates-age')
end
local fmt_date, fmt_ymd, problem
if date1.day then -- y, m, d known
problem, fmt_date = dateFormat(args)
fmt_ymd = '%-Y-%m-%d'
elseif date1.month then -- y, m known; d unknown
Line 1,011 ⟶ 1,024:
sep = 'sep_space',
}))
) .. (problem or '')
)
local warnings = tonumber(frame.args.warnings)
if warnings and warnings > 0 then
Line 1,034 ⟶ 1,047:
end
if invalid then
result = result .. message('mt-badwarn-param1', invalid)
end
end
Line 1,087 ⟶ 1,100:
local parms = {
extra = makeExtra(args),
wantDuration = yes([args.duration)],
range = yes([args.range)] or (args.range == 'dash' and 'dash' or nil),
wantSc = yes([args.sc)],
}
local fix = yes([args.fix)] and 'fix' or ''
local date1 = Date(fix, 'partial', stripToNil(args[1]) or 'currentdatetime')
if not date1 then
Line 1,126 ⟶ 1,139:
end
return from_en(dateDifference(parms))
end
 
local function yestemplateGeneric(parameterframe)
local name = frame.args.template
if not name then
return message('mt-template-x')
end
return ageGeneric(frame:newChild{title = mw.title.new(name, 10), args = frame.args})
end
 
Line 1,137 ⟶ 1,158:
JULIANDAY = dateToJd, -- Template:JULIANDAY
time_interval = timeInterval, -- Template:Time_interval
[''] = templateGeneric, -- same as age_generic, but can be invoked directly
}