Module:SafeCate
跳到导航
跳到搜索
本模块旨在应对本站时而发生的模板展开异常结合缓存功能导致的分类异常问题。
本模块仅应用于模板名字空间。
参数
- 1:添加的分类名称。
- 2:分类索引,一般用于修正排序或在分类页提前系列条目的主条目。
- nsid:生效的名字空间,填数字代码,参照Help:名字空间。支持使用半角逗号做分隔符。
- nsidExclude:排除的名字空间,如填写nsid参数且不为空则此参数不生效。
- noSubpage:不应用于子页面,可以用于处理在用户主页面的错误模板引用。
- strict:用于{{SafeCate}}以外的包装模板,使用了此参数该模板才能生效,如{{TemplateCate}},用于严格限制名字空间。
- 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