ข้ามไปเนื้อหา

มอดูล:maintenance category

จาก วิกิพจนานุกรม พจนานุกรมเสรี

local export = {}

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

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 is_sandbox(...)
		is_sandbox = require(pages_module).is_sandbox
		return is_sandbox(...)
	end
	
	local function is_testcase_page(...)
		is_testcase_page = require(pages_module).is_testcase_page
		return is_testcase_page(...)
	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, as well as all sandboxes and testcase pages (including their documentation pages).
	return (
		namespace == 2 or
		title.isTalkPage or
		is_sandbox(title, "include documentation") or
		is_testcase_page(title, "include documentation")
	) 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