Module:GetEventPageInfo

From Call of Duty Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.


local util_cargo = require('Module:CargoUtil')
local util_page = require("Module:PageUtil")
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_vars = require('Module:VarsUtil')

local subpages = mw.loadData('Module:SubpageSettings').tournaments.names

local h = {}
local p = {}

function p.getEventPageInfo(frame)
	local title = mw.title.getCurrentTitle().text
	local parsed = h.parseTitle(title)
	
	local subpage = parsed.subpage .. (parsed.subsubpage ~= '' and '/' or '') .. parsed.subsubpage
	
	util_vars.setVar('overviewpage', parsed.event, frame)
	util_vars.setVar('subpage', subpage, frame)
	util_vars.setVar('subpagetype', parsed.subpage, frame)
	
	if parsed.subpage == 'Scoreboards' then
		h.setScoreboardVars(parsed.event)
	end
	
	local datadiv = h.getDataInfo(parsed.event)
	return datadiv
end

function h.parseTitle(title)
	local titleparts = mw.text.split(title, '/')
	local event = {}
	local subpage = {}
	local parsed = {}
	for _,titlepart in ipairs(titleparts) do
		if subpages[titlepart] then
			-- event is everything leading up to here
			event = subpage
			-- subpage will be the rest
			subpage = {[1] = titlepart}
		else
			-- just keep adding to whichever part we are in
			subpage[#subpage+1] = titlepart
		end
	end
	
	-- case regular page
	if event[1] or subpages[titleparts[1]] then
		parsed.event = table.concat(event,'/')
		parsed.subpage = table.remove(subpage, 1)
		parsed.subsubpage = table.concat(subpage, '/')
	-- case overview page
	else
		parsed.event = table.concat(subpage, '/')
		parsed.subpage = ''
		parsed.subsubpage = ''
	end
	return parsed
end

function h.getDataInfo(page)
	local allDataPages = h.getListOfAllDataPages(page)
	local div = mw.html.create('div')
		:attr('id','datapage-info')
		:attr('data-datapages', h.getNumberOfDataPages(allDataPages, page))
		:attr('data-has-participants-datapage', h.hasParticipantsDatapage(allDataPages, page))
		:attr('data-overviewpage', page)
	return div
end

function h.getListOfAllDataPages(page)
	local result = util_page.getListOfPages(
		mw.ustring.format('_pageName RLIKE "^Data:%s(/[^/]+)?$"', page)
	)
	return result
end

function h.getNumberOfDataPages(allDataPages, page)
	pageEscaped = util_text.escape(page)
	local counter = 0
	for _, title in ipairs(allDataPages) do
		-- technically we don't like this regex
		-- the / should really be in a capture group along with [0-9]+
		-- and ? after the entire thing
		-- too bad lua's string matching is terrible
		-- but tbh, combined with the regex we have in the query this is pretty safe
		if mw.ustring.find(
			title,
			mw.ustring.format('^Data:%s/?[0-9]*$', pageEscaped)
		) then
			counter = counter + 1
		end
	end
	return counter
end

function h.hasParticipantsDatapage(allDataPages, page)
	if util_table.keyOf(allDataPages, ('Data:%s/Participants'):format(page)) then
		return 'Yes'
	end
end

function h.setScoreboardVars(page)
	local row = h.doScoreboardQuery(page)
	if not row then return end
	for k, v in pairs(row) do
		util_vars.setVar('sb' .. k, v)
	end
end

function h.doScoreboardQuery(page)
	local query = {
		tables = 'Tournaments',
		fields = { 'Region', 'TournamentLevel', 'IsQualifier', 'IsPlayoffs', 'IsOfficial', 'Year', 'League', 'OverviewPage' },
		where = ('OverviewPage="%s"'):format(page)
	}
	return util_cargo.getOneRow(query)
end

return p