Difference between revisions 21306642 and 21306643 on frwiktionary

local export = {}

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	if mw.title.getCurrentTitle().nsText == "Template" then
		return "(This template should be used on pages in the Category: namespace.)"
	elseif mw.title.getCurrentTitle().nsText ~= "Category" then
		error("This template/module can only be used on pages in the Category: namespace.")
	end
	
	local args = frame.args
	
	-- Get all the parameters and the label data
	local info = {}
	
	for key, val in pairs(args) do
		info[key] = val; if info[key] == "" then info[key] = nil end
	end
	
	if not info.template then
		error("The \"template\" parameter was not specified.")
	end
	
	local template = info.template
	info.template = nil
	
	-- Check if the category is empty
	local categories = {}
	
	if mw.site.stats.pagesInCategory(mw.title.getCurrentTitle().text, "all") == 0 then
		table.insert(categories, "[[Category:Empty categories]]")
	end
	
	-- Does the category have the correct name?
	local current = require("Module:category tree/" .. template).new(info)
	local errormessage = check_name(current, template, info)
	
	if errormessage then
		return table.concat(categories, "") .. errormessagelocal current = require("Module:category tree/" .. template).new(info)
	local display = {}
	local categories = {}
	
	-- Check if the category is empty
	local isEmpty = mw.site.stats.pagesInCategory(mw.title.getCurrentTitle().text, "all") == 0
	
	-- Are the parameters valid?
	if not current then
		table.insert(categories, "[[Category:Categories with invalid label]]")
		table.insert(categories, isEmpty and "[[Category:Empty categories]]" or nil)
		table.insert(display, show_error(
			"The label \"" .. (info.label or "") .. "\" given to the " ..
			mw.getCurrentFrame():expandTemplate{title = "temp", args = {template}} ..
			" template is not valid. You may have mistyped it, or it simply has not been created yet. " ..
			"To add a new label, please consult the documentation of the template."))
		
		-- Exit here, as all code beyond here relies on current not being nil
		return table.concat(categories, "") .. table.concat(display, "\n\n") .. "<br clear=\"all\"/>"
	end
	
	-- Does the category have the correct name?
	if mw.title.getCurrentTitle().text ~= current:getCategoryName() then
		table.insert(categories, "[[Category:Categories with incorrect name]]")
		table.insert(display, show_error(
			"Based on the parameters given to the " ..
			mw.getCurrentFrame():expandTemplate{title = "temp", args = {template}} ..
			" template, this category should be called '''[[:Category:" .. current:getCategoryName() .. "]]'''."))
	end
	
	-- Add cleanup category for empty categories
	if isEmpty and (not current.deleteEmpty or current:deleteEmpty()) then
		table.insert(categories, "[[Category:Empty categories]]")
	end
	
	-- Generate the displayed information
	local display = {}
	table.insert(display, show_breadcrumbs(current))
	table.insert(display, show_description(current))
	table.insert(display, show_children(current))
	table.insert(display, show_TOC(info))
	
	show_categories(current, categories)
	
	return table.concat(categories, "") .. table.concat(display, "\n\n") .. "<br clear=\"all\"/>"
end

function show_error(text)
	return  mw.getCurrentFrame():expandTemplate{title = "maintenance box", args = {
		"red",
		image = "[[File:Ambox warning pn.svg|50px]]",
		title = "The automatically-generated contents of this category has errors.",
		text = text,
		}}
end

-- Check the name of the current page, and return an error if it's not right.
function check_name(current, template, info)
	local errortext = nil
	local category = nil
	
	if not current then
		errortext =
			"The label \"" .. (info.label or "") .. "\" given to the " .. mw.getCurrentFrame():expandTemplate{title = "temp", args = {template}} .. " template is not valid. " ..
			"You may have mistyped it, or it simply has not been created yet. To add a new label, please consult the documentation of the template."
		category = "[[Category:Categories with invalid label]]"
	else
		local expected_name = current:getCategoryName()
		
		if expected_name ~= mw.title.getCurrentTitle().text then
			errortext = "Based on the parameters given to the " .. mw.getCurrentFrame():expandTemplate{title = "temp", args = {template}} .. " template, this category should be called '''[[:Category:" .. expected_name .. "]]'''."
			category = "[[Category:Categories with incorrect name]]"
		end
	end
	
	if errortext then
		return (category or "") .. mw.getCurrentFrame():expandTemplate{title = "maintenance box", args = {
			"red",
			image = "[[File:Ambox warning pn.svg|50px]]",
			title = "The automatically-generated contents of this category has errors.",
			text = 
	end
	
	if errortext then
		return (category or "") .. show_error(errortext,
			}})
	else
		return nil
	end
end

-- Show the parent categories that the current category should be placed in.
function show_categories(current, categories)
(contracted; show full)			return mw.getCurrentFrame():expandTemplate{title = TOC_template.text, args = {}}
		end
	end
	
	return nil
end

return export