Module:Site categorizer

From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search

local p = {}
local core = require('Module:Infobox categorizer')
local config = mw.loadData('Module:Site categorizer/config')

local where = 'Module:Site categorizer/config'
local dims = core.need(config, 'dimensions', where)
local maintenance = core.need(config, 'maintenance', where)
local invalidOverrideCat = core.need(maintenance, 'invalidOverride', where .. '.maintenance')
local usingOverrideCat = core.need(maintenance, 'usingOverride', where .. '.maintenance')

local D = {}
for _, key in ipairs({
	'type',
	'launched',
	'terminated',
}) do
	D[key] = core.need(dims, key, where .. '.dimensions')
end

local typeNames = core.need(D.type, 'name', where .. '.dimensions.type')
local launchedName = core.need(D.launched, 'name', where .. '.dimensions.launched')
local undatedName = core.need(D.launched, 'undated', where .. '.dimensions.launched')
local terminatedName = core.need(D.terminated, 'name', where .. '.dimensions.terminated')

local typeSet = core.need(config, 'types', where)
local types = core.compileAliasSet({ typeSet })
local typeKeys = {}
for _, entry in ipairs(typeSet) do
	typeKeys[#typeKeys + 1] = entry.category
end
local skipTypes = config.skipTypes or {}

local function typeOverride(value)
	return core.parseExactOverride(value, typeKeys)
end

function p._state(args)
	local siteType = core.resolve {
		candidates = core.matchAliasSets(args.type, types),
		override = args.category_type,
		parse = typeOverride,
		max = 1,
	}
	local launched = core.resolve {
		candidates = core.findYears(args.launched),
		override = args.category_launched,
		parse = core.parseYearOverride,
		max = 1,
	}
	local terminated = core.resolve {
		candidates = core.findYears(args.terminated),
		override = args.category_terminated,
		parse = core.parseYearOverride,
		suppress = args.noterminatedcat,
		max = 1,
	}

	return {
		skip = siteType.values[1] ~= nil and skipTypes[siteType.values[1]] == true,
		type = siteType,
		launched = launched,
		terminated = terminated,
	}
end

function p._categories(args)
	local out = core.newList()
	local state = p._state(args)
	if state.skip then return out end

	for _, value in ipairs(state.type.values) do
		out:add(core.need(typeNames, value, where .. '.dimensions.type.name'))
	end

	if state.launched.values[1] then
		out:add(core.fill(launchedName, state.launched.values[1]))
	else
		out:add(undatedName)
	end

	for _, value in ipairs(state.terminated.values) do
		out:add(core.fill(terminatedName, value))
	end

	if core.anyCustomized(state.type, state.launched, state.terminated) then out:add(usingOverrideCat) end
	if core.anyInvalid(state.type, state.launched, state.terminated) then out:add(invalidOverrideCat) end
	return out
end

p.main = core.makeMain(p._categories, config.namespaces)

return p