Module:Calendar
From Angelina Jordan Wiki
Documentation for this module may be created at Module:Calendar/doc
local p = {}
function p.month(frame)
local args = frame:getParent().args
local year = tonumber(args.year)
local month = tonumber(args.month)
-- configurable Cargo parameters
local tableName = args.table or 'DatedEvents'
local dateField = args.datefield or 'Date'
local urlField = args.urlfield or 'URL'
-- month boundaries
local daysInMonth = os.date("*t", os.time{year=year, month=month+1, day=0}).day
local firstWeekday = tonumber(os.date("*t", os.time{year=year, month=month, day=1})).wday
local startDate = string.format("%04d-%02d-01", year, month)
local endDate = string.format("%04d-%02d-%02d", year, month, daysInMonth)
-- query Cargo
local results = mw.ext.cargo.query(
tableName,
dateField .. ',' .. urlField,
{ where = string.format("%s >= '%s' AND %s <= '%s'", dateField, startDate, dateField, endDate) }
)
-- map events
local events = {}
for _, row in ipairs(results) do
local dateVal = row[dateField]
local urlVal = row[urlField]
local y, m, d = dateVal:match("(%d+)%-(%d+)%-(%d+)")
if y and m and d then
events[tonumber(d)] = urlVal
end
end
-- build table
local out = {}
table.insert(out, '{| class="wikitable" style="text-align:center;"')
table.insert(out, '!Sun !!Mon !!Tue !!Wed !!Thu !!Fri !!Sat')
local day, col = 1, 1
table.insert(out, '|-')
for i = 1, firstWeekday - 1 do
table.insert(out, '| ')
if i < 7 then table.insert(out, '||') end
col = col + 1
end
while day <= daysInMonth do
if col > 7 then
table.insert(out, '\n|-')
col = 1
end
local cell
if events[day] then
cell = string.format('| style="color:red;" | [[%s|%d]]', events[day], day)
else
cell = '|' .. day
end
table.insert(out, cell)
if col < 7 then table.insert(out, '||') end
day = day + 1
col = col + 1
end
while col <= 7 do
table.insert(out, '| ')
if col < 7 then table.insert(out, '||') end
col = col + 1
end
table.insert(out, '\n|}')
return table.concat(out, ' ')
end
return p