Jump to content

Module:Calendar date

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 213.147.181.177 (talk) at 14:55, 22 August 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[ 

Display Gregorian date of a holiday that moves year to 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/Module: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 >-----------------------

     Critical error. Render output completely in red. Add to tracking category.

 ]]

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

--[[--------------------------< 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
	end
	return mw.text.trim(arg)
end
local function trimArg2(arg)
	if arg == nil then
		return nil
	end
	return mw.text.trim(arg)
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, ']]'});
			end
			return table.concat ({'[[', link, ']]'});
		end
	else																		-- no_link
		if display and ('' ~= display) then										-- if there is display text
			return display;														-- return that
		end
		return link or '';   													-- return the target article name or empty string
	end
end

--[[--------------------------< createTracking >-----------------------

	Return data in track[] ie. tracking categories

  ]]

local function createTracking()
return "daddy"
end

--[[--------------------------< isValidDate >----------------------------------------------------

	Returns true if date is after 31 December 1899 , not after 2100, and represents a valid date 
	(29 February 2017 is not a valid date).  Applies Gregorian leapyear rules. All arguments are required.

]]

local function isValidDate (year, month, day)
return true
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)
return "daddy"
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)

return "daddy"
  end

--[[--------------------------< renderHoli >-----------------------

	Render the data

  ]]
  
local function renderHoli(cfg,eventdata,calcdate,date,df,format,tname,cite)
return "daddy"
end

--[[--------------------------< calendardate >-----------------------

     Main function

  ]]

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
	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 date
	date = trimArg(args.year)						-- required
	if not date then
		return inlineError("year", 'missing year argument', tname) .. createTracking()
	elseif not isValidDate(date, "01", "01") then
		return inlineError("year", 'invalid year', tname) .. createTracking()
	end

	--- Determine format type
	format = trimArg(args.format)
	if not format or format ~= "infobox" then
		format = "none"
	end 

	-- Load configuration file
	local eventsfile = mw.loadData ('Module:Calendar date/events')
	if eventsfile.hebrew_calendar[mw.ustring.upper(holiday)] then
		cfg = eventsfile.hebrew_calendar[mw.ustring.upper(holiday)]
	elseif eventsfile.christian_events[mw.ustring.upper(holiday)] then
		cfg = eventsfile.christian_events[mw.ustring.upper(holiday)]
	elseif eventsfile.carnivals[mw.ustring.upper(holiday)] then
		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 
	if cfg.datatype == "localfile" then
		local eventfile = nil
		eventfile = mw.loadData(cfg.datasource)
		if eventfile.event then
			eventdata = eventfile.event
		else
			return inlineError("holiday", 'unknown holiday file ' .. cfg.datasource .. '</span>', tname) .. createTracking()
		end

	-- If datatype = calculator
	elseif cfg.datatype == "calculator" then
		calcdate = frame:preprocess(cfg.datasource:gsub("YYYY", date))
		local year, month, day = calcdate:match ('(%d%d%d%d)-(%d%d)-(%d%d)')
		if not isValidDate(year, month, day) then
			return inlineError("holiday", 'invalid calculated date ' .. calcdate, tname) .. createTracking()
		end
	else
		return inlineError("holiday", 'unknown "datatype" in configuration', tname) .. createTracking()
	end

	--- Determine df - priority to |df in template, otherwise df in datafile, otherwise default to dmy
	df = trimArg(args.df)
	if not df then
		df = (cfg.df and cfg.df) or "dmy"
	end
	if df ~= "mdy" and df ~= "dmy" and df ~= "iso" then
		df = "dmy"
	end

	-- Determine citation
	cite = trimArg2(args.cite)
	if cite then
		if (cite ~= "no") then
			cite = ""
			if cfg.citeurl and cfg.accessdate and cfg.source and cfg.name then
				local citetitle = cfg.citetitle
				if citetitle == nil then
					citetitle = 'Dates for ' .. cfg.name
				end
				cite = frame:preprocess('<ref name="' .. holiday .. ' dates">{{cite web |url=' .. cfg.citeurl .. ' |title=' .. citetitle .. ' |publisher=' .. cfg.source .. '|accessdate=' .. cfg.accessdate .. '}}</ref>')
			elseif cfg.source then
				cite = frame:preprocess('<ref name="' .. holiday .. ' dates">' .. cfg.source:gsub("YYYY", date) .. '</ref>')
			end
		else
			cite = ""
		end
	else
		cite = ""
	end

	-- Render 
	local rend = renderHoli( cfg,eventdata,calcdate,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

return p