Module:Calendar date/sandbox: Difference between revisions

Content deleted Content added
No edit summary
use require('strict') instead of require('Module:No globals')
 
(101 intermediate revisions by 4 users not shown)
Line 1:
--[[
 
Display non-Gregorian date of a holiday datesthat usingmoves equivalentyear Gregorianto year. Date data can be obtained from multiple sources as configured in Module:Calendar date/events
 
"localfile" = local data file (eg. https://en.wikipedia.org/wiki/Template:Calendar_date/localfiles/Hanukkah)
"calculator" = user-supplied date calculator
"wikidata" = <tbd> for holidays with their own date entity page such as https://www.wikidata.org/wiki/Q51224536
]]
 
require('strict')
 
local p = {}
local cfg -- Data structure from ~/events
local eventdata -- Data structure from ~/localfiles/<holiday name>
local track = {} -- Tracking category container
 
--[[--------------------------< inlineError >-----------------------
Line 15 ⟶ 24:
local function inlineError(arg, msg, tname)
 
track["Category:Calendar date template errors"] = 1
return '<span style="font-size:100%" class="error citation-comment">Error in {{' .. tname .. '}} - Check <code style="color:inherit; border:inherit; padding:inherit;">&#124;' .. arg .. '=</code> ' .. msg .. '</span>'
 
end
Line 22 ⟶ 31:
--[[--------------------------< trimArg >-----------------------
 
trimArg returns nil if arg is "" while trimArg2 returns 'true' if arg is ""
trimArg2 is for args that might accept an empty value, as an on/off switch like nolink=
 
]]
 
local function trimArg(arg)
if arg == "" or arg == nil then
return nil
else
return mw.text.trim(arg)
end
end
local function trimArg2(arg)
if arg == nil then
return nil
else
return mw.text.trim(arg)
end
end
 
--[[--------------------------< tableLength >-----------------------
 
Given a 1-D table, return number of elements
 
]]
 
local function tableLength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
 
--[[-------------------------< make_wikilink >----------------------------------------------------
 
Makes a wikilink; when both link and display text is provided, returns a wikilink in the form [ [L|D] ]; if only
link is provided, returns a wikilink in the form [ [L] ]; if neither are provided or link is omitted, returns an
empty string.
 
]]
 
local function make_wikilink (link, display, no_link)
if nil == no_link then
if link and ('' ~= link) then
if display and ('' ~= display) then
return table.concat ({'[[', link, '|', display, ']]'});
else
return table.concat ({'[[', link, ']]'});
end
end
else -- no_link
if display and ('' ~= display) then -- if there is display text
return display; -- return that
else
return link or ''; -- return the target article name or empty string
end
end
end
 
--[[--------------------------< createTracking >-----------------------
 
Return data in track[] ie. tracking categories
 
]]
Line 62 ⟶ 97:
local function createTracking()
 
local sandout = ""{};
if tableLength(track) > 0 then
for key, _ in pairs(track) do -- loop through table
table.insert (out, make_wikilink (key)) -- and convert category names to links
sand = sand .. "[[" .. key .. "]]"
end
end
return table.concat (out) -- concat into one big string; empty string if table is empty
return sand
 
end
 
--[[--------------------------< verifyDateisValidDate >----------------------------------------------------
 
Returns true if date is after 31 December 1899 , not after 2100, and represents a valid date
Given the date arg, return true if within date range 2000-2050 else return false
(29 February 2017 is not a valid date). Applies Gregorian leapyear rules. All arguments are required.
 
]]
 
local function verifyDateisValidDate (dateyear, month, day)
 
local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if not date or date == "" then
local month_length
return nil
local y, m, d
end
local today = os.date ('*') -- fetch a table of current date parts
 
if not year or year == '' or not month or month == '' or not day or day == '' then
return false -- something missing
end
 
y = tonumber (year)
m = tonumber (month)
d = tonumber (day)
 
if 1900 > y or 2100 < y or 1 > m or 12 < m then -- year and month are within bounds
return false
end
 
if (2==m) then -- if February
month_length = 28 -- then 28 days unless
if (0==(y%4) and (0~=(y%100) or 0==(y%400))) then -- is a leap year?
month_length = 29 -- if leap year then 29 days in February
end
else
month_length=days_in_month[m];
end
 
if 1 > d or month_length < d then -- day is within bounds
if tonumber(date) > 1999 and tonumber(date) < 2051 then
return "true"false
end
else
return nil
return true
end
end
 
--[[--------------------------< makeDate >-----------------------
 
Given a zero-padded 4-digit year, 2-digit month and 2-digit day, return a full date in df format
df = mdy, dmy, iso, ymd
 
]]
 
local function makeDate(year, month, day, df, format)
local formatFull = {
['dmy'] = 'j F Y',
['mdy'] = 'F j, Y',
['ymd'] = 'Y F j',
['iso'] = 'Y-m-d'
}
local formatInfobox = {
['dmy'] = 'j F',
['mdy'] = 'F j',
['ymd'] = 'F j',
['iso'] = 'Y-m-d'
}
 
if not year or year == "" or not month or month == "" or not day or day == "" and format[df] then
return nil
end
 
local zmonth = month -- month with leading 0
month = month:match("0*(%d+)") -- month without leading 0
if tonumber(month) < 1 or tonumber(month) > 12 then
return year
end
local nmonth = os.date("%B", os.time{year=2000, month=month, day=1} ) -- month in name form
if not nmonth then
return year
end
 
local zday = day
day = zday:match("0*(%d+)")
if tonumber(day) < 1 or tonumber(day) > 31 then
if df == "mdy" or df == "dmy" then
return nmonth .. " " .. year
elseif df == "iso" then
return year .. "-" .. zmonth
elseif df == "ymd" then
return year .. " " .. nmonth
else
return nmonth .. " " .. year
end
end
 
if format ~= "infobox" then
if df == "mdy" then
return nmonth .. " " .. day .. ", " .. year -- September 1, 2016
elseif df == "dmy" then
return day .. " " .. nmonth .. " " .. year -- 1 September 2016
elseif df == "iso" then
return year .. "-" .. zmonth .. "-" .. zday -- 2016-09-01
elseif df == "ymd" then
return year .. " " .. nmonth .. " " .. day -- 2016 September 1
else
return nmonth .. " " .. day .. ", " .. year -- September 1, 2016
end
else
if df == "mdy" then
return nmonth .. " " .. day -- September 1
elseif df == "dmy" then
return day .. " " .. nmonth -- 1 September
elseif df == "iso" then
return year .. "-" .. zmonth .. "-" .. zday -- 2018-09-01
elseif df == "ymd" then
return nmonth .. " " .. day -- September 1
else
return nmonth .. " " .. day -- September 1
end
end
 
local date = table.concat ({year, month, day}) -- assemble iso format date
if format ~= "infobox" then
return mw.getContentLanguage():formatDate (formatFull[df], date)
else
return mw.getContentLanguage():formatDate (formatInfobox[df], date)
end
end
 
--[[--------------------------< dateOffset >-----------------------
 
Given a 'origdate' in ISO format, return the date offset by number of days in 'offset'
eg. given "2018-02-01" and "-1" it will return "2018-01-30"
On error, return origdate
 
]]
 
local function dateOffset(origdate, offset)
 
local year, month, day = origdate:match ('(%d%d%d%d)-(%d%d)-(%d%d)')
if offset < 1 then
local now = os.time{year = year, month = month, day = day}
return origdate
local newdate = os.date("%Y-%m-%d", now + (tonumber(offset) * 24 * 3600))
end
return newdate and newdate or origdate
 
local datesplit = {}
 
datesplit = mw.text.split(origdate, "-")
datesplit[1], datesplit[2], datesplit[3] = tonumber(datesplit[1]), tonumber(datesplit[2]), tonumber(datesplit[3])
local now = os.time{year = datesplit[1], month = datesplit[2], day = datesplit[3]}
local newdate = os.date("%Y-%m-%d", now + (tonumber(offset) * 24 * 3600))
if not newdate then
return origdate
else
return newdate
end
end
 
--[[--------------------------< renderHoli >-----------------------
 
Render the data
 
]]
local function renderHoli(isAvailcfg,iteventdata,jsoncfg,jsonlocalcalcdate,date,df,format,tname,cite,usercalc)
 
local hits = 0
local matchdate = "^" .. date
local startdate,enddate,outoffset,endoutoffset = nil
local starttitle,endtitle = ""
 
-- user-supplied date calculator
if isAvailcfg.datatype == "calculator" then
if jsoncfg.main.event[it]cfg.datasource then
startdate = jsoncfg.main.event[it].datasourcecalcdate
enddate = dateOffset(startdate, jsoncfg.main.event[it]cfg.days - 1)
else
return inlineError("holiday", 'Invalidinvalid calculator result', tname )
end
 
-- read dates from jsonlocal.itemslocalfile -- it assumes dates in the json are in chrono order, needsneed a more flexible method
elseif isAvailcfg.datatype == "jsonlocallocalfile" then
local numRecords = tableLength(jsonlocal.itemseventdata) -- Get first and last date of holiday
for i = 1, numRecords do
if mw.ustring.find( jsonlocal.itemseventdata[i].date, matchdate ) then
if hits == 0 then
startdate = jsonlocal.itemseventdata[i].date
hits = 1
end
if hits >= tonumber(jsoncfg.main.event[it]cfg.days) then
enddate = jsonlocal.itemseventdata[i].date
break
end
hits = hits + 1
end
end
end
-- Verify data isand special OKconditions
if startdate == nil or enddate == nil then
if jsoncfg.main.event[it]cfg.holidayname == "Hanukkah" and startdate and not enddate then -- Hanukkah bug, template doesn't support cross-year boundary
enddate = dateOffset(startdate, 8)
elseif cfg.datatype == "localfile" and cfg.days > "1" and startdate then
else
enddate = dateOffset(startdate, cfg.days - 1)
return nil
elseif startdate and not enddate then
end
return inlineError("year", 'cannot find enddate', tname) .. createTracking()
end
else
return inlineError("holiday", 'cannot find startdate and enddate', tname) .. createTracking()
end
end
-- Generate start-date offset (ie. holiday starts the evening before the given date)
if jsoncfg.main.event[it]cfg.startoffset then
startdate = dateOffset(startdate, jsoncfg.main.event[it]cfg.startoffset)
if startdate ~= enddate then
enddate = dateOffset(enddate, jsoncfg.main.event[it]cfg.startoffset)
else
cfg.days = if jsoncfg.main.event[it](cfg.days == "1") thenand "2"
end
jsoncfg.main.event[it].days = "2"
end
end
end
-- Generate end-date outside-Irael offset (ie. outside Israel the holiday ends +1 day later)
endoutoffset = cfg.endoutoffset and dateOffset(enddate, cfg.endoutoffset)
if jsoncfg.main.event[it].endoutoffset then
endoutoffset = dateOffset(enddate, jsoncfg.main.event[it].endoutoffset)
end
 
-- Format dates into df format
local year, month, day = startdate:match ('(%d%d%d%d)-(%d%d)-(%d%d)')
local datesplit = {}
startdate = makeDate(year, month, day, df, format)
datesplit = mw.text.split(startdate, "-")
year, month, day = enddate:match ('(%d%d%d%d)-(%d%d)-(%d%d)')
startdate = makeDate(datesplit[1], datesplit[2], datesplit[3], df, format)
enddate = makeDate(year, month, day, df, format)
datesplit = mw.text.split(enddate, "-")
if startdate == nil or enddate == nil then return nil end
enddate = makeDate(datesplit[1], datesplit[2], datesplit[3], df, format)
if startdate == nil or enddate == nil then return nil end
 
-- Add "outside of Israel" notices
if endoutoffset then
year, month, datesplitday = mw.text.split(endoutoffset,:match "('(%d%d%d%d)-"(%d%d)-(%d%d)')
local leader = ((format == "infobox") and "<br>") or " "
local leader = " "
endoutoffset = leader .. "(" .. makeDate(year, month, day, df, "infobox") .. " outside of Israel)"
if format == "infobox" then leader = "<br>" end
end
endoutoffset = leader .. "(" .. makeDate(datesplit[1], datesplit[2], datesplit[3], df, "infobox") .. " outside of Israel)"
if not endoutoffset then
end
endoutoffset = ""
if not endoutoffset then
end
endoutoffset = ""
end
 
--- generateDetermine format string
format = if ((format == "infobox") thenand " –<br>") or " – "
format = " –<br>"
else
format = " – "
end
-- return output
if startdate == enddate or jsoncfg.main.event[it].days == "1" then -- single date
return jsoncfg.main.event[it].prepend1 .. startdate .. endoutoffset .. cite
else
return jsoncfg.main.event[it].prepend1 .. startdate .. format .. jsoncfg.main.event[it].prepend2 .. enddate .. endoutoffset .. cite
end
end
 
--- Determine pre-pended text string eg. "sunset, <date>"
--[[--------------------------< isAvail >-----------------------
local prepend1 = (cfg.prepend1 and (cfg.prepend1 .. ", ")) or ""
local prepend2 = (cfg.prepend2 and (cfg.prepend2 .. ", ")) or ""
 
-- return output
Given the Configuration.js and name of holiday return if data type is usercalc, jsonfile or not-found (nil)
if startdate == enddate or cfg.days == "1" then -- single date
 
return prepend1 .. startdate .. endoutoffset .. cite
]]
else
 
return prepend1 .. startdate .. format .. prepend2 .. enddate .. endoutoffset .. cite
function isHolidayAvail(cfg, holiday)
end
 
local numRecords = tableLength(cfg.main)
for i = 1, numRecords do
if cfg.main.event[i].holiday:lower() == holiday:lower() then
if cfg.main.event[i].datatype:lower() == "calculator" then
return "calculator", i
elseif cfg.main.event[i].datatype:lower() == "jsonlocal" or cfg.main.event[i].datatype:lower() == "json local" then
return "jsonlocal", i
else
return "here1", "1"
end
end
end
return "here2", "1"
end
 
Line 315 ⟶ 304:
function p.calendardate(frame)
 
local pframe = frame:getParent()
local args = pframe.args
 
local tname = "Calendar date" -- name of calling template. Change if template rename.
local holiday = nil -- name of holiday
local date = nil -- date of holiday (year)
local df = nil -- date format (mdy, dmy, iso - default: iso)
local format = nil -- template display format options
local cite = nil -- leave a citation at end
local calcdate = ""
 
--- Determine holiday
track = {} -- global tracking-category table
holiday = trimArg(args.holiday) -- required
if not holiday then
holiday = trimArg(args.event) -- event alias
if not holiday then
return inlineError("holiday", 'missing holiday argument', tname) .. createTracking()
end
end
 
--- Determine holidaydate
holiday date = trimArg(args.holidayyear) -- required
if not holidaydate then
return inlineError("year", 'missing year argument', tname) .. createTracking()
holiday = trimArg(args.event) -- event alias
elseif not isValidDate(date, "01", "01") then
if not holiday then
return inlineError("holidayyear", "Missing'invalid holiday argument"year', tname) .. createTracking()
end
end
 
--- Determine dateformat type
date format = trimArg(args.yearformat) -- required
if not dateformat then
format = "none"
return inlineError("year", "Missing year argument", tname) .. createTracking()
elseif format ~= "infobox" then
elseif not verifyDate(date) then
format = "none"
return inlineError("year", "Invalid year", tname) .. createTracking()
end
end
 
-- Load configuration file
--- Determine format type
local eventsfile = mw.loadData ('Module:Calendar date/events')
format = trimArg(args.format)
if eventsfile.hebrew_calendar[mw.ustring.upper(holiday)] then
if not format then
cfg = eventsfile.hebrew_calendar[mw.ustring.upper(holiday)]
format = "none"
elseif eventsfile.christian_events[mw.ustring.upper(holiday)] then
elseif format ~= "infobox" then
cfg = eventsfile.christian_events[mw.ustring.upper(holiday)]
format = "none"
elseif eventsfile.carnivals[mw.ustring.upper(holiday)] then
end
cfg = eventsfile.carnivals[mw.ustring.upper(holiday)]
elseif eventsfile.chinese_events[mw.ustring.upper(holiday)] then
cfg = eventsfile.chinese_events[mw.ustring.upper(holiday)]
elseif eventsfile.misc_events[mw.ustring.upper(holiday)] then
cfg = eventsfile.misc_events[mw.ustring.upper(holiday)]
else
return inlineError("holiday", 'unknown holiday ' .. holiday, tname) .. createTracking()
end
 
-- If datatype = localfile
--- Parse JSON files
if cfg.datatype == "localfile" then
local cfgfp = mw.title.makeTitle( 'Template', tname .. '/Configuration.js' )
local eventfile = nil
if not cfgfp.exists then
eventfile = mw.loadData(cfg.datasource)
return inlineError("holiday", 'File missing: Template:' .. tname .. '/Configuration.js', tname) .. createTracking()
if eventfile.event then
end
eventdata = eventfile.event
local jsoncfg, jsonlocal = nil
else
jsoncfg = mw.text.jsonDecode( cfgfp:getContent() )
return inlineError("holiday", 'unknown holiday file ' .. cfg.datasource .. '</span>', tname) .. createTracking()
if not jsoncfg then
end
return inlineError("holiday", 'Error in file: Template:' .. tname .. '/Configuration.js', tname) .. createTracking()
end
local isAvail, it = isHolidayAvail( jsoncfg, holiday )
if isAvail then
if not jsoncfg.main.event[1].datasource then
return inlineError("holiday", 'Template:' .. tname .. '/Configuration.js misconfigured: missing datasource', tname) .. createTracking()
end
if isAvail == "jsonlocal" then -- dates are contained in a local JSON file
local version = mw.title.makeTitle( jsoncfg.main.event[it].datasource )
if not version.exists then
return inlineError("holiday", 'File missing: Template:' .. jsoncfg.main.event[it].datasource, tname) .. createTracking()
end
if version.isRedirect then
jsonlocal = mw.text.jsonDecode( version.redirectTarget:getContent() )
else
jsonlocal = mw.text.jsonDecode( version:getContent() )
end
elseif isAvail == "calculator" then -- dates are calculated with a user-provided calculator
jsoncfg.main.event[it].datasource = jsoncfg.main.event[it].datasource:gsub("^<nowiki>", '')
jsoncfg.main.event[it].datasource = jsoncfg.main.event[it].datasource:gsub("</[ ]*nowiki>$", '')
jsoncfg.main.event[it].datasource = jsoncfg.main.event[it].datasource:gsub("YYYY", date)
jsoncfg.main.event[it].datasource = frame:preprocess(jsoncfg.main.event[it].datasource)
else
return inlineError("holiday", 'Unknown datatype', tname )
end
else
return inlineError("holiday", 'Unknown holiday ' .. isAvail, tname )
end
 
-- If datatype = calculator
--- Determine df - priority to |df in template, otherwise df in datafile, otherwise default to dmy
elseif cfg.datatype == "calculator" then
df = trimArg(args.df)
calcdate = frame:preprocess(cfg.datasource:gsub("YYYY", date))
if not df then
local year, month, day = calcdate:match ('(%d%d%d%d)-(%d%d)-(%d%d)')
if jsoncfg.main.event[it].df then
if not isValidDate(year, month, day) then
df = jsoncfg.main.event[it].df
return inlineError("holiday", 'invalid calculated date ' .. calcdate, tname) .. createTracking()
else
end
df = "dmy"
else
end
return inlineError("holiday", 'unknown "datatype" in configuration', tname) .. createTracking()
end
end
if df ~= "mdy" and df ~= "dmy" and df ~= "iso" then
df = "dmy"
end
 
--- Determine df - priority to |df in template, otherwise df in datafile, otherwise default to dmy
-- Determine citation
cite df = trimArg2trimArg(args.citedf)
if not if citedf then
df = (cfg.df and citecfg.df) =or "dmy"
end
if isAvail == "jsonlocal" then
if df ~= "mdy" and df ~= "dmy" and df ~= "iso" then
if jsoncfg.main.event[it].citeurl and jsoncfg.main.event[it].accessdate and jsoncfg.main.event[it].source and jsoncfg.main.event[it].holiday then
df = "dmy"
cite = frame:preprocess('<ref name="' .. holiday .. ' dates">{{cite web |url=' .. jsoncfg.main.event[it].citeurl .. ' |title=Dates for ' .. jsoncfg.main.event[it].holiday .. ' |publisher=' .. jsoncfg.main.event[it].source .. ' |via=[[Template:' .. tname .. '|' .. tname .. ']] and [[Template:' .. tname .. '/holidays/' .. holiday .. '.js|' .. holiday .. '.js]] |accessdate=' .. jsoncfg.main.event[it].accessdate .. '}}</ref>')
end
elseif isAvail == "calculator" then
if jsoncfg.main.event[it].source then
cite = frame:preprocess('<ref name="' .. holiday .. ' dates">' .. jsoncfg.main.event[it].source .. '</ref>')
end
end
end
 
- -- Determine pre-pended text eg. "sunset, <date>"citation
cite = trimArg2(args.cite)
if not jsoncfg.main.event[it].prepend1 then
if cite then
jsoncfg.main.event[it].prepend1 = ""
if (cite ~= "no") then
else
cite = ""
jsoncfg.main.event[it].prepend1 = jsoncfg.main.event[it].prepend1 .. ", "
if cfg.citeurl and cfg.accessdate and cfg.source and cfg.name then
end
local citetitle = cfg.citetitle
if not jsoncfg.main.event[it].prepend2 then
if citetitle == nil then
jsoncfg.main.event[it].prepend2 = ""
citetitle = 'Dates for ' .. cfg.name
else
end
jsoncfg.main.event[it].prepend2 = jsoncfg.main.event[it].prepend2 .. ", "
cite = frame:preprocess('<ref name="' .. holiday .. ' dates">{{cite web |url=' .. cfg.citeurl .. ' |title=' .. citetitle .. ' |publisher=' .. cfg.source .. '|accessdate=' .. cfg.accessdate .. '}}</ref>')
end
elseif cfg.source then
cite = frame:preprocess('<ref name="' .. holiday .. ' dates">' .. cfg.source:gsub("YYYY", date) .. '</ref>')
else
cite = ""
end
else
cite = ""
end
else
cite = ""
end
 
-- Render
local rend = renderHoli( isAvail,itcfg,jsoncfgeventdata,jsonlocalcalcdate,date,df,format,tname,cite)
if not rend then
rend = '<span style="font-size:100%" class="error citation-comment">Error in [[:Template:' .. tname .. ']]: Unknown problem. Please report on template talk page.</span>'
track["Category:Webarchive template errors"] = 1
end
 
return rend .. createTracking()
 
end