Module:redlink category

Dokumentasie vir hierdie module kan geskep word by: Module:redlink category/doc

local export = {}

local rmatch = mw.ustring.match

---------------- for {{redlink category}} ---------------

function export.cat(frame)
	local redlink_category = ""

	local m_languages = require("Module:languages")
	local code = frame.args[1] -- language code
	local template = frame.args["template"]

	local lang = m_languages.getByCode(frame.args[1])
	local entry = require("Module:links").getLinkPage(frame.args[2], lang) -- entry name (parameter 2 in Template:m, Template:l)

	local link_object = mw.title.new (entry)
	
	-- Prevent an expensive parser function error. Unfortunately, we can't check
	-- the expensive parser function count before running the preceding code
	-- in this function.
	local success, exists
	if link_object then
		success, exists = pcall(function () return link_object.exists end)
	end

	if success and not exists then
		local langname = lang:getCanonicalName()
		
		redlink_category = "[[Category:" .. langname .. " redlinks]]"
		if template and template ~= "-" then
			redlink_category = redlink_category .. "[[Category:" .. langname .. " redlinks/" .. template .. "]]"
		end
	end

	return redlink_category

end

---------------- for {{redlinkcat}} ---------------

-- FIXME: Both of these shouldn't exist.

local redlinkcat_posname = {
	['n'] = 'nouns',
	['pn'] = 'proper nouns',
	['v'] = 'verbs',
	['a'] = 'adjectives',
	['part'] = 'participles',
	['sufn'] = 'suffixes',
	['sufv'] = 'suffixes',
	['num'] = 'numerals',
	['pron'] = 'pronouns',
	['det'] = 'determiners',
}

local redlinkcat_inflname = {
	['n'] = 'declension',
	['v'] = 'conjugation',
	['a'] = 'declension',
	['part'] = 'declension',
	['sufn'] = 'declension',
	['sufv'] = 'conjugation',
	['num'] = 'declension',
	['pron'] = 'declension',
	['det'] = 'declension',
}

local function if_not_empty(val)
	return val ~= "" and val
end

function export.redlinkcat(frame)
	local SUBPAGENAME = mw.title.getCurrentTitle().subpageText
	local args = frame:getParent().args
	local langcode = if_not_empty(args[1])
	local pos = if_not_empty(args[2])
	if langcode then
		local lang = require("Module:languages").getByCode(langcode)
		if not lang then
			if not langcode then
				error("Supply a language code in parameter 1.")
			else
				error(tostring(langcode) .. " is not a valid language code.")
			end
		end
		local canonicalname = lang:getCanonicalName()
		if not pos then
			local pos, inflname = rmatch(SUBPAGENAME, "^" .. canonicalname .. " (.*) with red links in their (.*) tables$")
			if pos then
				mw.log(langcode, require("Module:languages").getByCode(langcode), ('%q'):format(pos))
				local text = "This category contains " .. canonicalname .. " " .. pos
				text = text .. " with red links in their " .. inflname .. " tables."
				text = text .. " This category '''may''' sometimes be empty."
				local poscat = pos:gsub("^.", string.upper) .. " with red links in their " .. inflname .. " tables by language"
				local langcat = canonicalname .. " terms with red links in their inflection tables"
				text = text .. "[[Category:" .. poscat .. "]]"
				text = text .. "[[Category:" .. langcat .. "]]"
				text = text .. "__HIDDENCAT__"
				return text
			else
				return "[[Category:Terms with red links in their inflection tables by language]]"
			end
		else
			mw.log(langcode, require("Module:languages").getByCode(langcode), ('%q'):format(pos))
			local text = "This category contains " .. canonicalname .. " " .. redlinkcat_posname[pos]
			text = text .. " with red links in their " .. redlinkcat_inflname[pos] .. " tables."
			text = text .. " This category '''may''' sometimes be empty."
			local poscat = redlinkcat_posname[pos]:gsub("^.", string.upper) .. " with red links in their " .. redlinkcat_inflname[pos] .. " tables by language"
			local langcat = canonicalname .. " terms with red links in their inflection tables"
			text = text .. "[[Category:" .. poscat .. "]]"
			text = text .. "[[Category:" .. langcat .. "]]"
			text = text .. "__HIDDENCAT__"
			return text
		end
	else
		if pos then
			return "[[Category:Terms with red links in their inflection tables by language|*]]"
		else
			return "[[Category:Wiktionary maintenance]]"
		end
	end
end

return export