From Halo Esports Wiki
Jump to: navigation, search

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

local lang = mw.getLanguage('en')
local util_time = require('Module:TimeUtil')
local Stat = require('Module:StatClass')

local p = {}
local h = {}
function p.makeBday(y, m, d, death)
	-- expect death to be a string yyyy-mm-dd
	if not ((m and d) or death) then
		return {}
	if not m then m = '1' end
	if not d then d = '1' end
	local m = tostring(util_time.monthNameToNumber[m])
	if #m == 1 then
		m = '0' .. m
	if #d == 1 then
		d = '0' .. d
	local full = y and m and d
	if not full then
		y = 2014 -- we won't print this, just a random year so that the formatting works
	local str = ('%s-%s-%s'):format(y,m,d)
	local tbl = {
		full = full,
		age = full and util_time.age(str, death),
		display = h.pickAndGetDisplay(full, str, death),
		store = death or (full and str) or ''
	tbl.displayandage = tbl.display .. (full and (' (age %s)'):format(tbl.age) or '')
	return tbl

function h.pickAndGetDisplay(full, str, death)
	if death then return h.getDisplay(death, true) end
	return h.getDisplay(str, full)

function h.getDisplay(time, full)
	return full and lang:formatDate('F j, Y', time) or lang:formatDate('F j', time)

function p.getFile(arg, default)
	local title = arg and mw.title.makeTitle('Media',arg or '')
	return title and title.exists and arg or default

function p.statDisplays(tbl)
	local ret = {}
	for _, v in ipairs(tbl) do
		ret[v] = Stat(v):flair('medium')
	return ret


function p.getAndMergeRedirects(cargo, processed, tableType)
	local redirectsPruned = h.getUniqueCaseRedirects(processed.redirects, processed.pagename)
	cargo[#cargo+1] = {
		_table = ('%sRedirects'):format(tableType),
		AllName = processed.pagename,
	for k, v in ipairs(redirectsPruned) do
		cargo[#cargo+1] = h.getRedirectCargo(v, k, processed.pagename, tableType)

function h.getRedirects(pagename, tableType)

function h.getUniqueCaseRedirects(redirects, pagename)
	local hash = { [mw.ustring.lower(pagename)] = true }
	local ret = {}
	for _, v in ipairs(redirects) do
		local lc = mw.ustring.lower(v)
		if not hash[lc] then
			ret[#ret+1] = lc
		hash[lc] = true
	return ret

function h.getRedirectCargo(name, i, title, tableType)
	-- there's a cargo bug responsible for the commented code here
	-- cargo ignores special characters unless you use BINARY so when i deleted the BINARY conditions, the special characters became a liability
	-- lua can't check special-character-agnostic string equality natively
	-- so i'll rely on cargo's storing bug where it only stores an equivalent-up-to-case-and-special-character-substitution set of rows one time
	-- so here we guarantee it's unable to tell the difference between actually-different rows
	-- by removing OtherName field and UniqueLine field
	-- probably we don't need those fields anyway, but we absolutely cannot have them until this cargo issue is fixed
	return {
		_table = tableType .. 'Redirects',
		AllName = name,
		-- OtherName = name,
		-- UniqueLine = ('%s_%s'):format(title, i + 1)

return p