"मोड्युल:Citation/CS1/Date validation" को बिचमी भिन्नता

Content deleted Content added
ना १ संशोधन आयात भयो: Importing from Incubator
कुनै सम्पादन सारांश छैन
पंक्ति ९:
 
 
--[=[--------------------------< I S _ V A L I D _ A C C E S S D A T E >----------------------------------------
 
returns true if:
पंक्ति २१:
adding 24 hours gives 2015-01-03T00:00:00 – one second more than tomorrow
 
]]
This function does not work if it is fed month names for languages other than English. Wikimedia #time: parser
apparently doesn't understand non-Engish date month names. This function will always return false when the date
contains a non-English month name because good1 is false after the call to lang.formatDate(). To get around that
call this function with YYYY-MM-DD format dates.
 
]=]
 
local function is_valid_accessdate (accessdate)
Line ४२ ⟶ ३७:
return false; -- one or both failed to convert to unix time stamp
end
 
if 979516800 <= access_ts and access_ts < tomorrow_ts then -- Wikipedia start date <= accessdate < tomorrow's date
return true;
Line ४९ ⟶ ४४:
end
end
 
 
--[[--------------------------< G E T _ M O N T H _ N U M B E R >----------------------------------------------
Line ६० ⟶ ५४:
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 ntemp;
return long_months[month] or -- if month is the long-form name
temp=long_months[month];
short_months[month] or -- if month is the short-form name
if temp then return long_months[month]temp; or end -- if month is the long-form name
0; -- misspelled, improper case, or not a month name
temp=short_months[month];
short_months[month] if or temp then return temp; end -- if month is the short-form name
return 0; -- misspelled, improper case, or not a month name
end
 
 
--[[--------------------------< I S _ V A L I D _ E M B A R G O _ D A T E >------------------------------------
 
returns true and date value if that value has proper dmy, mdy, ymd format.
 
returns false and 9999 (embargoed forever) when date value is not proper format; assumes that when |embargo= is
set, the editor intended to embargo a pmc but |embargo= does not hold a single date.
 
]]
 
local function is_valid_embargo_date (v)
if v:match ('^%d%d%d%d%-%d%d%-%d%d$') or -- ymd
v:match ('^%d%d?%s+%a+%s+%d%d%d%d$') or -- dmy
v:match ('^%a+%s+%d%d?%s*,%s*%d%d%d%d$') then -- mdy
return true, v;
end
return false, '9999'; -- if here not good date so return false and set embargo date to long time in future
end
 
 
--[[--------------------------< G E T _ S E A S O N _ N U M B E R >--------------------------------------------
Line २१८ ⟶ १९५:
local function is_valid_month_season_range(range_start, range_end)
local range_start_number = get_month_number (range_start);
local range_end_number;
if 0 == range_start_number then -- is this a month range?
local range_start_number = get_season_number (range_start); -- not a month; is it a season? get start season number
local range_end_number = get_season_number (range_end); -- get end season number
 
if 0 ~= range_start_number then -- is start of range a season?
Line २३५ ⟶ २११:
end
 
local range_end_number = get_month_number (range_end); -- get end month number
if range_start_number < range_end_number then -- range_start is a month; does range_start precede range_end?
if is_valid_month_range_style (range_start, range_end) then -- do months have the same style?
Line ३५२ ⟶ ३२८:
]]
 
local function check_date (date_string, tCOinS_date, test_accessdate)
local year; -- assume that year2, months, and days are not used;
local year2=0; -- second year in a year range
Line ३७२ ⟶ ३४८:
if 0 == month then return false; end -- return false if month text isn't one of the twelve months
elseif mw.ustring.date_string: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=mw.ustringstring.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;
month = get_month_number (month);
Line ३८५ ⟶ ३६१:
if 0 == month then return false; end -- return false if month text isn't one of the twelve months
 
elseif mw.ustring.date_string: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=mw.ustringstring.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;
month = get_month_number (month);
Line ३९३ ⟶ ३६९:
year2=year;
 
elseif mw.ustring.date_string: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=mw.ustring.date_string: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;
month = get_month_number (month); -- for metadata
Line ४०० ⟶ ३७६:
year2=year;
 
elseif mw.ustring.date_string: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=mw.ustring.date_string: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
month = get_month_number (month); -- for metadata
Line ४०७ ⟶ ३८३:
year2=year;
 
elseif mw.ustring.date_string: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=mw.ustring.date_string: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 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 ४१४ ⟶ ३९०:
month2 = get_month_number (month2);
 
elseif mw.ustring.date_string: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=mw.ustring.date_string: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 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 ४२१ ⟶ ३९७:
month2 = get_month_number (month2);
 
elseif mw.ustring.date_string:match(date_string, "^%a+ +[1-9]%d%d%d[%-–]d–%d%d%a?$") then -- special case Winter/Summer year-year (YYYY-YY); year separated with unspaced endash
local century;
month, year, century, anchor_year, year2=mw.ustring.date_string: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
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
Line ४३१ ⟶ ४०७:
month = get_season_number (month);
 
elseif mw.ustring.date_string:match(date_string, "^%a+ +[1-9]%d%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=mw.ustring.date_string: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
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
Line ४३९ ⟶ ४१५:
month = get_season_number (month); -- for metadata
 
elseif mw.ustring.date_string: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=mw.ustring.date_string: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
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
Line ४५४ ⟶ ४३०:
end
 
elseif mw.ustring.date_string: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=mw.ustring.date_string: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 0 ~= get_month_number(month) then -- determined to be a valid range so just check this one to know if month or season
Line ४७८ ⟶ ४५४:
end
 
elseif mw.ustring.date_string: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=mw.ustring.date_string:match(date_string, "(%d%d%d%d?)[%-]((%d%d%d%d?)%a?)");
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 not is_valid_year(year2) then return false; end -- no year farther in the future than next year
 
elseif mw.ustring.date_string:match(date_string, "^[1-9]%d%d%d[%-–]d–%d%d%a?$") then -- Year range: YYYY–YY; separated by unspaced endash
local century;
year, century, anchor_year, year2=mw.ustring.date_string:match(date_string, "((%d%d)%d%d)[%-]((%d%d)%a?)");
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
Line ५०१ ⟶ ४७७:
else
return false; -- date format not one of the MOS:DATE approved formats
end
 
if test_accessdate then -- test accessdate here because we have numerical date parts
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; accessdate must not be a range
if not is_valid_accessdate (year..'-'..month..'-'..day) then
return false; -- return false when accessdate out of bounds
end
else
return false; -- return false when accessdate is a range of two dates
end
end
 
Line ५५५ ⟶ ५२०:
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 embargo_date; -- if embargo date is a good dmy, mdy, ymd date then holds original value else reset to 9999
local error_message = "";
local good_date = false;
Line ५७० ⟶ ५३४:
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
Line ५७६ ⟶ ५४०:
else
good_date, anchor_year, COinS_date = check_date (v, tCOinS_date); -- go test the date
end
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
good_date, anchor_year, COinS_date = true, v:match("((%d+)%a?)");
end
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
elseif 'embargo'==k then -- if the parameter is |embargo=
good_date = check_date (v); -- go test the date
if true == good_date then -- if the date is a valid date
good_date, embargo_date = is_valid_embargo_dateis_valid_accessdate (v); -- is |embargo=Wikipedia start date a< singleaccessdate dmy,< mdy, or ymd formattedtomorrow's date? yes:returns embargo; no: returns 9999
end
else -- any other date-holding parameter
Line ५९९ ⟶ ५५७:
end
end
return anchor_year, embargo_date, error_message; -- and done
end
 
Line ६३५ ⟶ ५९३:
end
 
elseif mw.ustring.date_string:match (date_string, "%d%d%d%d[%-–]d–%d%d") then -- YYYY-YY date ranges
local century;
date1, century, date2 = mw.ustring.match(date_string,:match ("((%d%d)%d%d)[%s%-–]+(%d%d)");
date2 = century..date2; -- convert YY to YYYY
if year ~= date1 and year ~= date2 then
Line ७६० ⟶ ७१८:
format_str = long_formats[format];
end
end
-- convert date and save;
date_parameters_list[param_name] = mw.text.trim (os.date (format_str, os.time(source_date))); -- stripconvert leading space when single digit daydate and %e is first formatsave
end
end
Line ७६७ ⟶ ७२५:
end
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
 
Line ८१२ ⟶ ७४५:
year_date_check = year_date_check,
reformat_dates = reformat_dates,
date_hyphen_to_dash = date_hyphen_to_dash,
set_selected_modules = set_selected_modules
}