Module:maintenance category: Difference between revisions

From Linguifex
Jump to navigation Jump to search
Created page with "local format_categories = require("Module:utilities").format_categories local match = string.match local process_params = require("Module:parameters").process local title local export = {} local function ret(name, cat) return cat and format_categories({name}, nil, "-", nil, true) or name end function export.get_category(name, cat) title = title or mw.title.getCurrentTitle() -- All talkpages. if title.isTalkPage then return ret(name .. "/hidden", cat) end loca..."
 
mNo edit summary
Line 1: Line 1:
local format_categories = require("Module:utilities").format_categories
local export = {}
local match = string.match
local process_params = require("Module:parameters").process


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


local export = {}
local new_title = mw.title.new
local uses_hidden_category -- Defined below.


local function ret(name, cat)
--[==[
return cat and format_categories({name}, nil, "-", nil, true) or name
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.]==]
end
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


function export.get_category(name, cat)
--[==[
title = title or mw.title.getCurrentTitle()
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.]==]
-- All talkpages.
local current_title
if title.isTalkPage then
local function get_current_title()
return ret(name .. "/hidden", cat)
current_title, get_current_title = mw.title.getCurrentTitle(), nil
return current_title
end
end
function export.uses_hidden_category(title)
local namespace = title.namespace
local namespace = title.namespace
-- User:, Thread:, and Summary:
-- Thread: and Summary: pages are named "Thread:PAGE" or "Summary:PAGE",
if namespace == 2 or namespace == 90 or namespace == 92 then
-- where PAGE is the page they relate to. How we treat them therefore
return ret(name .. "/hidden", cat)
-- depends on what that page is.
-- Template: and Module:.
while namespace == 90 or namespace == 92 do
elseif not (namespace == 10 or namespace == 828) then
title = new_title(title.text)
return ret(name, cat)
namespace = title.namespace
end
end
local title_text = title.text
-- User: and all talk namespaces, as well as all sandboxes and testcase pages (including their documentation pages).
if (
return (
-- Template:User: and Module:User:.
namespace == 2 or
match(title_text, "^User:") or
title.isTalkPage or
-- Any template/module sandboxes (which can be numbered and/or followed by further subpages), including any templates/modules with the name sandbox.
is_sandbox(title, "include documentation") or
match(title_text, "%f[^%z/][sS][aA][nN][dD][bB][oO][xX] ?%d*%f[%z/]") or
is_testcase_page(title, "include documentation")
-- Any template/module testcases (which can be numbered and/or followed by further subpages).
) and true or false
match(title_text, "/[tT][eE][sS][tT][cC][aA][sS][eE][sS]? ?%d*%f[%z/]")
end
) then
uses_hidden_category = export.uses_hidden_category
return ret(name .. "/hidden", cat)
 
function export.get_category(name, cat)
if uses_hidden_category(current_title or get_current_title()) then
name = name .. "/hidden"
end
end
return ret(name, cat)
return cat and format_categories(name, nil, "-", nil, true) or name
end
end



Revision as of 02:33, 22 December 2024

Documentation for this module may be created at Module:maintenance category/doc

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