Module:Trend categorizer

From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search
This page is currently under development(You) VVILL NOT edit the page until it's properly finished.

This module was selected to run a test pilot for a bot-related task, do not make adjustments to its configs for the time being.


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

local where = 'Module:Trend 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({
	'kind',
	'type',
	'year',
	'site',
	'trendsetter',
}) do
	D[key] = core.need(dims, key, where .. '.dimensions')
end

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

local typeNames = core.need(D.type, 'name', where .. '.dimensions.type')

local sites = core.compileAliasSet({ mw.loadData('Module:Infobox categorizer/sites') }, { match = 'substring' })
local variants = core.compileAliasSet({ mw.loadData('Module:Infobox categorizer/variants') })

local function aliasOverride(compiled)
	return function(value)
		return core.parseAliasSetOverride(value, compiled)
	end
end

local function kindOverride(value)
	return core.parseExactOverride(value, { 'ias', 'nas' })
end

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

local function variantCanon(value)
	local found, valid = core.parseAliasSetOverride(value, variants)
	return valid and found[1] or value
end

local function maint(dim, key)
	return dim.maintenance and dim.maintenance[key]
end

local function pick(key, field, kind)
	local path = where .. '.dimensions.' .. key
	return core.need(core.need(D[key], field, path), kind, path .. '.' .. field)
end

function p._state(args)
	local kindMatches = core.matchAliasSets(args.type, kinds)
	local detectedKind = 'ias'
	for _, value in ipairs(kindMatches) do
		if value == 'nas' then detectedKind = 'nas' end
	end

	local kind = core.resolve {
		candidates = { detectedKind },
		override = args.category_kind,
		parse = kindOverride,
		max = 1,
	}
	local trendType = core.resolve {
		candidates = core.matchAliasSets(args.trend_type, trendTypes),
		override = args.category_type,
		parse = typeOverride,
	}
	local year = core.resolve {
		candidates = core.findYears(args.origin),
		override = args.category_year,
		parse = core.parseYearOverride,
		max = 1,
	}
	local site = core.resolve {
		candidates = core.matchAliasSets(args.origin, sites),
		override = args.category_site,
		parse = aliasOverride(sites),
	}
	local trendsetter = core.resolve {
		candidates = core.findLinkTargets(args.trendsetter),
		override = args.category_trendsetter,
		parse = core.parseLinkTargetOverride,
		canon = variantCanon,
		max = 3,
	}

	return {
		class = kind.values[1] or detectedKind,
		kindMatched = #kindMatches > 0,
		kind = kind,
		type = trendType,
		year = year,
		site = site,
		trendsetter = trendsetter,
	}
end

function p._categories(args)
	local out = core.newList()
	local state = p._state(args)
	local class = state.class

	local function has(field)
		return core.trim(args[field]) ~= ''
	end

	local function emit(pattern, dimState)
		for _, value in ipairs(dimState.values) do
			out:add(core.fill(pattern, value))
		end
	end

	if state.kind.source ~= 'override' and has('type') and not state.kindMatched then
		out:add(maint(D.kind, 'unrecognized'))
	end

	local typeNames = pick('type', 'name', class)
	for _, value in ipairs(state.type.values) do
		out:add(core.need(typeNames, value, where .. '.dimensions.type.name.' .. class))
	end
	if state.type.source == 'detected' and state.type.undetected then
		out:add(maint(D.type, has('trend_type') and 'unrecognized' or 'missing'))
	end

	if state.year.values[1] then
		out:add(core.fill(pick('year', 'name', class), state.year.values[1]))
	else
		out:add(pick('year', 'undated', class))
	end
	if state.year.overflow then out:add(maint(D.year, 'overflow')) end

	emit(pick('site', 'name', class), state.site)
	if state.site.source == 'detected' and state.site.undetected then out:add(maint(D.site, 'unrecognized')) end

	emit(pick('trendsetter', 'name', class), state.trendsetter)
	if state.trendsetter.source == 'detected' and state.trendsetter.undetected and has('trendsetter') then
		out:add(maint(D.trendsetter, 'undetected'))
	end
	if state.trendsetter.overflow then out:add(maint(D.trendsetter, 'overflow')) end

	if core.anyCustomized(state.kind, state.type, state.year, state.site, state.trendsetter) then
		out:add(usingOverrideCat)
	end
	if core.anyInvalid(state.kind, state.type, state.year, state.site, state.trendsetter) then
		out:add(invalidOverrideCat)
	end
	return out
end

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

return p