Module:Sandbox/Theknightwho/Category disambiguation

From WikiProjectMed
Jump to navigation Jump to search
local concat = table.concat
local getArgs = require("Module:Arguments").getArgs
local insert = table.insert
local messageBox = require("Module:Message box").main
local namespace = mw.title.getCurrentTitle().namespace

local p = {}

function p.main(frame)
	-- We want to keep blanks in the list or ipairs will terminate early, but if allowredlink is blank it should be nil.
	local args = getArgs(frame, {
		removeBlanks = false,
	})
	local allowredlink = args.allowredlink ~= "" and args.allowredlink or nil
	local list, topic, needs_fixing = {}
	
	for i, arg in ipairs(args) do
		if i % 2 == 1 then
			topic = arg
		else
			insert(
				list,
				"*For " .. topic .. ", see '''[[:Category:" .. arg .. "]].'''"
			)
			-- Add "needs fixing" category if:
			-- (1) We're in the category namespace.
			-- (2) allowredlink isn't set.
			-- (3) The category is a redlink. (Use :getContent() to avoid triggering the expensive parser function count.)
			if (
				not needs_fixing and
				namespace == 14 and
				not (allowredlink or mw.title.new(arg, 14):getContent())
			) then
				needs_fixing = true
			end
		end
	end
	
	if #list < 2 then
		error("Please supply at least two categories, using the first four parameters.")
	end
	
	return 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."
	}) .. (needs_fixing and "[[Category:Wikipedia category-disambiguation box parameter needs fixing|∃" ..
		mw.title.getCurrentTitle().text .. "]]" or "")
end

return p