Module:MatchCalendarExport

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_esports = require('Module:EsportsUtil') local util_table = require('Module:TableUtil') local util_time = require('Module:TimeUtil') local util_tournament = require('Module:TournamentUtil') local util_vars = require('Module:VarsUtil') local i18n = require('Module:i18nUtil')

local m_team = require('Module:Team')

local HEADINGS = { 'subject', 'startDate', 'startTime' }

local h = {}

local p = {} function p.main(frame) i18n.initGlobalFromFile('MatchCalendarExport') local args = util_args.merge(true) local data = h.getData(args) util_table.mapInPlace(data, h.processRow) return h.makeOutput(data) end

function h.getData(args) return util_cargo.queryAndCast(h.getQuery(args)) end

function h.getQuery(args) local ret = { tables = 'MatchSchedule=MS,Tournaments=IT', join = 'MS.OverviewPage=IT._pageName', fields = { 'MS.Team1=Team1', 'MS.Team2=Team2', 'IT.Name=Name', 'MS.DateTime_UTC=DateTime_UTC', },		where = h.getWhere(args), limit = 9999, orderBy = 'MS.DateTime_UTC ASC', }	return ret end

function h.getWhere(args) local tbl = { 'MS.DateTime_UTC IS NOT NULL', util_cargo.concatWhereOr(h.getTournamentWhere(args[1] or args.tournament)), }	return util_cargo.concatWhere(tbl) end

function h.getTournamentWhere(tournament) if not tournament then return nil end return util_args.splitAndMap(tournament, nil, function(t)		return ('MS.OverviewPage="%s"'):format(util_esports.getOverviewPage(t))	end) end

function h.processRow(row) local tbl = { ('%s - %s vs %s'):format(row.Name, m_team.short(row.Team1), m_team.short(row.Team2)), util_time.dateOnlyInLocal(row.DateTime_UTC), util_time.timeInLocal(row.DateTime_UTC), }	return util_table.concat(tbl, ',') end

function h.makeOutput(data) local output = mw.html.create output:wikitext(i18n.print('instructions')) local pre = output:tag('pre') h.printHeading(pre) pre:wikitext(util_table.concat(data,'\n')) return output end

function h.printHeading(pre) local tbl = {} for _, v in ipairs(HEADINGS) do		tbl[#tbl+1] = i18n.default(v) end pre:wikitext(util_table.concat(tbl, ','), '\n') end

return p