Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:ATP rankings

Permanently protected module
From Wikipedia, the free encyclopedia

local mDisambiguation = require('Module:Disambiguation')
local mRedirect = require('Module:Redirect')

local function getData( ranking )
	if ranking == "singles" then
		return mw.loadJsonData( "Module:ATP rankings/data/singles.json" )
	end
end

local function formatName( nameIn )
	local name = nameIn
	local comma = mw.ustring.find( name, ", " )
	if comma == nil then
		return name
	end
	
	name = mw.ustring.sub( name, comma + 2, mw.ustring.len( name ) ) .. " " .. mw.ustring.sub( name, 1, comma - 1 )
	
	-- Check, in order, the existence of:
	--		- Page is on override list
	--		- Page with (tennis) disambiguator exists
	--		- Page exists
	-- If page exists, flag as a dab, otherwise return an unlinked name
	local overrides = mw.loadJsonData( "Module:ATP rankings/data/overrides.json" )
	if overrides[name] and overrides[name]["article"] then
		if overrides[name]["article"] == "x" then
			return name
		else
			return "[[" .. overrides[name]["article"] .. "|" .. name .. "]]"
		end
	end

	local title = mw.title.new( name )
	
	local defaultDisambig = mw.title.new( name .. ' (tennis)' )
	if defaultDisambig.exists then
		return "[[" .. defaultDisambig.fullText .. "|" .. name .. "]]"
	end

	if title.exists then
		local content = title:getContent()
		if not mDisambiguation.isDisambiguation( content ) and not mDisambiguation.isDisambiguation(mRedirect.luaMain(content)) then
			return "[[" .. name .. "]]"
		end
	
		return "[[" .. name .. "]]" .. "[[Category:ATP rankings call with disambiguated page title]]"
	end
	
	return name
end

local p = {}

function p.createNavbox( frame )
	local ranking = frame.args[1]
	local country = frame.args[2]
	local nationality = frame.args[3]
	
	local data = getData( ranking )
	local templateParams = {}
	
	local lang = mw.getContentLanguage()
	
	local countryDataCur = data["current"]["per-country"][country]
	local countryDataPrev = data["previous"]["per-country"][country]
	
	local flagicon

	if type(countryDataCur) ~= "table" then
		local countryAliases = mw.loadData( "Module:Country alias/data" )
		if type(countryAliases.countryAliases[country]) ~= "string" and type(countryAliases.countries[country]) ~= "table" then
			error( "Invalid country code '" .. country .. "' specified for Module:ATP rankings" )
		end
	end
	
	if country == "GEO" then
		flagicon = "[[File:Flag of Georgia.svg|23x15px|link=Georgia (country)]]"
	else
		local mCountryAlias = require( "Module:Country alias" )
		flagicon = "[[File:" .. mCountryAlias._templateMain{flag = 'yes', country = country} ..
			"|23x15px|link=" .. mCountryAlias._templateMain{country = country} .. "]]"
	end
	
	templateParams["name"] = "Top male singles tennis players by country"
	templateParams["title"] = "[[Association of Tennis Professionals]] " .. flagicon .. " Top " .. nationality .. " male " .. ranking .. " tennis players"
	templateParams["above"] = "''As of " .. data["current"]["as-of"] .. "''"
	templateParams["state"] = frame:getParent().args["state"] or "autocollapse"
	templateParams["listclass"] = "hlist"
	templateParams["list1"] = ""
	
	local mNavbox = require('Module:Navbox')
	
	if type(countryDataCur) ~= "table" then
		return require("Module:If preview")._warning{"No ranked players from '" ..
			country .. "'. Navbox will be empty!"} .. mNavbox['_navbox'](templateParams)
	end
	
	local lastRankings = {}
	for i, entry in ipairs( countryDataPrev ) do
		lastRankings[entry["name"]] = entry["rank"]
	end

	local listKey = "list1"
	for i, entry in ipairs( countryDataCur ) do
		if i == 6 then
			templateParams["list2"] = ""
			listKey = "list2"
		elseif i == 11 then
			break
		end
		
		local changeDirection = "increase"
		local changeAmount = ""
		if lastRankings[entry["name"]] == nil then
			-- do nothing
		elseif lastRankings[entry["name"]] == entry["rank"] then
			changeDirection = "steady"
		elseif lastRankings[entry["name"]] >= entry["rank"] then
			-- decrease in value means in increase in ranking
			changeDirection = "increase"
			changeAmount = lastRankings[entry["name"]] - entry["rank"]
		else
			changeDirection = "decrease"
			changeAmount = entry["rank"] - lastRankings[entry["name"]]
		end

		changeDirection = frame:expandTemplate{ title = changeDirection }
		
		local tied = ""
		if entry["tied"] then
			tied = "T"
		end

		templateParams[listKey] = templateParams[listKey] .. "* " .. i .. ". " .. formatName( entry["name"] ) .. " (" .. tied .. lang:formatNum(entry["rank"]) .. " " .. changeDirection .. changeAmount .. ")\n"
	end

	return mNavbox['_navbox'](templateParams)
end

return p