Module:Current events calendar
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| Editing of this module by new or unregistered users is currently disabled. See the protection policy and protection log for more details. If you cannot edit this module and you wish to make a change, you can submit an edit request, discuss changes on the talk page, request unprotection, log in, or create an account. |
| This module uses TemplateStyles: |
| This Lua module is used on approximately 540 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module generates calendars for Portal:Current events and Category:Current events archives. The current day and the previous six days are linked to the corresponding section of the page. The module also supports archival calendars when month and year parameters are passed. Note that the Current Month version will only link the previous six days and will include a link of form: More December 2025 events....
Usage on Portal:Current events
{{#invoke:current events calendar|main}}
Usage on Category:Current events archives pages
{{#invoke:current events calendar|main|year=[four digit numerical year]|month=[One or two digit number from 1 to 12]}}
Example: Current Events front page version
This version assumes that it is displayed in a page that will set padding/margins/gutters in parent containing elements. It will expand to a maximum of 350px wide and remain centered in the column. It includes a link to the full month's page in the footer. Only the six most recent days of the month will have hyperlinks, reflecting the 6 most recent days of content on the Portal:Current events page.
Lua error at line 132: attempt to call global 'done' (a nil value).
Example: Current Events archive version
This version assumes that it is displayed in a page that uses a table-based layout and expect individual children to set their margins and padding. It will expand to a maximum of 350px wide and puts an 8px margin on the top and left side of the calendar. It has no footer. All days will have links to the appropriate section of the page it is displayed in.
Lua error at line 132: attempt to call global 'done' (a nil value).
Sandbox version
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
-- This module renders the calendar seen on [[Portal:Current events]].
local p = {}
local function makeWikilink(link, display)
if display then
return string.format('[[%s|%s]]', link, display)
else
return string.format('[[%s]]', link)
end
end
function p.main()
local dateStuff = p.getDateStuff()
local dayStrings = p.makeDayStrings(dateStuff)
return p.export(dayStrings, 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
-- Month and year
local monthAndYear = lang:formatDate('F Y')
dateStuff.monthAndYear = monthAndYear
-- Previous month and year
dateStuff.previousMonthAndYear = lang:formatDate('F Y', monthAndYear .. ' -1 month')
-- Next month and year
dateStuff.nextMonthAndYear = lang:formatDate('F Y', monthAndYear .. ' +1 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')
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.makeDayStrings(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
function p.export(dayStrings, dateStuff)
-- Generates the calendar HTML.
local monthAndYear = dateStuff.monthAndYear
local root = mw.html.create('table')
root
:addClass('infobox')
:css{width = '250px', ['text-align'] = 'center', ['background-color'] = '#f5faff', border = '1px solid #cedff2'}
-- Headings
:tag('tr')
:css('background-color', '#cedff2')
:tag('td')
:css{['padding-top'] = '1px', ['padding-bottom'] = '3px'}
:wikitext(makeWikilink(dateStuff.previousMonthAndYear, '<<'))
:done()
:tag('td')
:attr('colspan', '5')
:css{padding = '1px 4px', ['font-weight'] = 'bold'}
:wikitext(makeWikilink(monthAndYear))
:done()
:tag('td')
:css{['padding-top'] = '1px', ['padding-bottom'] = '3px'}
:wikitext(makeWikilink(dateStuff.nextMonthAndYear, '>>'))
:done()
:done()
-- Day of week headings
:tag('tr')
local weekdays = {'S', 'M', 'T', 'W', 'T', 'F', 'S'}
for i, weekday in ipairs(weekdays) do
root:tag('td')
:wikitext(weekday)
:done()
end
root:done()
-- Days
local colspan = dateStuff.firstWeekday - 1
local cellCount = 0 -- Tracks the number of day cells.
root:tag('tr')
if colspan > 1 then
root:tag('td')
:attr('colspan', tostring(colspan))
:done()
elseif colspan == 1 then
root:tag('td')
done()
end
for i = colspan + 1, 7 do -- Finish the first row
cellCount = cellCount + 1
root:tag('td')
:wikitext(dayStrings[cellCount])
:done()
end
root:done()
while cellCount <= #dayStrings do -- Second day row onwards
root:tag('tr')
for i = 1, 7 do
cellCount = cellCount + 1
local dayString = dayStrings[cellCount]
if not dayString then
break
end
root:tag('td')
:wikitext(dayString)
:done()
end
root:done()
end
-- Footer
root:tag('tr')
:tag('td')
:attr('colspan', '7')
:css{['padding-top'] = '3px', ['padding-bottom'] = '5px', ['font-size'] = '78%', ['text-align'] = 'left'}
:wikitext(' ' .. makeWikilink(monthAndYear, 'More ' .. monthAndYear .. ' events...'))
return tostring(root)
end
return p