Module:DisambigPage

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_esports = require("Module:EsportsUtil") local util_map = require('Module:MapUtil') local util_sentence = require("Module:SentenceUtil") local util_table = require('Module:TableUtil') local util_text = require("Module:TextUtil") local i18n = require('Module:i18nUtil')

local m_team = require('Module:Team') local m_region = require('Module:Region') local Role = require('Module:Role') local m_country = require('Module:Country') local util_vars = require('Module:VarsUtil')

local lang = mw.getLanguage('en')

local ORDER = { 'players', 'teams', 'other' }

local TITLE local CARGO_INDEX = 0

local h = {}

local p = {} function p.main(frame) local args = util_args.merge i18n.init('DisambigPage') h.setTitle(args) local sections = { intro = i18n.print('DisambigPage', mw.site.siteName), players = h.getAndStorePlayerData(args), teams = h.getAndStoreTeamData(args), other = util_args.numberedArgsToTable(args, 'other'), }	return h.makeOutput(sections) end

function h.setTitle(args) -- strip out (Disambiguation) text if needed if args.title then TITLE = args.title return end TITLE = mw.title.getCurrentTitle.text:gsub(' %(Disambiguation%)', '') end

-- players function h.getAndStorePlayerData(args) local tbl = util_args.numberedArgsToTable(args, 'player') if not tbl then return nil end util_map.arrayInPlaceWithIndex(tbl, h.getPlayerData, args) h.storeCargo(tbl, 'Player') return util_map.inPlace(tbl, h.makePlayerLine) end

function h.getPlayerData(player, i, args) if mw.title.makeTitle('', player).exists then return h.getPlayerDataFromCargo(player) end return h.getPlayerDataFromArgs(args, i) end

function h.getPlayerDataFromCargo(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) return { tables = 'Players, PlayerRedirects=PR', join = 'Players._pageName=PR._pageName', fields = { "Players.Residency=Residency", "Players.Team", "Players.TeamLast", "Players.RoleLast", "Players.Country", "Players.IsSubstitute", "Players.IsLowercase", "Players._pageName", },		groupBy = 'Players._pageName', where = ('PR.AllName = "%s"'):format(player), types = { IsSubstitute = 'boolean', IsLowercase = 'boolean' }	} end

function h.getPlayerDataFromArgs(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 replacements = { RESIDENCY = m_region.onlyimage(row.Residency), LINK = util_text.intLinkOrText(row.Name), NATIONALITY = row.Country and m_country.localization(row.Country) or '', FORMER = row.IsFormer and i18n.default('former') or '', ROLE = Role(row.RoleLast, { sub = row.IsSubstitute }):name{len='sentence'}, TEAM = m_team.rightmediumlinked(row.Team), NOW_KNOWN_AS = row.CurrentName and i18n.default('nowKnownAs', row.CurrentName) or '', }	return util_sentence.makeReplacements(i18n.default('sentencePlayer'), replacements) end

function h.pageExistsField(name) return mw.title.new(name, '').exists end

function h.storeCargo(tbl, tableType) util_map.rowsInPlace(tbl, h.storeOneCargoRow, tableType) end

function h.storeOneCargoRow(row, tableType) CARGO_INDEX = CARGO_INDEX + 1 util_cargo.store(h.getGeneralCargoData(row, tableType)) end

function h.getGeneralCargoData(row, disambigType) local data = { _table = 'Disambiguations', FinalLocation = row._pageName, Term = TITLE, DisambigType = disambigType, N_LineInPage = CARGO_INDEX, DisambigID = h.getDisambigId, }	return data end

function h.getDisambigId return ('%s_%s'):format(mw.title.getCurrentTitle.text, CARGO_INDEX) end

-- teams function h.getAndStoreTeamData(args) local tbl = util_args.numberedArgsToTable(args, 'team') if not tbl then return nil end util_map.arrayInPlaceWithIndex(tbl, h.getTeamData, args) h.storeCargo(tbl, 'Team') return util_map.inPlace(tbl, h.makeTeamLine) end

function h.getTeamData(team, i, args) if mw.title.makeTitle('', team).exists then return h.getTeamDataFromCargo(team, args['team' .. i .. 'freetext']) end return h.getTeamDataFromArgs(args, i) end

function h.getTeamDataFromCargo(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', 'IsLowercase', }	return { tables = 'Teams', fields = fields, where = ('_pageName="%s"'):format(team), groupBy = '_pageName', types = { IsDisbanded = 'boolean', IsLowercase = 'boolean', }	} end

function h.getTeamDataFromArgs(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 replacements = { REGION = m_region.onlyimage(row.Region), LINK = util_text.intLinkOrText(h.getCasedLink(row._pageName, row.IsLowercase)), DISBANDED = row.IsDisbanded and i18n.default('disbanded') or '', COUNTRY = row.Location and m_country.localization(row.Location) or '', END = h.getTeamLineEnd(row), }	return util_sentence.makeReplacements(i18n.default('sentenceTeam'), replacements) end

function h.getCasedLink(link, isLowercase) if not isLowercase then return link end return util_text.lcfirst(link) end

function h.getTeamLineEnd(row) if not row.IsDisbanded and not row.Location then return row.freetext or '' end return i18n.default('teamEndDefault') end

-- output function h.makeOutput(sections) local output = mw.html.create output:wikitext(sections.intro) h.printLists(output, sections) return output end

function h.printLists(output, sections) notfirst = false for _, v in ipairs(ORDER) do		h.printOneList(output, sections[v], v, notfirst) notfirst = true end end

function h.printOneList(output, sectionData, sectionName, notfirst) if not sectionData or not next(sectionData) then return end output:wikitext(h.getIntro(sectionName, notfirst)) local ul = output:tag('ul') util_map.inPlace(sectionData, h.printOneListItems, ul) end

function h.getIntro(sectionName, notfirst) return i18n.print(		'intro_' .. sectionName,		TITLE,		i18n.print(notfirst and 'also_refer' or 'refer')	) end

function h.printOneListItems(line, ul) ul:tag('li'):wikitext(line) end

return p