local concat = table.concat
local insert = table.insert
local makeTitle = mw.title.makeTitle
local messageBox = require("Module:Message box").main
local nowiki = mw.text.nowiki
local pagesInCategory = mw.site.stats.pagesInCategory
local sort = table.sort
local trim = mw.text.trim
local yesno = require("Module:Yesno")
local title = mw.title.getCurrentTitle()
local namespace = title.namespace
local title_text = title.text
local p = {}
function p.main(frame)
local args
local len, needs_fixing = 0, {}
-- Documentation example.
if namespace == 10 and title_text:match("Category disambiguation") then
args = {
"the bird genus",
"Eremophila (bird)",
"the plant genus",
"Eremophila (plant)"
}
len = 4
-- Otherwise, process input arguments.
else
-- Produce a new args table, to get the actual length.
-- Trim input arguments, and add various maintenance warnings as needed.
-- e.g. "{{Category disambiguation|a|b|c|d||e}}" and "{{Category disambiguation|a|b|c|d|6=e}}" are invalid.
args = {}
local raw_args = (frame:getParent() or frame).args
for k, v in pairs(raw_args) do
v = trim(v)
if type(k) == "number" then
if v == "" then
insert(needs_fixing, "Parameter " .. k .. " is blank.")
v = "{{{" .. k .. "}}}"
end
if k > len then
len = k
end
end
args[k] = v
end
if len % 2 == 1 then
len = len + 1
end
if len < 4 then
insert(needs_fixing, "Should specify at least 2 categories.")
end
for i = 1, len do
if not args[i] then
insert(needs_fixing, "Parameter " .. i .. " not given.")
args[i] = "{{{" .. i .. "}}}"
end
end
end
local list = {}
for i = 2, len, 2 do
local topic, cat = args[i - 1], args[i]
local cat_title = makeTitle(14, cat)
-- Warn if the category isn't valid (e.g. "{" isn't a valid category name).
if not cat_title and cat ~= "" then
insert(needs_fixing, nowiki(cat) .. " is not a valid category title.")
end
insert(
list,
"* For " .. topic .. ", see '''[[:" .. (cat_title and cat_title.prefixedText or "Category:" .. cat) .. "]].'''"
)
-- Warn if the category is a redlink.
if cat_title and not (args.allowredlink or cat_title:getContent()) then
insert(needs_fixing, cat_title.prefixedText .. " is a redlink.")
end
end
local output = messageBox("cmbox", {
type = "content",
image = "[[File:Disambig gray.svg|50px]]",
text = "'''This category is not in use because it has an ambiguous title.'''" ..
frame:expandTemplate{
title = "Plainlist",
args = {
concat(list, "\n"),
style = "margin-left:1.6em;"
}
} ..
"'''Note:''' This category page should be empty. All entries should be recategorized under one of the above categories or an appropriate subcategory."
})
if #needs_fixing > 0 then
sort(needs_fixing)
output = output .. messageBox("cmbox", {
type = "style",
text = "[[:Template:Category disambiguation]] has the following issues:" ..
frame:expandTemplate{
title = "Plainlist",
args = {
"*" .. concat(needs_fixing, "\n*"),
style = "margin-left:1.6em;"
}
}
})
end
if namespace == 14 then
output = output ..
"__DISAMBIG__" ..
"__EXPECTUNUSEDCATEGORY__" ..
"[[Category:Disambiguation categories]]" ..
"[[Category:All disambiguation pages]]" ..
(#needs_fixing > 0 and "[[Category:Wikipedia category-disambiguation box parameter needs fixing|∃" ..
title_text .. "]]" or "") ..
(pagesInCategory(title_text) > 0 and "[[Category:Non-empty disambiguation categories]]" or "")
elseif not yesno(args.nocat, true) then
output = output .. frame:expandTemplate{
title = "Incorrect namespace",
args = {
"category"
}
}
end
return output
end
return p