Module:ListCurrentRosterPortals

local util_args = require('Module:ArgsUtil') local util_cargo = require("Module:CargoUtil") local util_html = require("Module:HtmlUtil") local util_map = require("Module:MapUtil") local util_table = require("Module:TableUtil") local util_text = require("Module:TextUtil") local util_title = require("Module:TitleUtil") local util_vars = require("Module:VarsUtil") local i18n = require('Module:i18nUtil') local lang = mw.getLanguage('en')

local h = {}

local p = {} function p.main(frame) local args = util_args.merge i18n.init('ListCurrentRosterPortals') local currentPortals = h.makeAndRunQuery(h.getStartDate) return h.makeOutput(currentPortals) end

function h.getStartDate return util_title.titleparts(nil, 1, 2) end

function h.makeAndRunQuery(args) return util_cargo.queryAndCast(h.getQuery(args), 'Region', 'Page') end

function h.getQuery(startDate) local ret = { tables = { 'RosterChangePortalDates=RCPD', 'Regions' }, join = 'RCPD.Region=Regions.RegionLong', where = h.getWhere(startDate), fields = { 'RCPD.Region', 'RCPD.OverviewPage=Page' }, orderBy = 'Regions.Priority' }	return ret end

function h.getWhere(startDate) local tbl = { ('RCPD.DateEnd >="%s"'):format(startDate), ('RCPD.DateStart <="%s"'):format(h.getEndDateFromStartDate(startDate)) }	return util_cargo.concatWhere(tbl) end

function h.getEndDateFromStartDate(startDate) return lang:formatDate('Y-m-d', startDate .. ' + 7 days') end

function h.makeOutput(currentPortals) local output = mw.html.create output:wikitext(i18n.print('currentPortals')) local ul = output:tag('ul') h.recordPortalsInDOM(ul, currentPortals) h.printPortalList(ul, currentPortals) return output end

function h.recordPortalsInDOM(ul, currentPortals) ul:attr('id', 'current-portal-list') :attr('data-current-portals', h.getPortalsAttr(currentPortals)) end

function h.getPortalsAttr(currentPortals) return util_table.concat(util_map.extractField(currentPortals, 'Page'), ',') end

function h.printPortalList(ul, currentPortals) for _, row in ipairs(currentPortals) do		ul:tag('li') :wikitext(h.getLink(row)) end end

function h.getLink(row) return util_text.intLinkOrText(row.Page, row.Region) end

return p