Jump to content

Module:Current events calendar

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 12:56, 3 February 2014 (save progress on a module to generate the calendar at Portal:Current events). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module renders the calendar seen on [[Portal:Current events]].

local p = {}

function p.main()
	local dateStuff = p.getDateStuff()
	local calendarStrings = p.makeCalendarStrings(dateStuff)
end

function p.getDateStuff()
	-- Gets date data.
	local dateStuff = {}
	local lang = mw.language.getContentLanguage()
	--Year
	local year = lang:formatDate('Y')
	year = tonumber(year)
	dateStuff.year = year
	-- Month
	local month = lang:formatDate('F')
	dateStuff.month = month
	-- Day
	local day = lang:formatDate('j')
	day = tonumber(day)
	dateStuff.day = day
	-- Days in month
	local firstOfMonth = string.format('1 %s %d', month, year)
	local daysInMonth = lang:formatDate('j', firstOfMonth .. ' +1 month -1 day', month, year)
	daysInMonth = tonumber(daysInMonth)
	dateStuff.daysInMonth = daysInMonth
	-- Weekday of the first day of the month
	local firstWeekday = lang:formatDate('w', firstOfMonth) -- Sunday = 0, Saturday = 6
	firstWeekday = tonumber(firstWeekday)
	firstWeekday = firstWeekday + 1 -- Make compatible with Lua tables. Sunday = 1, Saturday = 7.
	dateStuff.firstWeekday = firstWeekday
	return dateStuff
end

function p.makeCalendarStrings(dateStuff)
	local calStrings = {}
	local currentDay = dateStuff.day
	local isLinkworthy = p.isLinkworthy
	local currentMonth = dateStuff.month
	local currentYear = dateStuff.year
	local makeDayLink = p.makeDayLink
	for day = 1, dateStuff.daysInMonth do
		if isLinkworthy(day, currentDay) then
			calStrings[#calStrings + 1] = makeDayLink(day, currentMonth, currentYear)
		else
			calStrings[#calStrings + 1] = tostring(day)
		end
	end
	return calStrings
end

function p.isLinkworthy(day, currentDay)
	-- Returns true if the calendar day should be linked, and false if not.
	-- Days should be linked if they are the current day or if they are within the six
	-- preceding days, as that is the number of items on the current events page.
	if currentDay - 6 <= day and day <= currentDay then
		return true
	else
		return false
	end
end

function p.makeDayLink(day, month, year)
	return string.format("'''[[#%d %s %d|%d]]'''", year, month, day, day)
end

return p