Modul:maintenance category

local require_when_needed = require("Module:require when needed")

local format_categories = require_when_needed("Module:utilities", "format_categories")
local new_title = mw.title.new
local pagetype = require_when_needed("Module:pages", "pagetype")
local process_params = require_when_needed("Module:parameters", "process")
local uses_hidden_category -- Defined below.

local current_title

local export = {}

function export.uses_hidden_category(title)
	local namespace = title.namespace
	-- Thread: and Summary: pages are named "Thread:PAGE" or "Summary:PAGE",
	-- where PAGE is the page they relate to. How we treat them therefore
	-- depends on what that page is.
	while namespace == 90 or namespace == 92 do
		title = new_title(title.text)
		namespace = title.namespace
	end
	-- User: and all talk namespaces.
	if namespace == 2 or title.isTalkPage then
		return true
	end
	local title_type = pagetype(title)
	if (
		title_type == "template sandbox" or
		title_type == "template testcase page" or
		title_type == "module sandbox" or
		title_type == "module testcase page"
	) then
		return true
	end
	return false
end
uses_hidden_category = export.uses_hidden_category

function export.get_category(name, cat)
	current_title = current_title or mw.title.getCurrentTitle()
	if uses_hidden_category(current_title) then
		name = name .. "/hidden"
	end
	return cat and format_categories({name}, nil, "-", nil, true) or name
end

function export.template(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {required = true, default = ""},
		["cat"] = {type = "boolean", default = false}
	})
	return export.get_category(args[1], args.cat)
end

return export