Module:Soysphere officeholder categorizer
From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search

| This module is rated as alpha. It is ready for limited use and feedback from other editors. It may be used on a small number of pages, but should be monitored closely. Suggestions for new features or adjustments to input and output are welcome. |
| This module depends on the following other modules: |
local p = {}
local core = require('Module:Infobox categorizer')
local base = require('Module:Officeholder categorizer') -- unlike Module:Officeholder categorizer, we do not use namefag for shared lifting
-- to categorize shared params and instead rely on the full suite, and yet we also borrow some features from
-- all the way back from person for a few functions through exposed functions in Officeholder categorizer
local config = mw.loadData('Module:Soysphere officeholder categorizer/config')
local where = 'Module:Soysphere officeholder 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({
'wikiAccount',
'booruAccount',
'artistTag',
'nationality',
'state', -- USA
'site',
'first',
'final',
'status',
'jakked',
'gender',
'office', -- unlike the first 11 keys, office is a distinct feature of its own
}) do
D[key] = core.need(dims, key, where .. '.dimensions')
end
local sites = core.compileAliasSet({ mw.loadData('Module:Infobox categorizer/sites') })
local statuses = core.compileAliasSet({ core.need(config, 'statuses', where) })
local roles = core.compileAliasSet({ core.need(config, 'roles', where) })
local accountSites = config.accountSites or {}
local bucketSites = config.bucketSites or {}
local function aliasOverride(compiled)
return function(value)
return core.parseAliasSetOverride(value, compiled)
end
end
local function maint(dim, key)
return dim.maintenance and dim.maintenance[key]
end
-- office is a quirky template and requires even further tailoring
local function fillOffice(pattern, site, role, year)
if pattern == nil then return nil end
local values = { site, role, year }
return (mw.ustring.gsub(pattern, '%$(%d)', function(n)
return values[tonumber(n)]
end))
end
function p._state(args)
local state = base._state(args)
local siteMatches = core.matchAliasSets(args.frequented_sites, sites)
state.artistTag = core.trim(args.artist_tag) ~= ''
state.siteMatched = #siteMatches > 0
state.site = core.resolve {
candidates = siteMatches,
override = args.category_site,
parse = aliasOverride(sites),
filter = function(value)
return not accountSites[value]
end,
}
state.first = core.resolve {
candidates = core.findYears(args.first_appearance),
override = args.category_first,
parse = core.parseYearOverride,
max = 1,
}
state.final = core.resolve {
candidates = core.findYears(args.final_appearance),
override = args.category_final,
parse = core.parseYearOverride,
max = 1,
}
state.status = core.resolve {
candidates = core.matchAliasSets(args.status, statuses),
override = args.category_status,
parse = aliasOverride(statuses),
max = 1,
}
return state
end
-- once again, we offer no overrides for officeholder related params
local function emitOffices(state, out)
local held, order = {}, {}
local incumbent = false
for _, office in ipairs(state.offices) do
local roleMatches = core.matchAliasSets(office.office, roles)
local siteMatches = {}
for _, site in ipairs(core.matchAliasSets(office.office, sites)) do
if not bucketSites[site] then siteMatches[#siteMatches + 1] = site end
end
if #roleMatches == 0 then out:add(maint(D.office, 'unrecognizedRole')) end
if #siteMatches == 0 then out:add(maint(D.office, 'unrecognizedSite')) end
for _, role in ipairs(roleMatches) do
for _, site in ipairs(siteMatches) do
local key = site .. '|' .. role
local entry = held[key]
if not entry then
entry = { site = site, role = role }
held[key] = entry
order[#order + 1] = entry
end
for _, t in ipairs(office.tenures) do
if t.start and (entry.from == nil or t.start < entry.from) then entry.from = t.start end
local finish = t.incumbent and math.huge or t.finish -- we consider the word incumbent as an infinite that overrides any possible value
if finish and (entry.till == nil or finish > entry.till) then entry.till = finish end
end
end
end
for _, t in ipairs(office.tenures) do
if t.incumbent then incumbent = true end
end
end
for _, entry in ipairs(order) do
out:add(fillOffice(D.office.all, entry.site, entry.role))
if entry.from then out:add(fillOffice(D.office.from, entry.site, entry.role, entry.from)) end
if entry.till == math.huge then -- here incumbent knocks out any other number, because it is math.huge (infinite)
out:add(fillOffice(D.office.current, entry.site, entry.role))
elseif entry.till then
out:add(fillOffice(D.office.till, entry.site, entry.role, entry.till))
end
end
return incumbent
end
function p._categories(args)
local out = core.newList()
if core.bail() then return out end
local state = p._state(args)
local function has(field)
return core.trim(args[field]) ~= ''
end
local function emit(dim, dimState, names)
for _, value in ipairs(dimState.values) do
out:add(core.fill(dim.name, names and names[value] or value))
end
end
if state.wikiAccount then out:add(D.wikiAccount.name) end
if state.booruAccount then out:add(D.booruAccount.name) end
if state.artistTag then out:add(D.artistTag.name) end
emit(D.nationality, state.nationality, base.countryName)
if state.nationality.source == 'detected' and has('nationality') and state.nationality.undetected then
out:add(maint(D.nationality, 'unrecognized'))
end
emit(D.state, state.state, base.stateName)
emit(D.site, state.site)
if state.site.source == 'detected' and has('frequented_sites') and not state.siteMatched then
out:add(maint(D.site, 'unrecognized'))
end
emit(D.first, state.first)
if state.first.overflow then out:add(maint(D.first, 'overflow')) end
emit(D.final, state.final)
if state.final.overflow then out:add(maint(D.final, 'overflow')) end
emit(D.status, state.status)
if state.status.source == 'detected' and has('status') and state.status.undetected then
out:add(maint(D.status, 'unrecognized'))
end
local status = state.status.values[1]
local departed = has('final_appearance') or has('death_date')
if status ~= nil and status == D.status.active and departed then out:add(maint(D.status, 'conflicting')) end
if status ~= nil and status ~= D.status.active and not departed then out:add(maint(D.status, 'noDeparture')) end
if state.jakked == true then out:add(D.jakked.name) end
if has('IAS?') and state.jakked == nil then out:add(maint(D.jakked, 'unrecognized')) end
emit(D.gender, state.gender)
local incumbent = emitOffices(state, out)
if incumbent and ((status ~= nil and status ~= D.status.active) or has('final_appearance')) then
out:add(maint(D.office, 'staleIncumbent'))
end
base._tenureMaintenance(state, out) -- call on officeholder for tenuremaint
if
core.anyCustomized(
state.nationality,
state.state,
state.site,
state.first,
state.final,
state.status,
state.gender
)
then
out:add(usingOverrideCat)
end
if
core.anyInvalid(
state.nationality,
state.state,
state.site,
state.first,
state.final,
state.status,
state.gender
)
then
out:add(invalidOverrideCat)
end
return out
end
p.main = core.makeMain(p._categories, config.namespaces)
return p
