Module:Soysphere site 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 site = require('Module:Site categorizer')
local parser = require('Module:WikitextParser')
local tpv = require('Module:Template parameter value')
local redirect = require('Module:Redirect')
local config = mw.loadData('Module:Soysphere site categorizer/config')
local where = 'Module:Soysphere 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',
'parent',
'sister',
'generation',
}) 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 parentName = core.need(D.parent, 'name', where .. '.dimensions.parent')
local sisterName = core.need(D.sister, 'name', where .. '.dimensions.sister')
local generationNames = core.need(D.generation, 'name', where .. '.dimensions.generation')
local maxSteps = core.need(D.generation, 'maxSteps', where .. '.dimensions.generation')
local sites = core.compileAliasSet({ mw.loadData('Module:Infobox categorizer/sites') })
local bucketSites = config.bucketSites or {}
local rootSites = core.need(config, 'rootSites', where)
local function siteCanon(value)
local found, valid = core.parseAliasSetOverride(value, sites)
if valid and found[1] ~= nil and not bucketSites[found[1]] then return found[1] end
return value
end
local function linkOverride(value)
if core.normalize(value) == 'none' then return {}, true end
return core.parseLinkTargetOverride(value)
end
local function generationOverride(value)
local number = tonumber(string.match(core.trim(value), '^%d+$') or '')
if number and number > 0 then return { tostring(number) }, true end
return {}, false
end
local function maint(dim, key)
return dim.maintenance and dim.maintenance[key]
end
local function titleText(text)
local title = mw.title.new(text)
if title and not title.isExternal then return title.prefixedText end
end
local function nextLink(splinter, sister)
local link = core.findLinkTargets(splinter)[1]
if link then return link, 1 end
link = core.findLinkTargets(sister)[1]
if link then return link, 0 end
end
local function readPage(target)
local title = mw.title.new(target)
if not title then return nil end
local content = title:getContent()
if content == nil then return nil end
local redirectTarget = redirect.getTargetFromText(content)
if redirectTarget then return 'redirect', titleText(redirectTarget) end
local template = parser.getTemplate(content, 'Infobox soysphere site') -- dumb, unnecessary redirects to this template on pages will fail this function
if not template then return 'other' end -- they will be tossed out as other unless aware in config (still dumb)
return 'soysphere', tpv.getParameters(template)
end
-- trace thru raw links, category overrides apply only to the page for itself and WILL be ignored by the rest of the callers, this won't/cannot be fixed, the override
-- support at line 136 is a last resort fix
local function findGeneration(args)
local target, step = nextLink(args.splintered_from, args.sister_of)
if not target then return nil, 'none' end
local visited = { [mw.title.getCurrentTitle().prefixedText] = true }
local total, steps = 0, 0
while target do
total = total + step
if rootSites[siteCanon(target)] then
if total > 0 then return total end
return nil, 'unknown'
end
if visited[target] or steps >= maxSteps then return nil, 'loop' end
visited[target] = true
steps = steps + 1
local status, data = readPage(target)
if status == 'redirect' then
target, step = data, 0
elseif status == 'soysphere' then
target, step = nextLink(data.splintered_from, data.sister_of)
else
return nil, 'unknown'
end
end
return nil, 'unknown'
end
function p._state(args)
local state = site._state(args)
state.parent = core.resolve {
candidates = core.findLinkTargets(args.splintered_from),
override = args.category_parent,
parse = linkOverride,
canon = siteCanon,
max = 1,
}
state.sister = core.resolve {
candidates = core.findLinkTargets(args.sister_of),
override = args.category_sister,
parse = linkOverride,
canon = siteCanon,
max = 1,
}
-- let a valid override skip reading pages
-- a page using this override will not have its gen recognized by others
local generation, problem
local _, overrideValid = generationOverride(args.category_generation)
if not state.skip and not overrideValid then
generation, problem = findGeneration(args)
end
state.generation = core.resolve {
candidates = generation and { tostring(generation) } or {},
override = args.category_generation,
parse = generationOverride,
max = 1,
}
state.generationProblem = problem
return state
end
function p._categories(args)
local out = core.newList()
local state = p._state(args)
if state.skip then return out end
local function has(field)
return core.trim(args[field]) ~= ''
end
for _, value in ipairs(state.type.values) do
out:add(core.need(typeNames, value, where .. '.dimensions.type.name'))
end
if state.type.source == 'detected' then
if not has('type') then
out:add(maint(D.type, 'missing'))
elseif state.type.undetected then
out:add(maint(D.type, 'unrecognized'))
end
end
if state.type.overflow then out:add(maint(D.type, 'overflow')) 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
for _, value in ipairs(state.parent.values) do
out:add(core.fill(parentName, value))
end
if state.parent.source == 'detected' then
if not has('splintered_from') and #state.sister.values == 0 then
out:add(maint(D.parent, 'missing'))
elseif has('splintered_from') and state.parent.undetected then
out:add(maint(D.parent, 'undetected'))
end
end
if state.parent.overflow then out:add(maint(D.parent, 'overflow')) end
for _, value in ipairs(state.sister.values) do
out:add(core.fill(sisterName, value))
end
if state.sister.source == 'detected' and has('sister_of') and state.sister.undetected then
out:add(maint(D.sister, 'undetected'))
end
if state.sister.overflow then out:add(maint(D.sister, 'overflow')) end
local first = state.generation.values[1]
local generation = first and tonumber(first)
if generation then
out:add(generationNames[generation] or maint(D.generation, 'beyond'))
elseif state.generation.source == 'detected' and state.generationProblem ~= 'none' then
out:add(maint(D.generation, state.generationProblem == 'loop' and 'loop' or 'unknown'))
end
if
core.anyCustomized(state.type, state.launched, state.terminated, state.parent, state.sister, state.generation)
then
out:add(usingOverrideCat)
end
if core.anyInvalid(state.type, state.launched, state.terminated, state.parent, state.sister, state.generation) then
out:add(invalidOverrideCat)
end
return out
end
p.main = core.makeMain(p._categories, config.namespaces)
return p
