پودمان:SportsRankings

توضیحات پودمان[نمایش] [ویرایش] [تاریخچه] [پاکسازی]

Module to display sports rankings of countries in various sports. The data on the rankings is held in module subpages so the lists can be updated in one location.

Data subpages

Templates for single country rank (with flag and change):

Template to generate rankings table:

Usage ویرایش

  • Show ranking of named country with change:

{{#invoke:SportsRankings|main|country|mdy}}

  • List ranking from start ranking to end ranking (default: 1-10):

{{#invoke:SportsRankings|list|FIFA World Rankings|first|last}}

  • List ranking from for names country with number of countries above and below:

{{#invoke:SportsRankings|list2|FIFA World Rankings|country name|number to display above and below named country}}

  • Show ranking of a confederation:

{{#invoke:SportsRankings|list|FIH World Rankings|first|last|select=confederation|rank_header=FIH}}

Country examples:

Country examples Code Output
FIFA World Rankings for France {{#invoke:SportsRankings|main |FIFA World Rankings|FRA}}

Usage ویرایش

This template ({{FIFA World Rankings}}) is meant to help quickly update the FIFA World Rankings for all 211 men's national teams of FIFA, displayed in the infobox on each national team article. When given a country code, the template outputs the FIFA World Ranking of a country, along with a movement indicator (increase, decrease, or steady), and by how many spots a country moves, if applicable. The date of the rankings is also listed, along with a reference to the rankings from FIFA's website.

If desired, the FIFA ranking reference can be used elsewhere in the article using the name "FIFA World Rankings" (i.e. <ref name="FIFA World Rankings"/>).

Parameters ویرایش

  • 1 (unnamed): FIFA country code
  • mdy: Switches dates to month-day-year format

Example ویرایش

An example of the code used to display the ranking of Gibraltar:

{{FIFA World Rankings|GIB}}

This code produces the following:

۲۰۱ کاهش ۱ (۶ آوریل ۲۰۲۳)[۱]

  1. "The FIFA/Coca-Cola World Ranking". FIFA. 6 آوریل 2023. Retrieved 6 April 2023.

The template can be tested at the sandbox page, Template:FIFA World Rankings/sandbox.

Updating ویرایش

The main template page is not meant to be updated. The rankings should be updated at Module:SportsRankings/data/FIFA World Rankings.

To update the rankings:

  1. On line 16, add the day and month (along with the year if necessary) of the current rankings
  2. On line 17, add the day and month (along with the year if necessary) of the previous rankings
  3. Between line 22 and 232, paste in the code for the new rankings (see generating code section)

and save. Once the cache of a national team page is purged, the updated ranking will be displayed.

Generating rankings code ویرایش

All positions can be quickly updated using a spreadsheet. For example, after copying the entire ranking list (211 rows from all five pages, unedited) from FIFA's ranking list, the following formula can be used in an external spreadsheet to generate the code necessary to update the data page (given the FIFA rankings begin in cell A1):

="          {  """&B1&""", "&A1&", "&E1&", "&C1&" },"

If unnecessary lines are included, then paste the text into a site to remove lines with the string , , ,.

See also ویرایش

World Football Elo Ratings for France {{#invoke:SportsRankings|main |World Football Elo Ratings|France}} ۲ افزایش ۱ (۱۴ اکتبر ۲۰۲۰)[۱]
FIBA World Rankings for USA {{#invoke:SportsRankings|main |FIBA World Rankings|USA}} ۱ بی‌تغییر (۱۸ سپتامبر ۲۰۱۸)[۲]
List example Output
FIFA World Rankings table (1-5) {{#invoke:SportsRankings|list |FIFA World Rankings |1 |5 }}
FIFA World Rankings تا ۶ آوریل ۲۰۲۳.[۳]
رتبه تغییر تیم امتیازات
۱ افزایش ۱  آرژانتین ۱۸۴۰٫۹۳
۲ افزایش ۱  فرانسه ۱۸۳۸٫۴۵
۳ کاهش ۲  برزیل ۱۸۳۴٫۲۱
۴ بی‌تغییر  بلژیک ۱۷۹۲٫۵۳
۵ بی‌تغییر  انگلستان ۱۷۹۲٫۴۳
FIBA World Rankings table (1-5) {{#invoke:SportsRankings|list |FIBA World Rankings |1 |5 }}
FIBA World Rankings تا ۱۸ سپتامبر ۲۰۱۸.[۴]
رتبه تغییر تیم امتیازات
۱ بی‌تغییر  ایالات متحده آمریکا ۷۶۷٫۱
۲ بی‌تغییر  اسپانیا ۷۰۴٫۲
۳ بی‌تغییر  فرانسه ۶۴۷٫۲
۴ بی‌تغییر  صربستان ۶۴۴٫۲
۵ بی‌تغییر  آرژانتین ۶۲۵٫۱
FIBA Women's World Rankings table (1-5) {{#invoke:SportsRankings|list |FIBA Women's World Rankings |1 |5 }}
FIBA Women's World Rankings تا ۱۵ فوریه ۲۰۲۲.[۵]
رتبه تغییر تیم امتیازات
۱ بی‌تغییر  ایالات متحده آمریکا ۸۲۳٫۹
۲ بی‌تغییر  اسپانیا ۶۸۵٫۷
۳ بی‌تغییر  استرالیا ۶۷۸
۴ بی‌تغییر  کانادا ۶۶۱٫۶
۵ افزایش ۱  بلژیک ۶۵۱٫۲
FIFA Women's World Rankings (1-5): {{#invoke:SportsRankings|list |FIFA Women's World Rankings |1 |5 }}
FIFA Women's World Rankings تا ۲۶ ژوئن ۲۰۲۰.[۶]
رتبه تغییر تیم امتیازات
۱ بی‌تغییر  ایالات متحده آمریکا ۲۱۸۱
۲ بی‌تغییر  آلمان ۲۰۹۰
۳ افزایش ۱  فرانسه ۲۰۳۶
۴ کاهش ۱  هلند ۲۰۳۲
۵ بی‌تغییر  سوئد ۲۰۰۷
FIH World Rankings by confederation {{#invoke:SportsRankings|list |FIH World Rankings |1 |65 |select=AfHF |rankheader=FIH }}
FIH World Rankings تا ۳۰ ژوئن ۲۰۱۹.[۷]
AfHF FIH تغییر تیم امتیازات
1 ۱۴ افزایش ۲  آفریقای جنوبی ۱۰۴۳
2 ۲۰ بی‌تغییر  مصر ۶۹۸
3 ۳۶ کاهش ۱  غنا ۲۲۸
4 ۴۹ کاهش ۱  کنیا ۱۵۳
5 ۵۷ افزایش ۱  نیجریه ۱۰۶
6 ۶۱ افزایش ۳  زیمبابوه ۸۴
World Football Elo Ratings(1-5): {{#invoke:SportsRankings|list |World Football Elo Ratings |1 |5 }}
World Football Elo Ratings تا ۱۴ اکتبر ۲۰۲۰.[۱]
رتبه تغییر تیم امتیازات
۱ بی‌تغییر  برزیل ۲۱۰۵
۲ افزایش ۱  فرانسه ۲۰۸۸
۳ کاهش ۱  بلژیک ۲۰۸۴
۴ افزایش ۳  پرتغال ۲۰۴۶
۵ کاهش ۱  اسپانیا ۲۰۲۷
UEFA Coefficient Rankings (+/-2): {{#invoke:SportsRankings|list2 |UEFA Coefficient Rankings |Luxembourg |2 }}
UEFA Coefficient Rankings تا ۲۳ اکتبر ۲۰۱۸.[۸]
رتبه تغییر تیم امتیازات
۴۱ افزایش ۲  لیتوانی ۶٫۷۵
۴۲ کاهش ۲  لتونی ۵٫۶۲۵
۴۳ افزایش ۵  لوکزامبورگ ۵٫۲۵
۴۴ افزایش ۲  ارمنستان ۵٫۲۵
۴۵ افزایش ۲  مالت ۵٫۱۲۵
IIHF World Rankings (1-5)

(with optional caption parameter):
{{#invoke:SportsRankings|list |IIHF World Rankings |1 |5 caption=Men's IIHF World Rankings (INSERT_UPDATE_DATE) }}
Men's IIHF World Rankings (۲۱ مه ۲۰۱۸)[۹]
رتبه تغییر تیم امتیازات
۱ بی‌تغییر  کانادا ۳۹۹۰
۲ افزایش ۱  سوئد ۳۹۴۵
۳ کاهش ۱  روسیه ۳۹۳۰
۴ افزایش ۲  ایالات متحده آمریکا ۳۷۶۵
۵ کاهش ۱  فنلاند ۳۷۶۵
IIHF Women's World Rankings (1-5)

(with optional caption parameter):
{{#invoke:SportsRankings|list |IIHF Women's World Rankings |1 |5 caption=Women's IIHF World Rankings (INSERT_UPDATE_DATE) }}


Usage

  1. ۱٫۰ ۱٫۱ Elo rankings change compared to one year ago. "World Football Elo Ratings". eloratings.net. 14 اکتبر 2020. Retrieved 14 October 2020.
  2. "FIBA Ranking Presented by Nike". FIBA. 18 September 2018. Retrieved 18 September 2018.
  3. "The FIFA/Coca-Cola World Ranking". FIFA. 6 آوریل 2023. Retrieved 6 April 2023.
  4. "FIBA Ranking Presented by Nike". FIBA. 18 September 2018. Retrieved 18 September 2018.
  5. "FIBA Women's Ranking Presented by Nike". FIBA. 15 February 2022. Retrieved 15 February 2022.
  6. "The FIFA/Coca-Cola Women's World Ranking". FIFA. 26 ژوئن 2020. Retrieved 26 June 2020.
  7. "FIH Hero World Rankings June 2019 – Men" (PDF). FIH. 30 June 2019. Retrieved 30 June 2019.
  8. "Member Associations-UEFA Coefficients". UEFA. 23 October 2018. Retrieved 23 October 2018.
  9. "IIHF Men's World Ranking". IIHF. 21 May 2018. Retrieved 21 May 2018.

require('Module:No globals');


local p = {}
local num_con = require('Module:Numeral converter').convert
local date2fa = require('Module:Date2fa').convert

local error_msg = '<span style=\"font-size:100%\" class=\"error\"><code style=\"color:inherit; border:inherit; padding:inherit;\">&#124;_template=</code> missing or empty</span>';

-- data for various rankings held in module subpages, e.g. "Module:SportsRankings/data/FIFA World Rankings"
local data = {}      --[[ parameters containing data help in three tables
						data.source = {}     -- parameters for using in cite web (title, url, website)
						data.updated = {}    -- date of latest update (month, day, year)
						data.rankings = {}   -- the rankings list (country code, ranking, movement)
					    data.alias = {}      -- alias list (country code, country name [=key])
					    
					--]]

local  templateArgs = {} -- contains arguments from template involking module


local function getArgs(frame)
	local parents = mw.getCurrentFrame():getParent()
		
	for k,v in pairs(parents.args) do
		--check content
		if v and v ~= "" then
			templateArgs[k]=v --parents.args[k]
		end
	end
	for k,v in pairs(frame.args) do
		--check content
		if v and v ~= "" then
			templateArgs[k]=v --parents.args[k]
		end
	end
	-- allow empty caption to blank default
	if parents.args['caption'] then templateArgs['caption'] = parents.args['caption'] end
	if frame.args['caption'] then templateArgs['caption'] = frame.args['caption'] end
end

local function loadData(frame)
    
    local source = frame.args[1] -- source of rankings e.g. FIFA World Rankings
    data = require('Module:SportsRankings/data/'.. source);
    
end

local function getDate(option)
   
   local dateTable = data.updated         -- there must be date table (data.updated)
                                          -- TODO add a warning and/or category
   if option == "LAST" then 
   		local lastDateTable = data.previous 
   		if lastDateTable then             -- there might not be a previous data table (data.previous)
   			dateTable = lastDateTable
	   else 
	   		return "No previous date available (data.updated missing)"
       end
   end
   
   if templateArgs['mdy'] and templateArgs['mdy'] ~= "" then
   	   return dateTable['month'] .. " " .. dateTable['day'] .. ", " .. dateTable['year']
   else
   	   return dateTable['day'] .. " " .. dateTable['month'] .. " " .. dateTable['year']
   end
end

local function addCiteWeb(frame)  -- use cite web template
	
	return frame:expandTemplate{ title = 'cite web' , args = {
    		url = data.source['url'],            --"https://www.fifa.com/fifa-world-ranking/ranking-table/men/index.html", 
			title = data.source['title'],        -- "The FIFA/Coca-Cola World Ranking",
			website = data.source['website'],    --"FIFA",
			['date'] = getDate(),
			['access-date'] = getDate()
			}}
end
local function addReference(frame)
	
	local text = ""
	if data.source['text'] then text = data.source['text'] end
	
	return frame:expandTemplate{ title = 'refn' , args = {
		name=frame.args[1],                                 --ranking used, e.g. "FIFA World Rankings",
	    text .. addCiteWeb(frame)
	}}

end

--[[ the main function returning ranking for one country
      - takes three-letter country code or name of country as parameters
      - displays as rank | movement |date
]]
function p.main(frame)
	
    getArgs(frame) -- returns args table having checked for content
    loadData(frame)
    local outputString = ""
    local validCode = false
    local country = templateArgs[2] -- country name or county code passed as parameter
    local rank, move
    
    if string.len( country) ==  3 then -- if we have a three letter country code 
	    for _,u in pairs(data.alias) do  -- run through alias list { 3-letter code, country name }
	    	if u[1]==country then        -- if code = passed parameter
	       		country = u[2]           -- set country name as key for ranking table
	       		validCode = true
	       		break
	       	end
	    end    
	    -- if no match of code to country name, set category
	    if not validCode then
	    	outputString="[[رده:صفحه‌هایی که رده‌بندی ورزشی با پارامترهای نامعلوم استفاده می‌کنند]]" .. outputString
        end
    end
    for _,v in pairs(data.rankings) do
    	if v[1]==country then 
       		rank = v[2]    -- get rank
       		move = v[3]    -- get move from last ranking
       		break
       	end
    end
    if not rank then -- no ranking found (do we want a tracking for no rank found?)
    	rank = 'NR' 
	    --outputString="[[Category:Pages using SportsRankings with unknown parameters]]" .. outputString
        --outputString="[[Category:Pages using SportsRankings with no ranking]]" .. outputString
	end
	
	if rank ~= 'NR' then
		outputString = outputString .. ' ' .. num_con("fa", rank) .. ' '
		if move < 0 and math.abs( move ) == math.abs( rank ) then -- new teams in ranking: move = -ranking
			outputString = outputString .. frame:expandTemplate{ title = 'new entry' } 
	    elseif move == 0 then                                    -- if no change in ranking
	    	outputString = outputString .. frame:expandTemplate{ title = 'steady' } 
	    elseif move < 0 then                                 --  if ranking down
	    	outputString = outputString .. frame:expandTemplate{ title = 'decrease' } 
	    	if move ~= -1000 then outputString = outputString .. ' ' .. num_con("fa", math.abs(move)) end
	    elseif move > 0 then                                 -- if ranking up
	    	outputString = outputString .. frame:expandTemplate{ title = 'increase' } 
	    	if move ~= 1000 then outputString = outputString .. ' ' .. num_con("fa", move) end
	    end	
    else
    	outputString = outputString .. frame:expandTemplate{ title = 'Abbr', args = { "NR", "Not ranked"}  }
    	--	{{Abbr|NR|Not ranked}} 
	end
	outputString = outputString .. ' (' .. date2fa(getDate()) .. ')'
	outputString = outputString .. addReference(frame)
    
    return outputString
	
end

--[[  outputs a table of the rankings 
        called by list() or list2() 
        positional parameters - |ranking|first|last the ranking to use, fist and last in table
        other parameters: |style=               -- CSS styling
                          |headerN= footerN=    -- displays header and footer rows with additional information
                          |caption=             -- value of caption to display
                                                -- by default it generates a caption
                                                -- this can be suppressed with empty |caption=
]]
local function table(frame, ranking, first,last)

    local styleString = ""
    if templateArgs['style'] and templateArgs['style'] ~= "" then styleString = templateArgs['style'] end
    
    
    local sublist2 = { "ENG", "SCO", "WAL", "IRE", "NIR", "FRA", "England", "France", "Germany" }
    local sublist3 = { "AFG","AUS","BAN","BHR","BHU","BRU","CAM","CHN","GUM","HKG","IDN","IND","IRN","IRQ","JOR",
    	              "JPN","KGZ","KOR","KSA","KUW","LAO","LIB","MAC","MAS","MDV","MNG","MYA","NEP","OMA","PAK",
    	              "PHI","PLE","PRK","QAT","SIN","SRI","SYR","THA","TJK","TKM","TLS","TPE","UAE","UZB","VIE",
    	              "YEM" }
    local lastRank = 0
    local selectCount = 0
    local selectData = nil
    local selectList = nil
    if templateArgs['select'] then 
    	if data.confederation[templateArgs['select']] then
	    	selectList = templateArgs['select']
	    	selectData = data.confederation[selectList]
	    	selectCount = 1
    	end
    end
    
    -- column header customisation
    local rankHeader = templateArgs['rank_header'] or "رتبه"
    local selectionHeader = templateArgs['selection_header'] or selectList or "رتبه"
    local teamHeader = templateArgs['team_header'] or "تیم"
    local pointsHeader = templateArgs['points_header'] or "امتیازات"
    local changeHeader = templateArgs['change_header'] or "تغییر"
    
    --start table
    local outputString = '{| class="wikitable" style="text-align:center;' .. styleString .. '"'
    
    -- add default or custom caption
    local caption = ranking .. ' تا ' .. date2fa(getDate()) .. '.'
    if templateArgs['caption'] and templateArgs['caption']  ~= "" then 
    	caption = templateArgs['caption'] 
    	caption = p.replaceKeywords(caption)
    end
    if not (templateArgs['caption'] and templateArgs['caption']  == "") then 
		outputString = outputString ..	'\n|+' .. caption .. addReference(frame)
    end
    
    -- add header rows (logo, date of update etc)
    local count = 0
    local header = {}
    local tableWidth = 4
    if selectList then tableWidth = 5 end
    while count < 5 do
    	count = count + 1
	    if templateArgs['header'..count] then
	    	header[count] = templateArgs['header'..count] 
	    	header[count] = p.replaceKeywords( header[count])
	    	outputString = outputString ..	'\n|-\n| colspan="'.. tableWidth .. '" |' .. header[count]
	    end
    end
    
    -- add the add part of the table
    local optionalColumn = ""
    if selectList then
    	optionalColumn = '\n!' .. selectionHeader 
    end
   	outputString = outputString .. '\n|-' .. optionalColumn
    	                        .. '\n!' .. rankHeader .. '\n!' .. changeHeader 
    	                        .. '\n!' .. teamHeader .. '\n!' .. pointsHeader
   
    local change,code = '', ''
    --while i<last do 
    for k,v in pairs(data.rankings) do
	   --v[2] = tonumber(v[2])
	   if v[2] >= first and v[2] <= last then 

		   for _,u in pairs(data.alias) do  -- get country code from name
		    	if u[2]==v[1] then 
		       		code = u[1]    -- if alias (country code) then use country name as key
		       		break
		       	end
		    end   
	   	   
	   	    local continue = true
	   	    if selectList then                 -- select from list
	   	   	    continue = false 
	   			for _,u in pairs(selectData) do
	   				if u == v[1] or u == code then 
	   					continue = true 
	   					break
	   				end
	   			end
	   	    end
		
			if continue ==true  then 
	   	   
			   local rowString = '\n|-'
			   if selectList then 
			   	    local selectRank = selectCount
			   	    if v[2]==lastRank then selectRank = selectCount -1 end -- only handles two at same rank
					rowString = rowString ..  '\n|' .. selectRank 
					selectCount = selectCount + 1
			   end
			   rowString = rowString .. '\n|' .. num_con("fa", v[2])  -- rank
			   lastRank = v[2]
			   
			   local move = v[3]
			   if move < 0 and math.abs( move ) == math.abs( v[2] ) then -- new teams in ranking: move = -ranking
					change = frame:expandTemplate{ title = 'new entry' } 
			   elseif move == 0 then                                    -- if no change in ranking
			    	change = frame:expandTemplate{ title = 'steady' } 
			    elseif move < 0 then                                 --  if ranking down
			    	change = frame:expandTemplate{ title = 'decrease' } .. ' ' .. num_con("fa", math.abs(move))
			    elseif move > 0 then                                 -- if ranking up
			    	change = frame:expandTemplate{ title = 'increase' } .. ' ' .. num_con("fa", move)
			    end	
			   rowString = rowString .. '||' .. change
			   
	--[[		   for _,u in pairs(data.alias) do
			    	if u[2]==v[1] then 
			       		code = u[1]    -- if alias (country code) then use country name as key
			       		break
			       	end
			    end   
	]]		   
			   --TODO reorganise the following with better logic
			   --[[ template to display flag icon and team link (e.g. fb, fbw, bk, ih)
			       e.g.  "FIFA World Rankings" = 'fb', "FIFA Women's World Rankings" 'fbw',
			             "FIBA World Rankings" = 'bk', "IIHF World Ranking"  = 'ih'  
			       tries with country code, then if error, tried with country name]]
			   local countryTemplate = data.templates['flagged_team_link'] 
			   local flagVariant = ""
               if data.templates.flagvar and data.templates.flagvar[code] then
            		flagVariant = data.templates.flagvar[code] 
			   end
			   local countryIconString = frame:expandTemplate{ title = countryTemplate, args = {code, flagVariant} }    -- country

	 		   local _,test =  string.gsub( countryIconString, "Template:Country data", "") -- page does not exist
	 		   if test == 1 then -- if error try country name
	 		   	  countryIconString = frame:expandTemplate{ title = countryTemplate, args = {v[1]} }
	 		   	  --countryIconString = "testing"
	 		   else 
	 		   	 -- countryIconString = "exists"
	 		   end
	 		   	  
	 		   rowString = rowString .. '\n|style="text-align:right"|' .. countryIconString
			   
			   local points = ""
			   if v[4] then points = v[4] end
			   rowString = rowString ..  '||' .. num_con("fa", points)       -- country for now, later points
			   outputString = outputString .. rowString
			end
		end
	end
	
    -- add footer rows
    count = 0
    local footer = {}
    while count < 5 do
    	count = count + 1
	    if templateArgs['footer'..count] then
	    	footer[count] = templateArgs['footer'..count] 
	    	footer[count] = p.replaceKeywords(footer[count])
	    	outputString = outputString ..	'\n|-\n| colspan="'.. tableWidth .. '" |' .. footer[count]
	    end
    end


    outputString = outputString .. "\n|}"

    return outputString
	
end

function p.replaceKeywords(keyword)
	keyword =  string.gsub( keyword, "INSERT_UPDATE_DATE", date2fa(getDate()))
    keyword =  string.gsub( keyword, "INSERT_LAST_DATE", date2fa(getDate("LAST")))
	if string.find(keyword, "INSERT_REFERENCE") then
		keyword =  string.gsub( keyword, "INSERT_REFERENCE", addReference(mw.getCurrentFrame()))
	end
    return keyword
end

--[[ create a table of rankings
       parameters:  |ranking=        -- ranking to display (e.g. FIFA World Rankings)
                    |first= |last=   -- first and last ranking to display (defaults 1-10)
]]
function p.list(frame)

    getArgs(frame) -- returns args table having checked for content
    loadData(frame)	
    local ranking = frame.args[1]
    
    local first = tonumber(frame.args['2']) or 1
    local last = tonumber(frame.args['3']) or 10
    
    return table(frame, ranking, first, last)
end

--[[ create a particla table of rankings above and below a country 
       parameters:  |ranking |country |span     
                                              -- ranking - the ranking to display (e.g. FIFA World Rankings)
                                              -- country - country table is centred around
                                              -- span=   - rows to display above and below country
]]
function p.list2(frame)

    getArgs(frame) -- returns args table having checked for content
    loadData(frame)	
    local ranking = frame.args[1]
    local first, last = 1,10
    local country = frame.args[2]           -- name or code of country to center table around
    local span = frame.args[3] or 2         -- number of rows to display above and below country (default:2)
    
    if string.len(country) == 3 then        -- if three letter country code
    		for _,u in pairs(data.alias) do
		    	if u[1]==country then 
		       		country = u[2]          -- if country code then use country name 
		       		break
		       	end
		    end   
    end
    for k,v in pairs(data.rankings) do      -- find position of country in rankings
       if v[1] == country then
       	  first = v[2]-span
       	  last = v[2]+span
       end
    end
    
    return table(frame, ranking, first, last)
end

return p