置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:SafeCate

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]

本模块旨在应对本站时而发生的模板展开异常结合缓存功能导致的分类异常问题。

本模块仅应用于模板名字空间

参数

  • 1:添加的分类名称。
  • 2:分类索引,一般用于修正排序或在分类页提前系列条目的主条目。
  • nsid:生效的名字空间,填数字代码,参照Help:名字空间。支持使用半角逗号做分隔符。
  • nsidExclude:排除的名字空间,如填写nsid参数且不为空则此参数不生效。
  • noSubpage:不应用于页面,可以用于处理在用户页面的错误模板引用。
  • strict:用于{{SafeCate}}以外的包装模板,使用了此参数该模板才能生效,如{{TemplateCate}},用于严格限制名字空间。
  1. local module = {}
  2. local function _isvalid(value)
  3. return value ~= nil and type(value) == "string" and mw.text.trim(value) ~= ""
  4. end
  5. function module.main(frame)
  6. local strict = frame
  7. local parent = frame:getParent()
  8. if not _isvalid(frame.args[1]) then
  9. if not (parent and _isvalid(parent.args[1])) then return end
  10. local title = parent:getTitle()
  11. if title == "Template:SafeCate" then
  12. strict, frame = parent, parent
  13. elseif mw.title.new(title).namespace == 10 and frame.args["strict"]=="1" then
  14. frame = parent
  15. else return end
  16. end
  17. local cate = frame.args[1]
  18. local mod = frame.args[2]
  19. local nsMap = { }
  20. local bExclude, nsids = false, nil
  21. if strict.args["nsid"] then
  22. nsids = mw.text.split(strict.args["nsid"], ",", true)
  23. elseif strict.args["nsidExclude"] then
  24. bExclude = true
  25. nsids = mw.text.split(strict.args["nsidExclude"], ",", true)
  26. end
  27. if nsids then
  28. for _, value in ipairs(nsids) do
  29. nsMap[tonumber(value)] = true
  30. end
  31. else
  32. nsMap[0] = true
  33. end
  34. local noSubpage = (strict).args["noSubpage"]=="1"
  35. local title = mw.title.getCurrentTitle()
  36. local curNsid = title.namespace
  37. if ((nsMap[curNsid] and (not bExclude)) or ((not nsMap[curNsid]) and bExclude)) and ((not noSubpage) or (not title.isSubpage)) then
  38. if curNsid == 10 and title.isSubpage and title.subpageText == "doc" then return end
  39. if mod then
  40. return "[[Category:"..cate.."|"..mod.."]]"
  41. else
  42. return "[[Category:"..cate.."]]"
  43. end
  44. end
  45. end
  46. return module