Module:Citation/CS1/Date validation: Difference between revisions
synch from sandbox;
m>Trappist the monk No edit summary |
(synch from sandbox;) |
||
Line 569: | Line 569: | ||
for k, v in pairs(date_parameters_list) do -- for each date-holding parameter in the list | for k, v in pairs(date_parameters_list) do -- for each date-holding parameter in the list | ||
if is_set(v) then -- if the parameter has a value | if is_set(v.val) then -- if the parameter has a value | ||
if v:match("^c%. [1-9]%d%d%d?%a?$") then -- special case for c. year or with or without CITEREF disambiguator - only |date= and |year= | v.val = mw.ustring.gsub (v.val, '%d', cfg.date_names.local_digits); -- translate 'local' digits to Western 0-9 | ||
if v.val:match("^c%. [1-9]%d%d%d?%a?$") then -- special case for c. year or with or without CITEREF disambiguator - only |date= and |year= | |||
local year = v:match("c%. ([1-9]%d%d%d?)%a?"); -- get the year portion so it can be tested | local year = v.val:match("c%. ([1-9]%d%d%d?)%a?"); -- get the year portion so it can be tested | ||
if 'date'==k then | if 'date'==k then | ||
anchor_year, COinS_date = v:match("((c%. [1-9]%d%d%d?)%a?)"); -- anchor year and COinS_date only from |date= parameter | anchor_year, COinS_date = v.val:match("((c%. [1-9]%d%d%d?)%a?)"); -- anchor year and COinS_date only from |date= parameter | ||
good_date = is_valid_year(year); | good_date = is_valid_year(year); | ||
elseif 'year'==k then | elseif 'year'==k then | ||
Line 580: | Line 580: | ||
end | end | ||
elseif 'date'==k then -- if the parameter is |date= | elseif 'date'==k then -- if the parameter is |date= | ||
if v:match("^n%.d%.%a?$") then -- if |date=n.d. with or without a CITEREF disambiguator | if v.val:match("^n%.d%.%a?$") then -- if |date=n.d. with or without a CITEREF disambiguator | ||
good_date, anchor_year, COinS_date = true, v:match("((n%.d%.)%a?)"); --"n.d."; no error when date parameter is set to no date | good_date, anchor_year, COinS_date = true, v.val:match("((n%.d%.)%a?)"); --"n.d."; no error when date parameter is set to no date | ||
elseif v:match("^nd%a?$") then -- if |date=nd with or without a CITEREF disambiguator | elseif v.val:match("^nd%a?$") then -- if |date=nd with or without a CITEREF disambiguator | ||
good_date, anchor_year, COinS_date = true, v:match("((nd)%a?)"); --"nd"; no error when date parameter is set to no date | good_date, anchor_year, COinS_date = true, v.val:match("((nd)%a?)"); --"nd"; no error when date parameter is set to no date | ||
else | else | ||
good_date, anchor_year, COinS_date = check_date (v, tCOinS_date); -- go test the date | good_date, anchor_year, COinS_date = check_date (v.val, tCOinS_date); -- go test the date | ||
end | end | ||
elseif 'year'==k then -- if the parameter is |year= it should hold only a year value | elseif 'year'==k then -- if the parameter is |year= it should hold only a year value | ||
if v:match("^[1-9]%d%d%d?%a?$") then -- if |year= 3 or 4 digits only with or without a CITEREF disambiguator | if v.val:match("^[1-9]%d%d%d?%a?$") then -- if |year= 3 or 4 digits only with or without a CITEREF disambiguator | ||
good_date, anchor_year, COinS_date = true, v:match("((%d+)%a?)"); | good_date, anchor_year, COinS_date = true, v.val:match("((%d+)%a?)"); | ||
end | end | ||
elseif 'access-date'==k then -- if the parameter is |date= | elseif 'access-date'==k then -- if the parameter is |date= | ||
good_date = check_date (v, nil, true); -- go test the date; nil is a placeholder; true is the test_accessdate flag | good_date = check_date (v.val, nil, true); -- go test the date; nil is a placeholder; true is the test_accessdate flag | ||
elseif 'embargo'==k then -- if the parameter is |embargo= | elseif 'embargo'==k then -- if the parameter is |embargo= | ||
good_date = check_date (v); -- go test the date | good_date = check_date (v.val); -- go test the date | ||
if true == good_date then -- if the date is a valid date | if true == good_date then -- if the date is a valid date | ||
good_date, embargo_date = is_valid_embargo_date (v); -- is |embargo= date a single dmy, mdy, or ymd formatted date? yes:returns embargo; no: returns 9999 | good_date, embargo_date = is_valid_embargo_date (v.val); -- is |embargo= date a single dmy, mdy, or ymd formatted date? yes:returns embargo; no: returns 9999 | ||
end | end | ||
else -- any other date-holding parameter | else -- any other date-holding parameter | ||
good_date = check_date (v); -- go test the date | good_date = check_date (v.val); -- go test the date | ||
end | end | ||
if false==good_date then -- assemble one error message so we don't add the tracking category multiple times | if false==good_date then -- assemble one error message so we don't add the tracking category multiple times | ||
Line 605: | Line 605: | ||
error_message=error_message .. ", "; -- ... add a comma space separator | error_message=error_message .. ", "; -- ... add a comma space separator | ||
end | end | ||
error_message=error_message .. "|" .. | error_message=error_message .. "|" .. v.name .. "="; -- add the failed parameter | ||
end | end | ||
end | end | ||
Line 753: | Line 753: | ||
for param_name, param_val in pairs (date_parameters_list) do -- for each date-holding parameter in the list | for param_name, param_val in pairs (date_parameters_list) do -- for each date-holding parameter in the list | ||
if is_set (param_val) then -- if the parameter has a value | if is_set (param_val.val) then -- if the parameter has a value | ||
if not all and in_array (param_name, {'access-date', 'archive-date'}) then -- if access- or archive-date and format not xxx-all | if not all and in_array (param_name, {'access-date', 'archive-date'}) then -- if access- or archive-date and format not xxx-all | ||
param_val = ''; -- set to empty string so we don't process this date | param_val.val = ''; -- set to empty string so we don't process this date | ||
end | end | ||
for source, pattern in pairs (source_patterns) do | for source, pattern in pairs (source_patterns) do | ||
if param_val:match (pattern) then | if param_val.val:match (pattern) then | ||
if 'ymd' == source then | if 'ymd' == source then | ||
get_ymd_date_parts (param_val, source_date); -- get the date parts into the source_date table | get_ymd_date_parts (param_val.val, source_date); -- get the date parts into the source_date table | ||
elseif 'dmy' == source then | elseif 'dmy' == source then | ||
get_dmy_date_parts (param_val, source_date); -- get the date parts into the source_date table | get_dmy_date_parts (param_val.val, source_date); -- get the date parts into the source_date table | ||
elseif 'mdy' == source then | elseif 'mdy' == source then | ||
get_mdy_date_parts (param_val, source_date); -- get the date parts into the source_date table | get_mdy_date_parts (param_val.val, source_date); -- get the date parts into the source_date table | ||
end | end | ||
Line 777: | Line 777: | ||
end | end | ||
-- convert date and save; | -- convert date and save; | ||
date_parameters_list[param_name] = mw.text.trim (os.date (format_str, os.time(source_date))); -- strip leading space when single digit day and %e is first format | date_parameters_list[param_name].val = mw.text.trim (os.date (format_str, os.time(source_date))); -- strip leading space when single digit day and %e is first format | ||
end | end | ||
end | end | ||
Line 799: | Line 799: | ||
local n; | local n; | ||
for param_name, param_val in pairs(date_parameters_list) do -- for each date-holding parameter in the list | for param_name, param_val in pairs(date_parameters_list) do -- for each date-holding parameter in the list | ||
if not | if is_set (param_val.val) then | ||
if not mw.ustring.match (param_val.val, '%d%d%d%d%-%d%d%-%d%d') then -- for those that are not ymd dates (ustring because here digits may not be western) | |||
param_val.val, n = param_val.val:gsub ('%-', '–'); -- replace any hyphen with ndash | |||
if 0 ~= n then | |||
date_parameters_list[param_name].val = param_val.val; -- update the list | |||
result = true; | |||
end | |||
end | end | ||
end | end | ||
Line 815: | Line 817: | ||
Attempts to translate English month names to local-language month names using names supplied by MediaWiki's | Attempts to translate English month names to local-language month names using names supplied by MediaWiki's | ||
date parser function. This is simple name-for-name replacement and may not work for all languages. | date parser function. This is simple name-for-name replacement and may not work for all languages. | ||
if xlat_dig is true, this function will also translate western (English) digits to the local language's digits. | |||
This will also translate ymd dates. | |||
]] | ]] | ||
local function date_name_xlate (date_parameters_list) | local function date_name_xlate (date_parameters_list, xlt_dig) | ||
local xlate; | local xlate; | ||
local mode; -- long or short month names | local mode; -- long or short month names | ||
Line 825: | Line 830: | ||
for param_name, param_val in pairs(date_parameters_list) do -- for each date-holding parameter in the list | for param_name, param_val in pairs(date_parameters_list) do -- for each date-holding parameter in the list | ||
if is_set(param_val) then | if is_set(param_val.val) then -- if the parameter has a value | ||
date = param_val; | date = param_val.val; | ||
for month in mw.ustring.gmatch (date, '%a+') do -- iterate through all dates in the date (single date or date range) | for month in mw.ustring.gmatch (date, '%a+') do -- iterate through all dates in the date (single date or date range) | ||
if cfg.date_names.en.long[month] then | if cfg.date_names.en.long[month] then | ||
Line 839: | Line 844: | ||
xlate = mw.getContentLanguage():formatDate(mode, '1' .. month); -- translate the month name to this local language | xlate = mw.getContentLanguage():formatDate(mode, '1' .. month); -- translate the month name to this local language | ||
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] = date; | date_parameters_list[param_name].val = date; -- save the translated date | ||
modified = true; | modified = true; | ||
end | end | ||
end | end | ||
if xlt_dig then -- shall we also translate digits? | |||
date = date:gsub ('%d', cfg.date_names.xlate_digits); -- translate digits from western to 'local digits' | |||
date_parameters_list[param_name].val = date; -- save the translated date | |||
modified = true; | |||
end | |||
end | end | ||
end | end |