Module:SafeCate

From Moegirlpedia
Jump to: navigation, search
Template-info.svg Module Documentation  [View] [Edit] [History] [Refresh]

This module is designed to handle abnormal categorization caused by occasional abnormal template deployment with cache function.

This module should be used only under Template namespace.

Parameters

  • 1: Category name to add.
  • 2: Category index, generally used to modify the sorting or advance the main article in articles of a serie on the category page.
  • nsid: The namespaces to be effective. Fill the numbers of the namespaces, seeHelp:Namespace. The use of half-width commas as separators is supported.
  • nsidExclude: The namespaces to exclude. If the parameter nsid is filled and not empty, this parameter will be uneffective.
  • noSubpage: Should not be used on subpages. It can be applied to handle incorrect template references on users' main pages.
  • strict: For packaging templates other than {{SafeCate}}. The template is effective only when this parameter is used, such as {{TemplateCate}}, which is used to limit namespaces strictly.
local module = {}

local function _isvalid(value)
	return value ~= nil and type(value) == "string" and mw.text.trim(value) ~= ""
end

function module.main(frame)
	local strict = frame
	local parent = frame:getParent()
	if not _isvalid(frame.args[1]) then
		if not (parent and _isvalid(parent.args[1])) then return end
		local title = parent:getTitle()
		if title == "Template:SafeCate" then
			strict, frame = parent, parent
		elseif mw.title.new(title).namespace == 10 and frame.args["strict"]=="1" then
			frame = parent
		else return end
	end
	local cate = frame.args[1]
	local mod = frame.args[2]
	local nsMap = { }
	local bExclude, nsids = false, nil
	if strict.args["nsid"] then
		nsids = mw.text.split(strict.args["nsid"], ",", true)
	elseif strict.args["nsidExclude"] then
		bExclude = true
		nsids = mw.text.split(strict.args["nsidExclude"], ",", true)
	end
	if nsids then
		for _, value in ipairs(nsids) do
			nsMap[tonumber(value)] = true
		end
	else
		nsMap[0] = true
	end
	local noSubpage = (strict).args["noSubpage"]=="1"
	local title = mw.title.getCurrentTitle()
	local curNsid = title.namespace
	if ((nsMap[curNsid] and (not bExclude)) or ((not nsMap[curNsid]) and bExclude)) and ((not noSubpage) or (not title.isSubpage)) then
		if curNsid == 10 and title.isSubpage and title.subpageText == "doc" then return end
		if mod then
			return "[[Category:"..cate.."|"..mod.."]]"
		else
			return "[[Category:"..cate.."]]"
		end
	end
end

return module