Module:PHL sports team
| Semi-protected | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is semi-protected from editing. | 
Usage edit source
{{#invoke:PHL sports team|function_name}}
Data modules edit source
- Module:PHL sports team/collegiate – high school and collegiate leagues, e.g. UAAP and NCAA
- Module:PHL sports team/PBA – PBA and PBA D–League
Test cases edit source
| Code or Alias/es | Usage | Output | 
|---|---|---|
| ALA | {{PBA team|ALA}} | Template:PBA team | 
| {{PBA team|ALA|short}} | Template:PBA team | |
| {{PBA team|ALA|name=Custom Team Name}} | Template:PBA team | |
| AMA | {{PBA team|AMA}} | Template:PBA team | 
| {{PBA team|AMA|short}} | Template:PBA team | |
| {{PBA team|AMA|name=Custom Team Name}} | Template:PBA team | |
| BWB | {{PBA team|BWB}} | Template:PBA team | 
| {{PBA team|BWB|short}} | Template:PBA team | |
| {{PBA team|BWB|name=Custom Team Name}}or {{PBA team|BWB|yr=2014}} (2014–2020) | Template:PBA team or Template:PBA team (2014–2020) | |
| CEU | {{PBA team|CEU}} | Template:PBA team | 
| {{PBA team|CEU|short}} | Template:PBA team | |
| {{PBA team|CEU|name=Custom Team Name}}or {{PBA team|CEU|yr=2020}} (2020–2020) | Template:PBA team or Template:PBA team (2020–2020) | |
| COL | {{PBA team|COL}} | Template:PBA team | 
| {{PBA team|COL|short}} | Template:PBA team | |
| {{PBA team|COL|name=Custom Team Name}}or {{PBA team|COL|yr=2018}} (2018–2019) | Template:PBA team or Template:PBA team (2018–2019) | |
| CON | {{PBA team|CON}} | Template:PBA team | 
| {{PBA team|CON|short}} | Template:PBA team | |
| {{PBA team|CON|name=Custom Team Name}} | Template:PBA team | |
| DIL | {{PBA team|DIL}} | Template:PBA team | 
| {{PBA team|DIL|short}} | Template:PBA team | |
| {{PBA team|DIL|name=Custom Team Name}} | Template:PBA team | |
| DLS | {{PBA team|DLS}} | Template:PBA team | 
| {{PBA team|DLS|short}} | Template:PBA team | |
| {{PBA team|DLS|name=Custom Team Name}}or {{PBA team|DLS|yr=2020}} (2020–2020) | Template:PBA team or Template:PBA team (2020–2020) | |
| END | {{PBA team|END}} | Template:PBA team | 
| {{PBA team|END|short}} | Template:PBA team | |
| {{PBA team|END|name=Custom Team Name}}or {{PBA team|END|yr=2019}} (2019–2020) | Template:PBA team or Template:PBA team (2019–2020) | |
| FEU | {{PBA team|FEU}} | Template:PBA team | 
| {{PBA team|FEU|short}} | Template:PBA team | |
| {{PBA team|FEU|name=Custom Team Name}}or {{PBA team|FEU|yr=2020}} (2020–2020) | Template:PBA team or Template:PBA team (2020–2020) | |
| GSM | {{PBA team|GSM}} | Template:PBA team | 
| {{PBA team|GSM|short}} | Template:PBA team | |
| {{PBA team|GSM|name=Custom Team Name}} | Template:PBA team | |
| LET | {{PBA team|LET}} | Template:PBA team | 
| {{PBA team|LET|short}} | Template:PBA team | |
| {{PBA team|LET|name=Custom Team Name}}or {{PBA team|LET|yr=2020}} (2020–2020) | Template:PBA team or Template:PBA team (2020–2020) | |
| MAG | {{PBA team|MAG}} | Template:PBA team | 
| {{PBA team|MAG|short}} | Template:PBA team | |
| {{PBA team|MAG|name=Custom Team Name}} | Template:PBA team | |
| MAP | {{PBA team|MAP}} | Template:PBA team | 
| {{PBA team|MAP|short}} | Template:PBA team | |
| {{PBA team|MAP|name=Custom Team Name}} | Template:PBA team | |
| MAU | {{PBA team|MAU}} | Template:PBA team | 
| {{PBA team|MAU|short}} | Template:PBA team | |
| {{PBA team|MAU|name=Custom Team Name}}or {{PBA team|MAU|yr=2020}} (2020–2020) | Template:PBA team or Template:PBA team (2020–2020) | |
| MER | {{PBA team|MER}} | Template:PBA team | 
| {{PBA team|MER|short}} | Template:PBA team | |
| {{PBA team|MER|name=Custom Team Name}} | Template:PBA team | |
| NLX | {{PBA team|NLX}} | Template:PBA team | 
| {{PBA team|NLX|short}} | Template:PBA team | |
| {{PBA team|NLX|name=Custom Team Name}} | Template:PBA team | |
| NP | {{PBA team|NP}} | Template:PBA team | 
| {{PBA team|NP|short}} | Template:PBA team | |
| {{PBA team|NP|name=Custom Team Name}} | Template:PBA team | |
| PHX | {{PBA team|PHX}} | Template:PBA team | 
| {{PBA team|PHX|short}} | Template:PBA team | |
| {{PBA team|PHX|name=Custom Team Name}}or {{PBA team|PHX|yr=2016}} (2016–2018)or {{PBA team|PHX|yr=2019}} (2019–2019) | Template:PBA team or Template:PBA team (2016–2018) or Template:PBA team (2019–2019) | |
| ROS | {{PBA team|ROS}} | Template:PBA team | 
| {{PBA team|ROS|short}} | Template:PBA team | |
| {{PBA team|ROS|name=Custom Team Name}} | Template:PBA team | |
| SMB | {{PBA team|SMB}} | Template:PBA team | 
| {{PBA team|SMB|short}} | Template:PBA team | |
| {{PBA team|SMB|name=Custom Team Name}} | Template:PBA team | |
| SSC | {{PBA team|SSC}} | Template:PBA team | 
| {{PBA team|SSC|short}} | Template:PBA team | |
| {{PBA team|SSC|name=Custom Team Name}}or {{PBA team|SSC|yr=2020}} (2020–2020) | Template:PBA team or Template:PBA team (2020–2020) | |
| TIP | {{PBA team|TIP}} | Template:PBA team | 
| {{PBA team|TIP|short}} | Template:PBA team | |
| {{PBA team|TIP|name=Custom Team Name}} | Template:PBA team | |
| TNT | {{PBA team|TNT}} | Template:PBA team | 
| {{PBA team|TNT|short}} | Template:PBA team | |
| {{PBA team|TNT|name=Custom Team Name}}or {{PBA team|TNT|yr=2016}} (2016–2019) | Template:PBA team or Template:PBA team (2016–2019) | |
| UST | {{PBA team|UST}} | Template:PBA team | 
| {{PBA team|UST|short}} | Template:PBA team | |
| {{PBA team|UST|name=Custom Team Name}}or {{PBA team|UST|yr=2019}} (2019–2019)or {{PBA team|UST|yr=2020}} (2020–2020) | Template:PBA team or Template:PBA team (2019–2019) or Template:PBA team (2020–2020) | |
| {{{team1}}} | {{PBA team|{{{team1}}}}} | Template:PBA team | 
| {{PBA team|{{{team1}}}|short}} | Template:PBA team | |
| {{PBA team|{{{team1}}}|name=Custom Team Name}} | Template:PBA team | |
| XYZ | {{PBA team|XYZ}} | Template:PBA team | 
| {{PBA team|XYZ|short}} | Template:PBA team | |
| {{PBA team|XYZ|name=Custom Team Name}} | Template:PBA team | 
require('strict')
local p = { isalias = false }
local yesno = require('Module:Yesno')
local constants = {	CODE_LEN = 3, SPACE = ' ', SHORT = 'short', INST = 'inst' }
local function makeInvokeFunc(funcName, league)
	return function (frame)
		local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
		args['league'] = args['league'] or league or 'collegiate'
		return p[funcName](args)
	end
end
local function stripwhitespace(text) return text:match("^%s*(.-)%s*$") end
local function load_data(args)
	local data_module = 'Module:PHL sports team/' .. args['league']
	return mw.loadData(data_module)
end
local function get_key_by_code(code, data)
	if (data[code] ~= nil and type(data[code]) == 'string') then
		p.isalias = true
		return data[code]
	elseif (string.len(code) == constants.CODE_LEN) then
		for k,v in pairs(data) do
			if v[1]==code then return k end
		end
	end
	return code
end
local function get_icon(team, size, alt_team)
	local icon = '[[File:%s colors.svg|%s|border|%s school colors|link=]]'
	return string.format(icon, team, size or '11px', alt_team or team)
end
local function get_athlete_link(link, athl_name)
	if mw.title.new(link or athl_name, '').exists == true then return string.format('[[%s|%s]]', link or athl_name, athl_name) else return athl_name end
end
local function show_empty_param(param)
	return mw.html.create('span'):css('color', 'red'):cssText('style'):wikitext(string.format('Value for parameter \'%s\' not provided.', param))
end
local function get_link_by_evt(args, div, divLt, text, team)
	local evt = { bk = 'basketball', vb = 'volleyball', ft = 'football' }
	local mRdr = require('Module:Redirect')
	evt = evt[args[2]] or evt[args[3]] or evt[args['evt']]
	if evt == nil then
		if type(div) == 'number' and div < 3 then
			 return mRdr.getTarget(team)
		else return mRdr.getTarget(text) end
	end
	if (type(div) == 'string') then div = 3 end
	local evt_link = string.format('%s %s %s', text, divLt[div-2], evt)
	if (args['yr'] ~= nil) then return args['yr'] .. ' ' .. evt_link .. ' team' end
	return mRdr.getTarget(evt_link) or evt_link
end
local function get_name_by_year(team, year)
	if not year then return team[1] end
	for k, v in pairs(team) do
		if mw.ustring.find(k, "%d%d%d%dthru%d%d%d%d$") then
			local start_year, end_year = mw.ustring.match(k, "(%d%d%d%d)thru(%d%d%d%d)$")
			if (tonumber(start_year) <= tonumber(year)) and (tonumber(year) <= tonumber(end_year)) then
				return v
			end
		end
	end
	return team[1]
end
local function add_link(args, team, name, div, divLt, text)
	local evt_link = get_link_by_evt(args, div, divLt, team[div] or team[3], team[3])
	if (type(div) == 'number' and div >= 3 and div <= 6) then
		if ((args['inst'] or name) ~= nil or args[3] == constants.SHORT)
				then return string.format('[[%s|%s]]', evt_link or team[div], not p.isalias and team[args['inst']] or text)
			elseif (evt_link ~= nil) then return string.format('[[%s|%s]]', evt_link, text)
			else return string.format('[[%s]]', text) end
	end
	return string.format('[[%s|%s]]', evt_link or team[3], text)
end
local function add_link_generic(args, team, text)
	local mRdr = require('Module:Redirect')
	local tln = team.link or get_name_by_year(team, args['season'] or args['team'])
	local dab  = team.dab and (tln .. ' (' .. team.dab .. ')') or nil
	local fln = dab or tln
	
	if args['name']     then fln = team[args['name']] or dab or tln end
	if args['season']   then fln = args['season'] .. ' ' .. tln .. ' season'
	elseif args['team'] then fln = args['team'] .. ' ' .. tln .. ' team'
	else fln = mRdr.getTarget(fln) end
	return string.format('[[%s|%s]]', fln or dab or tln, text)
end
function p._main(args)
	local data = load_data(args)
	local in_team = stripwhitespace(args[1] or '')
	if (in_team:match('^{{{.*}}}$') ~= nil) then return show_empty_param(in_team) elseif (in_team == '' or nil) then return '—' end
	
	local in_div = stripwhitespace(args[2] or '')
	local in_name = args['name']
	local key = get_key_by_code(in_team, data)
	local team = data[key]
	if (team == nil) then return error(string.format('Invalid team: %s', in_team)) end
	
	local out
	local divLt = { "men's", "women's", "boys'", "girls'" }
	
	if (in_div ~= constants.SHORT) then
		local div = { inst = 2, men = 3, women = 4, junior = 5, boys = 5, girls = 6 }
		in_div = div[in_div] or tonumber(in_div:match('[2-6]') or '3')
		if (args[3] == constants.INST or args['inst'] ~= nil or in_div == 2) then out = p.isalias and team[in_team] or team[div['inst']]
			elseif (args[3] == constants.SHORT) then out = team[7] or key or in_team
			else out = in_name or team[in_div] end
		if (out == nil) then return error('No ' .. divLt[in_div-2] .. ' team') end
	else
		out = team[7] or key or in_team
	end
	
	out = out:gsub("-", "–")
	
	if yesno(args['add_link'] or 'y') then
		out = add_link(args, team, in_name, in_div, divLt, out)
	end
	if yesno(args['icon'] or 'y') then
		local icon = get_icon(key or in_team, args['iconsize'])
		out = yesno(args['rt'] or 'n') and out .. constants.SPACE .. icon or icon .. constants.SPACE .. out
	end
	
	return out
end
function p._rt(args)
	args['rt'] = 'y'
	return p._main(args)
end
function p._name(args)
	args['icon'] = 'n'
	return p._main(args)
end
function p._color(args)
	local data = load_data(args)
	local in_team = stripwhitespace(args[1] or '')
	local no_img = string.format('[[File:No image.svg|%s|link=]]', args['size'] or '11px')
	local note = ''
	if in_team and in_team:match('[%*]$') then
		note = mw.ustring.gsub(in_team, '^(.-)([%*]*)$', '%2')
		in_team = mw.ustring.gsub(in_team, '^(.-)([%*]*)$', '%1')
	end
	if (in_team:match('^{{{.*}}}$') ~= nil) then
		return show_empty_param(in_team) .. note
	elseif (in_team == '' or nil) then return no_img .. note end
		
	local key = get_key_by_code(in_team, data)
	if ((data[in_team] or data[key]) == nil) then
		return no_img .. note
	end
	return get_icon(key or in_team, args['size'], args[2]) .. note
end
function p._generic(args)
	local data = load_data(args)
	local code, name, out = stripwhitespace(args[1] or ''), args['name']
	local team = data[code]
	
	if (code:match('^{{{.*}}}$') ~= nil) then return show_empty_param(code) elseif (code == '' or nil) then return '—' end
	if type(team) == 'string' then
		local alias = mw.text.split(team, '%s*|%s*')
		team = data[alias[1]]
		name = name or alias[2]
	end
	
	if not team then return error(string.format('Invalid team: %s', code)) end
	if args[2] == constants.SHORT then out = team[2] or code
		elseif name then out = team[name] or name
		else out = get_name_by_year(team, args['season'] or args['team'] or args['yr'])
	end
	return add_link_generic(args, team, out)
end
function p._athlete(args)
	local athl_1 = args['athl']
	local athl_2 = args['athl2']
	
	if athl_1 == nil then return error('Invalid athlete: no value') end
	local link = get_athlete_link(args['link'], athl_1)
	
	if (athl_2 ~= nil) then link = link .. ' and ' .. get_athlete_link(args['link2'], athl_2) end
	
	local showicon = yesno(args['icon'])
	local sport = args['sp']
	
	if (showicon) then args[3] = constants.SHORT end
	args['add_link'] = 'n'
	local lbl = p._name(args)
	if (sport ~= nil) then
		lbl = string.format('%s <span style="font-size:90%%;">(%s, %s)</span>', link, lbl, string.lower(sport))
	else lbl = string.format('%s <span style="font-size:90%%;">(%s)</span>', link, lbl)
	end
	
	if (showicon) then
		args[2] = args[1]
		return p._color(args) .. constants.SPACE .. lbl
	else return lbl
	end
end
function p._athlete_bc(args)
	if (args['athl'] == nil) then
	    -- reassign arguments for backward compatibility --
	    args['athl'] = args[2]
		args[2] = args[3] or ''
		args[3] = args[4] or ''
	end
	return p._athlete(args)
end
p.main = makeInvokeFunc('_main')
p.rt = makeInvokeFunc('_rt')
p.name = makeInvokeFunc('_name')
p.color = makeInvokeFunc('_color')
p.pba = makeInvokeFunc('_generic', 'PBA')
p.athlete = makeInvokeFunc('_athlete_bc')
return p
