Mô đun:Category disambiguation

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 remove = table.remove
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("Định hướng thể loại") then
		args = {
			"chi của chim",
			"Eremophila (chi chim)",
			"chi của thực vật",
			"Eremophila (chi thực vật)"
		}
		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.
		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
				len = k > len and k or len
			end
			args[k] = v
		end
		-- Number of parameters should be even.
		local orig_len = len
		if len % 2 == 1 then
			-- Don't give a blank parameter warning if the template call ends "|}}".
			if args[len] == "{{{" .. len .. "}}}" then
				args[len] = nil
				len = len - 1
				remove(needs_fixing)
			-- Otherwise we need a blank final parameter for the missing category.
			else
				len = len + 1
			end
		end
		if len < 4 then
			insert(needs_fixing, "Phải chỉ định ít nhất 2 thể loại.")
		end
		-- Fill out any missing parameters, but stop inputs like {{Category disambiguation|10000=foo}} causing a cascade of warnings.
		local missing = 0
		for i = 1, len do
			if not args[i] then
				insert(needs_fixing, "Tham số " .. i .. " chưa được chỉ định.")
				args[i] = "{{{" .. i .. "}}}"
				missing = missing + 1
				if missing == 10 then
					error("Quá nhiều tham số bị để trống giữa 1 và " .. orig_len .. " (tham số cao nhất được chỉ định)")
				end
			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 then
			insert(needs_fixing, nowiki(cat) .. " không phải tiêu đề thể loại hợp lệ.")
		end
		insert(list, "* Đối với " .. topic .. ", xem '''[[:" .. (cat_title and cat_title.prefixedText or "Thể loại:" .. 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 .. " không tồn tại.")
		end
	end
	
	local output = messageBox("cmbox", {
		type  = "content",
		image = "[[File:Disambig gray.svg|50px]]",
		text = "'''Thể loại này không được sử dụng vì tiêu đề của nó không rõ ràng.'''" .. 
			frame:expandTemplate{
				title = "Plainlist",
				args = {
					concat(list, "\n"),
					style = "margin-left:1.6em;"
				}
			} ..
			"'''Ghi chú:''' Thể loại này nên được để trống. Tất cả các thể loại con và trang cần được phân loại lại vào một trong các thể loại trên hoặc một thể loại con thích hợp hơn."
	})
	
	-- Add maintenance warnings if necessary.
	if #needs_fixing > 0 then
		sort(needs_fixing)
		output = output .. messageBox("cmbox", {
			type  = "style",
			text = frame:expandTemplate{
					title = "Template link",
					args = {
						"Định hướng thể loại"
					}
				} .. " có (các) vấn đề sau:" .. 
				frame:expandTemplate{
					title = "Plainlist",
					args = {
						"*" .. concat(needs_fixing, "\n*"),
						style = "margin-left:1.6em;"
					}
				}
		})
	end
	
	-- Only add behaviour switches and categories if in the category namespace.
	if namespace == 14 then
		output = output ..
			"__DISAMBIG__" ..
			"__EXPECTUNUSEDCATEGORY__" ..
			"[[Thể loại:Trang định hướng thể loại]]" ..
			"[[Thể loại:Tất cả các trang định hướng]]" ..
			(#needs_fixing > 0 and "[[Thể loại:Tham số hộp trang định hướng thể loại Wikipedia cần sửa|∃" ..
		title_text .. "]]" or "") ..
			(pagesInCategory(title_text) > 0 and "[[Thể loại:Trang định hướng thể loại không trống]]" or "")
	elseif not yesno(args.nocat, true) then
		output = output .. frame:expandTemplate{
			title = "Incorrect namespace",
			args = {
				"category"
			}
		}
	end
	
	return output
end

return p