Module:maintenance category

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module implements the template {{maintenance category}}.


local export = {}

local pages_module = "Module:pages"
local parameters_module = "Module:parameters"
local utilities_module = "Module:utilities"

local find = string.find
local new_title = mw.title.new
local uses_hidden_category -- Defined below.

--[==[
Loaders for functions in other modules, which overwrite themselves with the target function when called. This ensures modules are only loaded when needed, retains the speed/convenience of locally-declared pre-loaded functions, and has no overhead after the first call, since the target functions are called directly in any subsequent calls.]==]
	local function format_categories(...)
		format_categories = require(utilities_module).format_categories
		return format_categories(...)
	end
	
	local function pagetype(...)
		pagetype = require(pages_module).pagetype
		return pagetype(...)
	end
	
	local function process_params(...)
		process_params = require(parameters_module).process
		return process_params(...)
	end

--[==[
Loaders for objects, which load data (or some other object) into some variable, which can then be accessed as "foo or get_foo()", where the function get_foo sets the object to "foo" and then returns it. This ensures they are only loaded when needed, and avoids the need to check for the existence of the object each time, since once "foo" has been set, "get_foo" will not be called again.]==]
	local current_title
	local function get_current_title()
		current_title, get_current_title = mw.title.getCurrentTitle(), nil
		return current_title
	end

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)
	return (find(title_type, "sandbox", 1, true) or find(title_type, "testcase page", 1, true)) and true or false
end
uses_hidden_category = export.uses_hidden_category

function export.get_category(name, cat)
	if uses_hidden_category(current_title or get_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