Module:IntroSentence/DisambigList

From Call of Duty Esports Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:IntroSentence/DisambigList/doc

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_sentence = require("Module:SentenceUtil")
local util_table = require('Module:TableUtil')
local util_text = require("Module:TextUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require('Module:i18nUtil')

local h = {}

local p = {}

function p.main(id, sentenceType)
	local disambigCounts = h.getDisambiguationCounts(id)
	if not next(disambigCounts) then return nil end
	disambigCounts.Player = disambigCounts.Player or 0
	disambigCounts.Team = disambigCounts.Team or 0
	disambigCounts[sentenceType] = disambigCounts[sentenceType] - 1
	return h.makeDisambigSentence(disambigCounts, id)
end

function h.getDisambiguationCounts(id)
	return util_cargo.getConstDict(h.getDisambigQuery(id), 'DisambigType', 'Count')
end

function h.getDisambigQuery(id)
	local query = {
		tables = 'Disambiguations',
		where = ('Term="%s"'):format(id),
		fields = {
			'COUNT(*)=Count[number]',
			'DisambigType',
		},
		groupBy = 'DisambigType',
	}
	return query
end

function h.makeDisambigSentence(disambigCounts, id)
	local output = mw.html.create('div')
		:addClass('sbs-disambig')
		:wikitext(h.makeDisambigSentenceText(disambigCounts, id))
	return tostring(output)
end

function h.makeDisambigSentenceText(counts, id)
	local replacements = {
		DISAMBIG_PAGE = util_text.intLink(
			h.getDisambigLocation(id), i18n.default('disambig_page')
		),
		PLAYERS_PART = counts.Player > 0 and i18n.default('disambig_players') or '',
		TEAMS_PART = counts.Team > 0 and i18n.default('disambig_teams') or '',
		AND = h.getAndReplacement(counts),
		PLAYERS = util_sentence.getConjugation('players', counts.Player),
		TEAMS = util_sentence.getConjugation('teams', counts.Team),
		SHARE = util_sentence.getConjugation('share', counts.Team + counts.Player),
		N_PLAYERS = h.getNumberText(counts.Player, true),
		N_TEAMS = h.getNumberText(counts.Team, counts.Player <= 0),
		ID = id,
	}
	return util_sentence.makeReplacements(i18n.default('disambigSentence'), replacements)
end

function h.getDisambigLocation(id)
	return util_cargo.getOneResult({
		tables = 'Disambiguations',
		fields = '_pageName',
		where = ('Term="%s"'):format(id)
	}, '_pageName')
end

function h.getAndReplacement(counts)
	if counts.Player <= 0 or counts.Team <= 0 then return '' end
	return i18n.default('and')
end

function h.getNumberText(n, capitalize)
	local text = mw.getCurrentFrame():callParserFunction{
		name = '#numbertext',
		args = { n }
	}
	if capitalize then
		return util_text.ucfirst(text)
	end
	return text
end

return p