Module:Calendar
From Angelina Jordan Wiki
Documentation for this module may be created at Module:Calendar/doc
local p = {}
local function trim(s)
if not s then return nil end
return (tostring(s):gsub("^%s*(.-)%s*$", "%1"))
end
function p.month(frame)
local args = frame.args or (frame.getParent and frame:getParent().args) or {}
local year = tonumber(trim(args.year))
local month = tonumber(trim(args.month))
if not year or not month then
return "Error: need |year= and |month=."
end
local tableName = trim(args.table) or "DatedEvents"
local dateField = trim(args.datefield) or "Date"
local urlField = trim(args.urlfield) or "Link"
local daysInMonth = tonumber(os.date("*t", os.time{year=year, month=month+1, day=0}).day)
local firstWday = 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)
local results = {}
if mw.ext and mw.ext.cargo and mw.ext.cargo.query then
results = mw.ext.cargo.query(
tableName,
dateField .. "," .. urlField,
{ where = string.format("%s >= '%s' AND %s <= '%s'", dateField, startDate, dateField, endDate),
limit = 500 }
) or {}
end
local events = {}
for _, row in ipairs(results) do
local d = tostring(row[dateField]):match("%d%d%d%d%-%d%d%-(%d%d)")
if d then
events[tonumber(d)] = row[urlField]
end
end
local out = {}
table.insert(out, '{| class="wikitable" style="text-align:center;"')
table.insert(out, '!Sun !!Mon !!Tue !!Wed !!Thu !!Fri !!Sat')
table.insert(out, '|-')
local col = 1
for i = 1, firstWday - 1 do
table.insert(out, '| ')
col = col + 1
if col <= 7 then table.insert(out, '||') end
end
for day = 1, daysInMonth do
if col > 7 then
table.insert(out, '|-')
col = 1
end
if events[day] then
table.insert(out, string.format('| style="color:red;" | [[%s|%d]]', events[day], day))
else
table.insert(out, '|' .. day)
end
col = col + 1
if col <= 7 then table.insert(out, '||') end
end
table.insert(out, '|}')
return table.concat(out, "\n")
end
return p