Module:DataTabs

local util_args = require('Module:ArgsUtil') local util_cargo = require("Module:CargoUtil") local util_map = require("Module:MapUtil") local util_page = require("Module:PageUtil") local util_title = require("Module:TitleUtil") local util_vars = require('Module:VarsUtil') local Tabs = require('Module:TabsHeader').fromTables

local i18n = require('Module:i18nUtil')

local h = {} local p = {} function p.main(frame) local args = util_args.merge i18n.init('DataTabs') local title_obj = mw.title.getCurrentTitle local title = title_obj.text local overviewPage = args.basepage or title:match('^(.*)(/%d%d?)$') or title local links = h.getAllDataPages(overviewPage) local lookup = h.makeLookup(links, args.basepage) h.sortLinks(links, lookup) local names = h.getNames(#links) local this = lookup[title_obj.prefixedText] util_vars.setVar('N_Page', tonumber(util_title.titleparts(nil, 1, -1)) or 1) util_vars.setVar('MVPPoints', args.mvppoints or 1) util_vars.setVar('overviewpage', overviewPage) util_vars.setVar('Bestof', args.bestof) h.setGroupVariables(overviewPage) local div = h.makeDiv(overviewPage, args) if #links > 1 then return Tabs(names, links, this), div end return div end

function h.getAllDataPages(page) return util_page.getListOfPages(('_pageName RLIKE "%s"'):format( ('^Data:%s(/[0-9]+)?$'):format(page) )) end

function h.makeLookup(links, basepage) local lookup = {} for _, v in ipairs(links) do		if basepage then lookup[v] = tonumber(v:match(basepage .. '/(%d+)$') or 1) else lookup[v] = tonumber(v:match('/(%d+)$') or 1) end end return lookup end

function h.sortLinks(links, lookup) table.sort(links, function(a,b)		return lookup[a] < lookup[b]	end) end

function h.getNames(n) local names = {} for i = 1, n do names[i] = 'Page ' .. i	end return names end

function h.setGroupVariables(overviewPage) local data = util_cargo.getConstDict({		tables = { 'TournamentGroups' },		where = util_cargo.whereFromArg('OverviewPage="%s"', overviewPage),		fields = { 'Team', 'GroupName' },	}, 'Team', 'GroupName') for team, group in pairs(data) do util_vars.setVar('Group_' .. team, group or 'kittens') end end

function h.makeDiv(overviewPage, args) local div = mw.html.create('div') :attr('id','data-ns-pageinfo') :attr('data-overviewpage', overviewPage) :attr('data-extra-purges', args.extra_purges) :attr('data-pickban',util_args.boolToStringYN(true)) if util_args.castAsBool(args.timeline) then div:attr('data-timeline',util_args.boolToStringYN(true)) :wikitext(i18n.print('yesTimeline')) end if args.fl then div:attr('data-page-link', h.getFlLinks(args.fl)) div:attr('data-template-link', h.getFlTemplateLinks(args.fl)) end return div end

function h.getFlLinks(str) return util_map.splitAndConcat(str, nil, h.formatOneFlLink,',') end

function h.formatOneFlLink(str) return 'Project:Featured Leagues/' .. str end

function h.getFlTemplateLinks(str) return util_map.splitAndConcat(str, nil, h.formatOneFlTemplateLink,',') end function h.formatOneFlTemplateLink(str) return 'Project:Featured Leagues/Template/' .. str end

return p