32,512
edits
(Synch from sandbox;) |
m (1 revision imported: Modules) |
||
Line 2:
local p = {}
--[[--------------------------< I S _ V A L I D _ A C C E S S D A T E >----------------------------------------
returns true if:
Line 21 ⟶ 14:
adding 24 hours gives 2015-01-03T00:00:00 – one second more than tomorrow
]]
local function is_valid_accessdate (accessdate)
Line 61 ⟶ 47:
local long_months = {['January']=1, ['February']=2, ['March']=3, ['April']=4, ['May']=5, ['June']=6, ['July']=7, ['August']=8, ['September']=9, ['October']=10, ['November']=11, ['December']=12};
local short_months = {['Jan']=1, ['Feb']=2, ['Mar']=3, ['Apr']=4, ['May']=5, ['Jun']=6, ['Jul']=7, ['Aug']=8, ['Sep']=9, ['Oct']=10, ['Nov']=11, ['Dec']=12};
local temp;
temp=long_months[month];
if temp then return temp; end -- if month is the long-form name
temp=short_months[month];
if temp then return temp; end -- if month is the short-form name
return 0; -- misspelled, improper case, or not a month name
end
Line 73 ⟶ 62:
local function get_season_number (season)
local season_list = {['Winter']=
local temp;
temp=season_list[season];
if temp then return temp; end
return 0;
end
Line 87 ⟶ 76:
local function is_proper_name (name)
local name_list = {['Christmas']=
local temp;
temp=name_list[name];
Line 108 ⟶ 97:
return true;
end
--[[--------------------------< I S _ V A L I D _ Y E A R >----------------------------------------------------
Line 114 ⟶ 104:
]]
local function is_valid_year(year)
if not is_set(year_limit) then
Line 122 ⟶ 112:
end
--[[
Returns true if day is less than or equal to the number of days in month and year is no farther into the future than next year; else returns false.
Assumes Julian calendar prior to year 1582 and Gregorian calendar thereafter. Accounts for Julian calendar leap years before 1582 and Gregorian leap years after 1582.
Where the two calendars overlap (1582 to approximately 1923) dates are assumed to be Gregorian.
]]
local function is_valid_date (year, month, day)
local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
local month_length;
if not is_valid_year(year) then
return false;
end
month_length = 28; -- then 28 days unless
if 0==(year%4) then
month_length = 29;
end
else
if (0==(year%4) and (0~=(year%100) or 0==(year%400))) then
month_length = 29;
end
end
Line 199 ⟶ 183:
local function is_valid_month_season_range(range_start, range_end)
local range_start_number = get_month_number (range_start);
if 0 == range_start_number then
local range_start_number = get_season_number (range_start);
local range_end_number = get_season_number (range_end);
if 0 ~= range_start_number then
if range_start_number < range_end_number then
return true;
end
if
return true;
end
Line 216 ⟶ 199:
end
local range_end_number = get_month_number (range_end);
if range_start_number < range_end_number then
if is_valid_month_range_style (range_start, range_end) then -- do months have the same style?
return true; -- proper order and same style
end
end
return false;
end
--[[--------------------------< C H E C K _ D A T E >----------------------------------------------------------
Check date format to see that it is one of the formats approved by WP:DATESNO or WP:DATERANGE. Exception: only allowed range separator is endash.
character abbreviations, and seasons must be spelled correctly. Future years beyond next year are not allowed.
If the date fails the format tests, this function returns false and does not return values for anchor_year and COinS_date. When this happens, the date parameter is
used in the COinS metadata and the CITEREF identifier gets its year from the year parameter if present otherwise CITEREF does not get a date value.
Inputs:
Line 329 ⟶ 224:
true, anchor_year, COinS_date
anchor_year can be used in CITEREF anchors
COinS_date is
]]
local function check_date (date_string)
local year; -- assume that year2, months, and days are not used;
local year2=0; -- second year in a year range
Line 345 ⟶ 238:
if date_string:match("^%d%d%d%d%-%d%d%-%d%d$") then -- year-initial numerical year month day format
year, month, day=string.match(date_string, "(%d%d%d%d)%-(%d%d)%-(%d%d)");
month=tonumber(month);
if 12 < month or 1 > month or 1583 > tonumber(year) then return false; end -- month number not valid or not Gregorian calendar
anchor_year = year;
Line 358 ⟶ 252:
month = get_month_number (month);
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? +%a+ +[1-9]%d%d%d%a?$") then -- day-initial: day month year
Line 371 ⟶ 263:
month = get_month_number (month);
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? +%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?)");
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);
month2 = get_month_number (month2);
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
month, day, month2, day2, anchor_year, year=date_string:match("(%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
month = get_month_number (month);
month2 = get_month_number (month2);
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
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?)");
if tonumber(year2) <= tonumber(year) then return false; end
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
month = get_month_number (month);
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
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?)");
if tonumber(year2) <= tonumber(year) then return false; end
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
month = get_month_number (month);
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
if nil == date_string:match("^Winter") and nil == date_string:match("^Summer") then return false end; -- 'month' can only be Winter or Summer
local century;
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
year2 = century..year2; -- add the century to year2 for comparisons
if 1 ~= tonumber(year2) - tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
if not is_valid_year(year2) then return false; end -- no year farther in the future than next year
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
if nil == date_string:match("^Winter") and nil == date_string:match("^Summer") then return false end; -- 'month' can only be Winter or Summer
year, anchor_year, year2=date_string:match("%a+ +(%d%d%d%d)–((%d%d%d%d)%a?)");
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
if 1 ~= tonumber(year2) - tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
if not is_valid_year(year2) then return false; end -- no year farther in the future than next year
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
Line 425 ⟶ 311:
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((0 ~= get_month_number(month) and 0 ~= get_month_number(month2) and is_valid_month_range_style(month, month2))
(0 ~= get_season_number(month) and 0 ~= get_season_number(month2))) then return false; end -- or season year, not mixed
elseif date_string:match ("^%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?)");
if (not is_valid_month_season_range(month, month2)) or (not is_valid_year(year)) then
return false;
end
elseif date_string:match("^%a+ +%d%d%d%d%a?$") then -- month/season year or proper-name year
Line 451 ⟶ 324:
if not is_valid_year(year) then return false; end
if not is_valid_month_or_season (month) and 0 == is_proper_name (month) then return false; 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
Line 481 ⟶ 347:
else
return false;
end
local result=true;
if 0 ~= year and 0 ~= month and 0 ~= day and 0 == year2 and 0 == month2 and 0 == day2 then -- YMD (simple whole date)
result=is_valid_date(year,month,day);
Line 502 ⟶ 368:
if false == result then return false; end
-- if here, then date_string is valid; get coins_date from date_string (leave CITEREF disambiguator) ...
coins_date=date_string:match("^(.+%d)%a?$"); -- last character of valid disambiguatable date is always a digit
coins_date= mw.ustring.gsub(coins_date, "–", "-" ); -- ... and replace any ndash with a hyphen
return true, anchor_year, coins_date;
end
--[[--------------------------< D A T E S >--------------------------------------------------------------------
Line 522 ⟶ 386:
]]
local function dates(date_parameters_list)
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 error_message = "";
local mismatch = 0;
local good_date = false;
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 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=
local year = v:match("c%. ([1-9]%d%d%d?)%a?"); -- get the year portion so it can be tested
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
Line 538 ⟶ 404:
good_date = is_valid_year(year);
end
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
good_date, anchor_year, COinS_date = true, v: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
good_date, anchor_year, COinS_date = true, v:match("((nd)%a?)"); --"nd"; no error when date parameter is set to no date
else
good_date, anchor_year, COinS_date = check_date (v
end
elseif '
good_date = check_date (v); -- go test the date
if true == good_date then -- if the date is a valid date
good_date = is_valid_accessdate (v); -- is Wikipedia start date < accessdate < tomorrow's date?
end
else -- any other date-holding parameter
good_date = check_date (v); -- go test the date
end
if false==good_date then -- assemble one error message so we don't add the tracking category multiple times
if is_set(error_message) then -- once we've added the first portion of the error message ...
error_message=error_message .. ", "; -- ... add a comma space separator
end
error_message=error_message .. "|" .. k .. "="; -- add the failed parameter
end
end
end
return anchor_year, COinS_date, error_message, mismatch;
end
--[[--------------------------< Y E A R _ D A T E _ C H E C K >------------------------------------------------
Line 583 ⟶ 448:
year = year_string:match ('(%d%d%d%d?)');
if date_string:match ('%d%d%d%d%-%d%d%-%d%d') and year_string:match ('%d%d%d%d%a') then --special case where
date1 = date_string:match ('(%d%d%d%d)');
year = year_string:match ('(%d%d%d%d)');
Line 592 ⟶ 457:
end
elseif date_string:match ("%d%d%d%d?.-%d%d%d%d?") then -- any of the standard
date1, date2 = date_string:match ("(%d%d%d%d?).-(%d%d%d%d?)");
if year ~= date1 and year ~= date2 then
Line 598 ⟶ 463:
end
elseif date_string:match ("%d%d%d%
local century;
date1, century, date2 = date_string:match ("((%d%d)%d%d)[%s%-–]+(%d%d)");
Line 611 ⟶ 476:
result = 0;
end
end
return result;
end
return {dates = dates, year_date_check = year_date_check} -- return exported functions
|