Module:Events
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Events/doc
local p = {}
local dplhelper = require('Module:DplHelper')
local getArgs = require('Module:Arguments').getArgs
function p.renderCurrentEvents(frame)
args = getArgs(frame)
return p._renderCurrentEvents(frame, nil, args.template)
end
function p.renderFutureEvents(frame)
args = getArgs(frame)
return p._renderFutureEvents(frame, nil, args.template)
end
function p.renderCurrentAndFutureEvents(frame)
local now = os.time()
-- current events are from 3 hours ago to 36 hours into the future
local current_start = now - (3 * 60 * 60)
local current_end = now + (36 * 60 * 60)
-- future starts after current and ends 90 days later
local future_start = current_end
local future_end = current_end + (90 * 24 * 60 * 60)
args = getArgs(frame)
local data = p.fetchEvents(current_start, future_end)
local template_separator = args.template_separator or ''
return p._renderCurrentEvents(frame, data, args.template_current) ..
template_separator ..
p._renderFutureEvents(frame, data, args.template_future)
end
function p.fetchEvents(period_start, period_end)
-- Only collect events from years in period
local year_start = tonumber(os.date('%Y', period_start))
local year_end = tonumber(os.date('%Y', period_end))
local titles = {}
for year = year_start,year_end do
table.insert(titles, 'Events/' .. year)
end
local frame = mw.getCurrentFrame()
local fields = { 'group', 'name', 'wiki', 'time_known', 'time_start', 'time_end', 'image', 'image_soon', 'jpimage', 'jpimage_soon', 'unf_element', 'element', 'timers' }
local temp = dplhelper.fetchByTitles(frame, 'Events', 'EventHistory', titles, fields, function(entry)
entry.utc_timestamp_start = tonumber(frame:callParserFunction('#time:U', { entry.time_start }))
if (entry.time_end == nil) or (entry.time_end == '') then
entry.utc_timestamp_end = 0
else
entry.utc_timestamp_end = tonumber(frame:callParserFunction('#time:U', { entry.time_end }))
end
if entry.utc_timestamp_start == nil then
entry.utc_timestamp_start = 0
end
if entry.utc_timestamp_end == nil then
entry.utc_timestamp_end = 0
elseif entry.utc_timestamp_end < entry.utc_timestamp_start then
entry.utc_timestamp_end = 0
end
entry.jst_timestamp_start = entry.utc_timestamp_start + (9 * 60 * 60)
entry.jst_timestamp_end = entry.utc_timestamp_end + (9 * 60 * 60)
return entry
end)
local data = p.filterEvents(temp, period_start, period_end)
table.sort(data, function(a, b)
return a.utc_timestamp_start < b.utc_timestamp_start
end)
return data
end
function p.filterEvents(events, period_start, period_end)
local result = {}
for i = 1,#events do
local entry = events[i]
if (entry.utc_timestamp_end == 0) and (entry.utc_timestamp_start <= period_end) and (entry.utc_timestamp_start >= period_start) then
table.insert(result, entry)
elseif (entry.utc_timestamp_start < period_end) and (entry.utc_timestamp_end > period_start) then
table.insert(result, entry)
end
end
return result
end
function p._renderEvents(frame, events, template)
local result = {}
local index = 0
for key,event in pairs(events) do
index = index + 1
event.index = index
table.insert(result, frame:expandTemplate{title = template, args = event})
end
return table.concat(result, '')
end
function p._renderCurrentEvents(frame, data, template)
local now = os.time()
local period_start = now - (3 * 60 * 60)
local period_end = now + (36 * 60 * 60)
local events = {}
if data == nil then
events = p.fetchEvents(period_start, period_end)
else
events = p.filterEvents(data, period_start, period_end)
end
return p._renderEvents(frame, events, template)
end
function p._renderFutureEvents(frame, data, template)
local now = os.time()
local period_start = now + (36 * 60 * 60)
local period_end = period_start + (90 * 24 * 60 * 60)
if data == nil then
data = p.fetchEvents(period_start, period_end)
end
local events = {}
for i = 1,#data do
local entry = data[i]
if entry.utc_timestamp_start > period_start then
table.insert(events, entry)
end
end
return p._renderEvents(frame, events, template)
end
function p.test()
local frame = mw.getCurrentFrame()
local now = os.time()
-- current events are from 3 hours ago to 36 hours into the future
local current_start = now - (3 * 60 * 60)
local current_end = now + (36 * 60 * 60)
-- future starts after current and ends 90 days later
local future_start = current_end
local future_end = current_end + (90 * 24 * 60 * 60)
local events = p.fetchEvents(future_start, future_end)
mw.logObject(events)
end
return p