Module:Officeholder categorizer

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

local p = {}
local core = require('Module:Infobox categorizer')
local person = require('Module:Person categorizer') -- let person do all the shared lifting
local officeholder = require('Module:Infobox officeholder') -- we need _offices to avoid drift
local config = mw.loadData('Module:Officeholder categorizer/config')

local tenureCats = core.need(config, 'tenures', 'Module:Officeholder categorizer/config')

local function tenure(block)
	if core.trim(block.term) ~= '' then return { block = block, legacy = true } end
	local start = tonumber(core.findYears(block.term_start)[1])
	local ended = core.trim(block.term_end) ~= ''
	return {
		block = block,
		start = start,
		finish = tonumber(core.findYears(block.term_end)[1]),
		ended = ended,
		incumbent = not ended and start ~= nil,
	}
end

local function readOffices(args)
	local offices, orphans = officeholder._offices(args)
	for _, office in ipairs(offices) do
		for i, block in ipairs(office.tenures) do
			office.tenures[i] = tenure(block)
		end
	end
	for i, block in ipairs(orphans) do
		orphans[i] = tenure(block)
	end
	return offices, orphans
end

function p._state(args)
	local state = person._state(args)
	state.offices, state.orphans = readOffices(args)
	return state
end

function p._tenureMaintenance(state, out)
	local groups = {}
	for _, office in ipairs(state.offices) do
		groups[#groups + 1] = office.tenures
	end
	if #state.orphans > 0 then
		out:add(tenureCats.orphan)
		groups[#groups + 1] = state.orphans
	end

	for _, tenures in ipairs(groups) do
		local incumbents = 0
		for i, a in ipairs(tenures) do
			if not a.legacy then
				if a.start == nil then out:add(tenureCats.undated) end
				if a.start and a.finish and a.start > a.finish then out:add(tenureCats.reversed) end
				if a.ended and core.trim(a.block.successor) == '' then out:add(tenureCats.noSuccessor) end
				if a.incumbent then incumbents = incumbents + 1 end
			end
			for j = i + 1, #tenures do
				local b = tenures[j]
				if a.start and b.start then
					if (a.incumbent and b.start > a.start) or (b.incumbent and a.start > b.start) then
						out:add(tenureCats.notLatest)
					end
					local aEnd = a.incumbent and math.huge or a.finish
					local bEnd = b.incumbent and math.huge or b.finish
					if aEnd and bEnd and a.start < bEnd and b.start < aEnd then out:add(tenureCats.overlap) end
				end
			end
		end
		if incumbents > 1 then out:add(tenureCats.twoIncumbents) end
	end
end

function p._categories(args)
    -- no overrides for office categories for now
	local out = person._categories(args)
	if core.bail() then return out end -- let bail execute for the same reason it does on person
	local offices, orphans = readOffices(args)
	p._tenureMaintenance({ offices = offices, orphans = orphans }, out)
	return out
end

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

return p