Module:DisambigPage

local m_team = require('Module:Team') local m_region = require('Module:Region') local m_role = require('Module:Role') local m_country = require('Module:Country') local util_table = require('Module:TableUtil') local util_cargo = require('Module:CargoUtil') local util_args = require('Module:ArgsUtil')

local lang = mw.getLanguage('en')

local ORDER = { 'players', 'teams', 'other' } local INTROS = { players = "The ID %s can %s refer to one of the following players:", teams = "%s can %s refer to one of the following teams:", other = "%s can %s refer to one of the following:" }

local h = {}

-- players function h.playerSection(args) local tbl = {} local i = 1 local thisplayer = args['player' .. i]	while thisplayer do		local playertitle = mw.title.makeTitle('', thisplayer) if playertitle.exists then tbl[#tbl+1] = h.playerCargo(thisplayer) else tbl[#tbl+1] = h.playerArgs(args, i)		end i = i + 1 thisplayer = args['player' .. i]	end h.storePlayerCargo(tbl) return util_table.mapInPlace(tbl, h.makePlayerLine) end

function h.playerCargo(player) local row = util_cargo.getOneRow(h.makePlayerQuery(player)) row.IsFormer = not row.Team row.Team = row.Team or row.TeamLast row.CurrentName = row._pageName ~= lang:ucfirst(player) and row._pageName row.Name = player return row end

function h.makePlayerQuery(player) local fields = { "Players.Residency=Residency", "Players.Team=Team", "Players.TeamLast=TeamLast", "Players.RoleLast=RoleLast", "Players.Country=Country", "Players.IsSubstitute=IsSubstitute", "Players.IsLowercase=IsLowercase", "Players._pageName=_pageName" }	return { tables = 'Players, Players__IDList', join = 'Players._ID=Players__IDList._rowID', fields = fields, groupBy = 'Players._pageName', where = ('Players__IDList._value = "%s"'):format(lang:ucfirst(player)), types = { IsSubstitute = 'boolean', IsLowercase = 'boolean' }	} end

function h.playerArgs(args, i) local index = 'player' .. i	local row = { Name = args[index], Residency = args[index .. 'res'], Country = args[index .. 'country'], Team = args[index .. 'team'], RoleLast = args[index .. 'role'], IsSubstitute = util_cargo.strToBool(args[index .. 'isSubstitute']), IsFormer = util_cargo.strToBool(args[index .. 'isFormer']), CurrentName = args[index .. 'currentName'], _pageName = args[index], }	return row end

function h.makePlayerLine(row) local tbl = { m_region.onlyimage(row.Residency), ('%s,'):format(row.Name or ''), row.Country and m_country.localization(row.Country), 'player,', row.IsFormer and 'former', m_role.sentence(row.RoleLast, { sub = row.IsSubstitute }), m_team.rightmedium(row.Team), row.CurrentName and ("(now known as %s)"):format(row.CurrentName), }	util_table.removeFalseEntries(tbl) return table.concat(tbl, ' ') end

function h.storePlayerCargo(tbl) for _, row in ipairs(tbl) do		local data = { _table = 'PlayerDisambig', Name = row.Name, Region = m_region.long(row.Residency), Country = row.Country and m_country.default(row.Country), Team = m_team.teamlinkname(row.team), IsFormer = row.IsFormer and 'Yes' or 'No', m_role.rolename(row.RoleLast), CurrentName = row.CurrentName, IsSubstitute = row.IsSubstitute and 'Yes' or 'No' }		util_cargo.store(data) end return end

-- teams function h.teamSection(args) local tbl = {} local i = 1 local thisteam = args['team' .. i]	while thisteam do		local teamtitle = mw.title.makeTitle('', thisteam) if teamtitle.exists then tbl[#tbl+1] = h.teamCargo(thisteam, args['team' .. i .. 'freetext']) else tbl[#tbl+1] = h.teamArgs(args, i)		end i = i + 1 thisteam = args['team' .. i]	end return util_table.mapInPlace(tbl, h.makeTeamLine) end

function h.teamCargo(team, freetext) local row = util_cargo.getOneRow(h.makeTeamQuery(team)) row.freetext = freetext row.Location = row.TeamLocation or row.Location return row end

function h.makeTeamQuery(team) local fields = { 'Region', '_pageName', 'Location', 'TeamLocation', 'IsDisbanded' }	return { tables = 'Teams', fields = fields, where = ('_pageName="%s"'):format(team), groupBy = '_pageName', types = { IsDisbanded = 'boolean' } } end

function h.teamArgs(args, i) local index = 'team' .. i	local row = { Region = args[index .. 'region'], Location = args[index .. 'location'], IsDisbanded = util_cargo.strToBool(args[index .. 'isDisbanded']), _pageName = args[index], freetext = args[index .. 'freetext'], }	return row end

function h.makeTeamLine(row) local tbl = { m_region.onlyimage(row.Region), ('%s,'):format(row._pageName or ''), row.IsDisbanded and 'disbanded', row.Location and m_country.localization(row.Location), 'team' .. (row.freetext and '.' or ''), row.freetext }	util_table.removeFalseEntries(tbl) return table.concat(tbl, ' ') end

-- final function h.printSentence(sections, title) local tbl = { sections.intro }	notfirst = false for _, v in ipairs(ORDER) do		if sections[v] and next(sections[v]) then tbl[#tbl+1] = INTROS[v]:format(title, notfirst and 'also' or '') notfirst = true util_table.mapInPlace(sections[v], h.addStars) util_table.mergeArrays(tbl, sections[v]) end end tbl[#tbl+1] = sections.outro return tbl end

function h.addStars(line) return ('* %s.'):format(line) end

local p = {} function p.main(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end local title = args.title or mw.title.getCurrentTitle.text local sections = { intro = ('%s does not have a page with this exact title.'):format(mw.site.siteName), outro = frame:expandTemplate{ title = 'Disambig' }, players = h.playerSection(args), teams = h.teamSection(args), other = util_args.numberedArgsToTable(args, 'other') }	local sentence = h.printSentence(sections, title) return table.concat(sentence, '\n') end return p