Module:Citation/CS1/Date validation/sandbox: Difference between revisions
Richardpruen (talk | contribs) m 1 revision imported: Templates and CSS files |
imported>Trappist the monk No edit summary |
||
Line 1: | Line 1: | ||
--[[ | --[[ | ||
History of changes since last sync: | History of changes since last sync: 2023-01-14 | ||
2023-02-20: break out of for-loop early when date has been formatted; see Help_talk:Citation_Style_1#date_reformatting_tweak | |||
2023-02-21: detect |archive-date= / |archive-url= timestamp mismatch; see Help_talk:Citation_Style_1#Error_or_Maint_message_if_archive_date_doesn't_match_url | |||
2023-03-15: i18n tonumber() fix; see Help_talk:Citation_Style_1#i18n_date_handling | |||
]] | ]] | ||
Line 194: | Line 195: | ||
year = tonumber (year) or lang_object:parseFormattedNumber (year); -- convert to number for the comparison; | year = tonumber (year) or lang_object:parseFormattedNumber (year); -- convert to number for the comparison; | ||
if 'pmc-embargo-date' == param then -- special case for |pmc-embargo-date= | if 'pmc-embargo-date' == param then -- special case for |pmc-embargo-date= | ||
return year and (year <= tonumber(os.date("%Y"))+2) or false; -- years more than two years in the future are not accepted | return year and (year <= tonumber(os.date("%Y"))+2) or false; -- years more than two years in the future are not accepted | ||
Line 329: | Line 330: | ||
local date; -- one date or first date in a range | local date; -- one date or first date in a range | ||
local date2 = ''; -- end of range date | local date2 = ''; -- end of range date | ||
input.year = tonumber (input.year) or lang_object:parseFormattedNumber (input.year); -- language-aware tonumber() | |||
input.year2 = tonumber (input.year2) or lang_object:parseFormattedNumber (input.year2); -- COinS dates are pseudo-ISO 8601 so convert to Arabic numerals | |||
-- start temporary Julian / Gregorian calendar uncertainty detection | -- start temporary Julian / Gregorian calendar uncertainty detection | ||
local year = | local year = input.year; -- this temporary code to determine the extent of sources dated to the Julian/Gregorian | ||
local month = tonumber(input.month); -- interstice 1 October 1582 – 1 January 1926 | local month = tonumber(input.month); -- interstice 1 October 1582 – 1 January 1926 | ||
local day = tonumber (input.day); | local day = tonumber (input.day); | ||
Line 341: | Line 345: | ||
-- end temporary Julian / Gregorian calendar uncertainty detection | -- end temporary Julian / Gregorian calendar uncertainty detection | ||
if 1582 > | if 1582 > input.year or 20 < tonumber(input.month) then -- Julian calendar or season so &rft.date gets year only | ||
date = input.year; | date = input.year; | ||
if 0 ~= input.year2 and input.year ~= input.year2 then -- if a range, only the second year portion when not the same as range start year | if 0 ~= input.year2 and input.year ~= input.year2 then -- if a range, only the second year portion when not the same as range start year | ||
date = string.format ('%.4d/%.4d', | date = string.format ('%.4d/%.4d', input.year, input.year2) -- assemble the date range | ||
end | end | ||
if 20 < tonumber(input.month) then -- if season or proper-name date | if 20 < tonumber(input.month) then -- if season or proper-name date | ||
Line 368: | Line 372: | ||
end | end | ||
end | end | ||
tCOinS_date.rftdate = date; | tCOinS_date.rftdate = tostring(date); | ||
return; -- done | return; -- done | ||
end | end | ||
Line 655: | Line 659: | ||
end | end | ||
if 'access-date' == param then -- test | if 'access-date' == param then -- test access-date here because we have numerical date parts | ||
if 0 ~= year and 0 ~= month and 0 ~= day and -- all parts of a single date required | if 0 ~= year and 0 ~= month and 0 ~= day and -- all parts of a single date required | ||
0 == year2 and 0 == month2 and 0 == day2 then -- none of these; | 0 == year2 and 0 == month2 and 0 == day2 then -- none of these; access-date must not be a range | ||
if not is_valid_accessdate(year .. '-' .. month .. '-' .. day) then | if not is_valid_accessdate(year .. '-' .. month .. '-' .. day) then | ||
return false; -- return false when | return false; -- return false when access-date out of bounds | ||
end | end | ||
else | else | ||
return false; -- return false when | return false; -- return false when access-date is a range of two dates | ||
end | end | ||
end | end | ||
if 'archive-date' == param then -- test archive-date here because we have numerical date parts | |||
if not (0 ~= year and 0 ~= month and 0 ~= day and -- all parts of a single date required | |||
0 == year2 and 0 == month2 and 0 == day2) then -- none of these; archive-date must not be a range | |||
return false; -- return false when archive-date is a range of two dates | |||
end | |||
end | |||
local result=true; -- check whole dates for validity; assume true because not all dates will go through this test | local result=true; -- check whole dates for validity; assume true because not all dates will go through this test | ||
if 0 ~= year and 0 ~= month and 0 ~= day and 0 == year2 and 0 == month2 and 0 == day2 then -- YMD (simple whole date) | if 0 ~= year and 0 ~= month and 0 ~= day and 0 == year2 and 0 == month2 and 0 == day2 then -- YMD (simple whole date) | ||
Line 1,039: | Line 1,050: | ||
date_parameters_list[param_name].val = new_date; -- update date in date list | date_parameters_list[param_name].val = new_date; -- update date in date list | ||
result = true; -- and announce that changes have been made | result = true; -- and announce that changes have been made | ||
break; | |||
end | end | ||
end -- if | end -- if | ||
Line 1,116: | Line 1,128: | ||
xlate = is_xlateable (month); -- get translate <month>; returns translation or nil | xlate = is_xlateable (month); -- get translate <month>; returns translation or nil | ||
if xlate then | if xlate then | ||
date = mw.ustring.gsub (date, month, xlate); -- replace the English with the translation | date = mw.ustring.gsub (date, month, xlate); -- replace the English with the translation | ||
date_parameters_list[param_name].val = date; -- save the translated date | date_parameters_list[param_name].val = date; -- save the translated date | ||
Line 1,179: | Line 1,162: | ||
cfg = cfg_table_ptr; -- import tables from selected Module:Citation/CS1/Configuration | cfg = cfg_table_ptr; -- import tables from selected Module:Citation/CS1/Configuration | ||
end | |||
--[[--------------------------< A R C H I V E _ D A T E _ C H E C K >------------------------------------------ | |||
Compare value in |archive-date= with the timestamp in Wayback machine urls. Emits an error message when |archive-date= | |||
does not match the timestamp. | |||
]] | |||
local function archive_date_check (archive_date, archive_url_timestamp) | |||
local good, archive_date_ts = pcall (lang_object.formatDate, lang_object, 'Ymd', archive_date); -- |archive-date= value to YYYYMMDD format | |||
-- local archive_date_ts = lang_object:formatDate ('Ymd', archive_date); -- |archive-date= value to YYYYMMDD format | |||
if good then | |||
if not archive_url_timestamp:find (archive_date_ts, 1, true) then -- plain text find; begin search at position 1 | |||
set_message ('err_archive_date_url_ts_mismatch'); -- emit an error message | |||
end | |||
end | |||
end | end | ||
Line 1,186: | Line 1,188: | ||
return { -- return exported functions | return { -- return exported functions | ||
archive_date_check = archive_date_check, | |||
date_hyphen_to_dash = date_hyphen_to_dash, | |||
date_name_xlate = date_name_xlate, | |||
dates = dates, | dates = dates, | ||
reformat_dates = reformat_dates, | |||
set_selected_modules = set_selected_modules, | |||
year_date_check = year_date_check, | year_date_check = year_date_check, | ||
} | } |