Module:Citation/CS1/Date validation: Difference between revisions

Synch from sandbox;
m>Trappist the monk
(embargo date bug fix;)
m>Trappist the monk
(Synch from sandbox;)
Line 72: Line 72:
returns true and date value if that value has proper dmy, mdy, ymd format.
returns true and date value if that value has proper dmy, mdy, ymd format.


returns false and 9999 when date value is not proper format; assumes that when |embargo= is set, the editor
returns false and 9999 (embargoed forever) when date value is not proper format; assumes that when |embargo= is
intended to embargo a pmc but |embargo does not hold a single date.
set, the editor intended to embargo a pmc but |embargo= does not hold a single date.


]]
]]
Line 374: Line 374:
if 0 == month then return false; end -- return false if month text isn't one of the twelve months
if 0 == month then return false; end -- return false if month text isn't one of the twelve months
elseif date_string:match("^%a+ +[1-9]%d?–[1-9]%d?, +[1-9]%d%d%d%a?$") then -- month-initial day range: month day–day, year; days are separated by endash
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d?[%-][1-9]%d?, +[1-9]%d%d%d%a?$") then -- month-initial day range: month day–day, year; days are separated by endash
month, day, day2, anchor_year, year=string.match(date_string, "(%a+) +(%d%d?)–(%d%d?), +((%d%d%d%d)%a?)");
month, day, day2, anchor_year, year=mw.ustring.match(date_string, "(%a+) +(%d%d?)[%-](%d%d?), +((%d%d%d%d)%a?)");
if tonumber(day) >= tonumber(day2) then return false; end -- date range order is left to right: earlier to later; dates may not be the same;
if tonumber(day) >= tonumber(day2) then return false; end -- date range order is left to right: earlier to later; dates may not be the same;
month = get_month_number (month);
month = get_month_number (month);
Line 387: Line 387:
if 0 == month then return false; end -- return false if month text isn't one of the twelve months
if 0 == month then return false; end -- return false if month text isn't one of the twelve months


elseif date_string:match("^[1-9]%d?–[1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then -- day-range-initial: day–day month year; days are separated by endash
elseif mw.ustring.match(date_string, "^[1-9]%d?[%-][1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then -- day-range-initial: day–day month year; days are separated by endash
day, day2, month, anchor_year, year=string.match(date_string, "(%d%d?)–(%d%d?) +(%a+) +((%d%d%d%d)%a?)");
day, day2, month, anchor_year, year=mw.ustring.match(date_string, "(%d%d?)[%-](%d%d?) +(%a+) +((%d%d%d%d)%a?)");
if tonumber(day) >= tonumber(day2) then return false; end -- date range order is left to right: earlier to later; dates may not be the same;
if tonumber(day) >= tonumber(day2) then return false; end -- date range order is left to right: earlier to later; dates may not be the same;
month = get_month_number (month);
month = get_month_number (month);
Line 395: Line 395:
year2=year;
year2=year;


elseif date_string:match("^[1-9]%d? +%a+ – [1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then -- day initial month-day-range: day month - day month year; uses spaced endash
elseif mw.ustring.match(date_string, "^[1-9]%d? +%a+ [%-] [1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then -- day initial month-day-range: day month - day month year; uses spaced endash
day, month, day2, month2, anchor_year, year=date_string:match("(%d%d?) +(%a+) – (%d%d?) +(%a+) +((%d%d%d%d)%a?)");
day, month, day2, month2, anchor_year, year=mw.ustring.match(date_string, "(%d%d?) +(%a+) [%-] (%d%d?) +(%a+) +((%d%d%d%d)%a?)");
if (not is_valid_month_season_range(month, month2)) or not is_valid_year(year) then return false; end -- date range order is left to right: earlier to later;
if (not is_valid_month_season_range(month, month2)) or not is_valid_year(year) then return false; end -- date range order is left to right: earlier to later;
month = get_month_number (month); -- for metadata
month = get_month_number (month); -- for metadata
Line 402: Line 402:
year2=year;
year2=year;


elseif date_string:match("^%a+ +[1-9]%d? – %a+ +[1-9]%d?, +[1-9]%d%d%d?%a?$") then -- month initial month-day-range: month day – month day, year;  uses spaced endash
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d? [%-] %a+ +[1-9]%d?, +[1-9]%d%d%d?%a?$") then -- month initial month-day-range: month day – month day, year;  uses spaced endash
month, day, month2, day2, anchor_year, year=date_string:match("(%a+) +(%d%d?) – (%a+) +(%d%d?), +((%d%d%d%d)%a?)");
month, day, month2, day2, anchor_year, year=mw.ustring.match(date_string, "(%a+) +(%d%d?) [%-] (%a+) +(%d%d?), +((%d%d%d%d)%a?)");
if (not is_valid_month_season_range(month, month2)) or not is_valid_year(year) then return false; end
if (not is_valid_month_season_range(month, month2)) or not is_valid_year(year) then return false; end
month = get_month_number (month); -- for metadata
month = get_month_number (month); -- for metadata
Line 409: Line 409:
year2=year;
year2=year;


elseif date_string:match("^[1-9]%d? +%a+ +[1-9]%d%d%d – [1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then -- day initial month-day-year-range: day month year - day month year; uses spaced endash
elseif mw.ustring.match(date_string, "^[1-9]%d? +%a+ +[1-9]%d%d%d [%-] [1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then -- day initial month-day-year-range: day month year - day month year; uses spaced endash
day, month, year, day2, month2, anchor_year, year2=date_string:match("(%d%d?) +(%a+) +(%d%d%d%d?) – (%d%d?) +(%a+) +((%d%d%d%d?)%a?)");
day, month, year, day2, month2, anchor_year, year2=mw.ustring.match(date_string, "(%d%d?) +(%a+) +(%d%d%d%d?) [%-] (%d%d?) +(%a+) +((%d%d%d%d?)%a?)");
if tonumber(year2) <= tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
if tonumber(year2) <= tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
if not is_valid_year(year2) or not is_valid_month_range_style(month, month2) then return false; end -- year2 no more than one year in the future; months same style
if not is_valid_year(year2) or not is_valid_month_range_style(month, month2) then return false; end -- year2 no more than one year in the future; months same style
Line 416: Line 416:
month2 = get_month_number (month2);
month2 = get_month_number (month2);


elseif date_string:match("^%a+ +[1-9]%d?, +[1-9]%d%d%d – %a+ +[1-9]%d?, +[1-9]%d%d%d%a?$") then -- month initial month-day-year-range: month day, year – month day, year;  uses spaced endash
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d?, +[1-9]%d%d%d [%-] %a+ +[1-9]%d?, +[1-9]%d%d%d%a?$") then -- month initial month-day-year-range: month day, year – month day, year;  uses spaced endash
month, day, year, month2, day2, anchor_year, year2=date_string:match("(%a+) +(%d%d?), +(%d%d%d%d) – (%a+) +(%d%d?), +((%d%d%d%d)%a?)");
month, day, year, month2, day2, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +(%d%d?), +(%d%d%d%d) [%-] (%a+) +(%d%d?), +((%d%d%d%d)%a?)");
if tonumber(year2) <= tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
if tonumber(year2) <= tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
if not is_valid_year(year2) or not is_valid_month_range_style(month, month2) then return false; end -- year2 no more than one year in the future; months same style
if not is_valid_year(year2) or not is_valid_month_range_style(month, month2) then return false; end -- year2 no more than one year in the future; months same style
Line 423: Line 423:
month2 = get_month_number (month2);
month2 = get_month_number (month2);


elseif date_string:match("^%a+ +[1-9]%d%d%d–%d%d%a?$") then -- special case Winter/Summer year-year (YYYY-YY); year separated with unspaced endash
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d%d%d[%-–]%d%d%a?$") then -- special case Winter/Summer year-year (YYYY-YY); year separated with unspaced endash
local century;
local century;
month, year, century, anchor_year, year2=date_string:match("(%a+) +((%d%d)%d%d)–((%d%d)%a?)");
month, year, century, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +((%d%d)%d%d)[%-]((%d%d)%a?)");
if 'Winter' ~= month and 'Summer' ~= month then return false end; -- 'month' can only be Winter or Summer
if 'Winter' ~= month and 'Summer' ~= month then return false end; -- 'month' can only be Winter or Summer
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
Line 433: Line 433:
month = get_season_number (month);
month = get_season_number (month);


elseif date_string:match("^%a+ +[1-9]%d%d%d–[1-9]%d%d%d%a?$") then -- special case Winter/Summer year-year; year separated with unspaced endash
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d%d%d[%-–][1-9]%d%d%d%a?$") then -- special case Winter/Summer year-year; year separated with unspaced endash
month, year, anchor_year, year2=date_string:match("(%a+) +(%d%d%d%d)–((%d%d%d%d)%a?)");
month, year, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +(%d%d%d%d)[%-]((%d%d%d%d)%a?)");
if 'Winter' ~= month and 'Summer' ~= month then return false end; -- 'month' can only be Winter or Summer
if 'Winter' ~= month and 'Summer' ~= month then return false end; -- 'month' can only be Winter or Summer
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
Line 441: Line 441:
month = get_season_number (month); -- for metadata
month = get_season_number (month); -- for metadata


elseif date_string:match("^%a+ +[1-9]%d%d%d% – %a+ +[1-9]%d%d%d%a?$") then -- month/season year - month/season year; separated by spaced endash
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d%d%d% [%-] %a+ +[1-9]%d%d%d%a?$") then -- month/season year - month/season year; separated by spaced endash
month, year, month2, anchor_year, year2=date_string:match("(%a+) +(%d%d%d%d) – (%a+) +((%d%d%d%d)%a?)");
month, year, month2, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +(%d%d%d%d) [%-] (%a+) +((%d%d%d%d)%a?)");
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
Line 456: Line 456:
end
end


elseif date_string:match ("^%a+–%a+ +[1-9]%d%d%d%a?$") then -- month/season range year; months separated by endash  
elseif mw.ustring.match(date_string, "^%a+[%-]%a+ +[1-9]%d%d%d%a?$") then -- month/season range year; months separated by endash  
month, month2, anchor_year, year=date_string:match ("(%a+)–(%a+)%s*((%d%d%d%d)%a?)");
month, month2, anchor_year, year=mw.ustring.match(date_string, "(%a+)[%-](%a+)%s*((%d%d%d%d)%a?)");
if (not is_valid_month_season_range(month, month2)) or (not is_valid_year(year)) then return false; end
if (not is_valid_month_season_range(month, month2)) or (not is_valid_year(year)) then return false; end
if 0 ~= get_month_number(month) then -- determined to be a valid range so just check this one to know if month or season
if 0 ~= get_month_number(month) then -- determined to be a valid range so just check this one to know if month or season
Line 480: Line 480:
end
end


elseif date_string:match("^[1-9]%d%d%d?–[1-9]%d%d%d?%a?$") then -- Year range: YYY-YYY or YYY-YYYY or YYYY–YYYY; separated by unspaced endash; 100-9999
elseif mw.ustring.match(date_string, "^[1-9]%d%d%d?[%-][1-9]%d%d%d?%a?$") then -- Year range: YYY-YYY or YYY-YYYY or YYYY–YYYY; separated by unspaced endash; 100-9999
year, anchor_year, year2=date_string:match("(%d%d%d%d?)–((%d%d%d%d?)%a?)");
year, anchor_year, year2=mw.ustring.match(date_string, "(%d%d%d%d?)[%-]((%d%d%d%d?)%a?)");
anchor_year=year..'–'..anchor_year; -- assemble anchor year from both years
anchor_year=year..'–'..anchor_year; -- assemble anchor year from both years
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
if not is_valid_year(year2) then return false; end -- no year farther in the future than next year
if not is_valid_year(year2) then return false; end -- no year farther in the future than next year


elseif date_string:match("^[1-9]%d%d%d–%d%d%a?$") then -- Year range: YYYY–YY; separated by unspaced endash
elseif mw.ustring.match(date_string, "^[1-9]%d%d%d[%-–]%d%d%a?$") then -- Year range: YYYY–YY; separated by unspaced endash
local century;
local century;
year, century, anchor_year, year2=date_string:match("((%d%d)%d%d)–((%d%d)%a?)");
year, century, anchor_year, year2=mw.ustring.match(date_string, "((%d%d)%d%d)[%-]((%d%d)%a?)");
anchor_year=year..'–'..anchor_year; -- assemble anchor year from both years
anchor_year=year..'–'..anchor_year; -- assemble anchor year from both years
if 13 > tonumber(year2) then return false; end -- don't allow 2003-05 which might be May 2003
if 13 > tonumber(year2) then return false; end -- don't allow 2003-05 which might be May 2003
Line 546: Line 546:
local anchor_year; -- will return as nil if the date being tested is not |date=
local anchor_year; -- will return as nil if the date being tested is not |date=
local COinS_date; -- will return as nil if the date being tested is not |date=
local COinS_date; -- will return as nil if the date being tested is not |date=
local embargo_date; -- if good dmy, mdy, ymd date then holds original value else 9999
local embargo_date; -- if embargo date is a good dmy, mdy, ymd date then holds original value else reset to 9999
local error_message = "";
local error_message = "";
local good_date = false;
local good_date = false;
Line 625: Line 625:
end
end


elseif date_string:match ("%d%d%d%d–%d%d") then -- YYYY-YY date ranges
elseif mw.ustring.match(date_string, "%d%d%d%d[%-–]%d%d") then -- YYYY-YY date ranges
local century;
local century;
date1, century, date2 = date_string:match ("((%d%d)%d%d)[%s%-–]+(%d%d)");
date1, century, date2 = mw.ustring.match(date_string, "((%d%d)%d%d)[%-–]+(%d%d)");
date2 = century..date2; -- convert YY to YYYY
date2 = century..date2; -- convert YY to YYYY
if year ~= date1 and year ~= date2 then
if year ~= date1 and year ~= date2 then
Line 757: Line 757:
end
end
return true; -- declare success and done
return true; -- declare success and done
end
--[[--------------------------< D A T E _ H Y P H E N _ T O _ D A S H >----------------------------------------
Loops through the list of date-holding parameters and converts any hyphen to an ndash.  Not called if the cs1|2
template has any date errors.
Modifies the date_parameters_list and returns true if hyphens are replaced, else returns false.
]]
local function date_hyphen_to_dash (date_parameters_list)
local result = false;
local n;
for param_name, param_val in pairs(date_parameters_list) do -- for each date-holding parameter in the list
if not param_val:match ('%d%d%d%d%-%d%d%-%d%d') then -- for those that are not ymd dates
param_val, n = param_val:gsub ('%-', '–'); -- replace any hyphen with ndash
if 0 ~= n then
date_parameters_list[param_name] = param_val; -- update the list
result = true;
end
end
end
return result; -- so we know if
end
end


Line 777: Line 802:
year_date_check = year_date_check,
year_date_check = year_date_check,
reformat_dates = reformat_dates,
reformat_dates = reformat_dates,
date_hyphen_to_dash = date_hyphen_to_dash,
set_selected_modules = set_selected_modules
set_selected_modules = set_selected_modules
}
}
Anonymous user