Module:StoreRedirects

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

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

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_page = require("Module:PageUtil")
local util_vars = require("Module:VarsUtil")

local LCS = require('Module:LuaClassSystem')

-- there's some annoying issues responsible for this code (if it weren't for this then just use _pageData gg & this table doesn't exist period)
-- 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 re-store so it's unable to tell the difference between actually-different rows
-- this code needs to be invoked as the VERY FIRST THING on any page that has it, so it's available immediately to the rest of the page.

-- this code used to be part of Module:InfoboxUtil until Nov 2019

local h = {}
local p = {}
local Redirects = LCS.class()
local PlayerRedirects = Redirects:extends()

function p.main(frame)
	local args = util_args.merge()
	if args.type == 'Player' then
		return PlayerRedirects:init(args)
	end
	return Redirects:init(args)
end

function Redirects:init(args)
	local allRedirects = util_page.whatRedirectsHere()
	util_cargo.setStoreNamespace('')
	util_vars.setVar('whatredirectshere', table.concat(allRedirects,';'))
	table.insert(allRedirects, 1, mw.title.getCurrentTitle().text)
	local redirects = self:getUniqueCaseRedirects(allRedirects)
	for _, redirect in ipairs(redirects) do
		self:storeOneRedirect(redirect, args.type)
	end
end

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

function Redirects:storeOneRedirect(name, tableType)
	local toStore = self:getStoreCargo(name, tableType)
	util_cargo.store(toStore)
end

function Redirects:getStoreCargo(name, tableType)
	return {
		_table = ('%sRedirects'):format(tableType),
		AllName = name
	}
end

function PlayerRedirects:getStoreCargo(name, tableType)
	local toStore = self:super('getStoreCargo', name, 'Player')
	toStore.OverviewPage = mw.title.getCurrentTitle().text
	toStore.ID = util_esports.playerDisplay(name)
	return toStore
end

return p