Module:PlayerTabs

From Liquipedia Chess Wiki

Documentation for this module may be created at Module:PlayerTabs/doc

local PlayerTabs = {}

local Class = require('Module:Class')
local Table = require('Module:Table')
local Page = require('Module:Page')
local Tabs = require('Module:Tabs')
local String = require('Module:StringUtils')
local Variables = require('Module:Variables')

local SPECIAL_SUBPAGES = {
	'Results',
	'Played Matches',
	'Coaching',
	'Broadcasts',
}
local DEFAULT_SUBPAGES = {}

function PlayerTabs.run(args)
	args = args or {}
	local pageName = mw.title.getCurrentTitle().prefixedText
	-- first remove any year range sub sub page stuff
	pageName = string.gsub(pageName, '/[%d%-]+$' , '')

	local subpageName = string.gsub(pageName, '.*/([^/]+)$', '%1')
	local player = string.gsub(pageName, '(.*)/[^/]-$', '%1')

	--build display title
	local displayTitle = args.title
	if not displayTitle then
		if player ~= subpageName then
			displayTitle = args.displayName
			if not displayTitle then
				local data = mw.ext.LiquipediaDB.lpdb('player', {
					conditions = '[[pagename::' .. player:gsub(' ', '_') .. ']]',
					query = 'id',
				})
				displayTitle = (data[1] or {}).id or player
			end
			if Table.includes(SPECIAL_SUBPAGES, subpageName) or Table.includes(DEFAULT_SUBPAGES, subpageName) then
				displayTitle = displayTitle .. ': ' .. subpageName
			end
		end
	end
	if displayTitle then
		Variables.varDefine('displayTitle', displayTitle)
	end

	local yearTabs = PlayerTabs._yearTabs(args, subpageName, player)

	return PlayerTabs._display(player, subpageName, yearTabs)
end

function PlayerTabs._yearTabs(args, subpageName, player)
	local prefix = string.lower(subpageName)
	local linkPrefix = player .. '/' .. subpageName
	local tabArgs = {}
	local index = 1
	while args[prefix .. index] do
		local link = string.gsub(args[prefix .. index], ' ', '')
		link = string.gsub(link, '–', '-')

		if String.contains(string.lower(link), 'present') then
			tabArgs['link' .. index] = linkPrefix
		else
			tabArgs['link' .. index] = linkPrefix .. '/' .. link
		end
		tabArgs['name' .. index] = string.gsub(link, '-', ' – ')
		
		index = index + 1
	end

	if Table.isNotEmpty(tabArgs) then
		return tabArgs
	end
end

function PlayerTabs._display(player, subpageName, yearTabs)
	local tabArgs = {
		name1 = 'Overview',
		link1 = player,
	}

	local currentTab
	local tabCounter = 1
	-- add default sub pages that every player has to have
	for _, item in ipairs(DEFAULT_SUBPAGES) do
		tabCounter = tabCounter + 1
		tabArgs['name' .. tabCounter] = item
		tabArgs['link' .. tabCounter] = player .. '/' .. item
		if item == subpageName then
			currentTab = tabCounter
		end
	end

	-- add special sub pages that some might have
	-- only add them if the according sub page actually exists
	for _, item in ipairs(SPECIAL_SUBPAGES) do
		if Page.exists(player .. '/' .. item) then
			tabCounter = tabCounter + 1
			tabArgs['name' .. tabCounter] = item
			tabArgs['link' .. tabCounter] = player .. '/' .. item
			if item == subpageName then
				currentTab = tabCounter
			end
		end
	end

	player = player:gsub(' ', '_')

	tabCounter = tabCounter + 1
	tabArgs['name' .. tabCounter] = statistics

	tabArgs.This = currentTab

	if yearTabs and currentTab then
		tabArgs['tabs' .. currentTab] = Tabs.static(yearTabs)
	end

	return Tabs.static(tabArgs)
end

return Class.export(PlayerTabs)