Module:namespace

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Allows templates to access information about namespaces from mw.site.namespaces. Namespaces can be accessed by name or id (number). Boolean properties are converted to 1 if true, else the empty string, so that they can be used in {{#if:}}.


local export = {}

local function check(arg, what, n, allow_empty)
	arg = mw.text.trim(arg)
	if not allow_empty and arg == "" then
		arg = nil
	end
	if not arg then
		error("Supply a " .. what .. " as argument " .. n)
	end
	return arg
end

local function get_info(namespace_name_or_id, item)
	local namespace = mw.site.namespaces[namespace_name_or_id]
	if namespace == nil then
		error("Invalid namespace name or id " .. namespace_name_or_id)
	end
	local datum = namespace[item]
	if datum == nil then
		error("Invalid data item " .. item)
	end
	return datum
end

function export.info(frame)
	local namespace_name_or_id = check(frame.args[1], "namespace", 1, true)
	if not namespace_name_or_id then
		error("Supply a namespace as argument 1")
	end
	namespace_name_or_id = tonumber(namespace_name_or_id) or namespace_name_or_id
	local item = check(frame.args[2], "item to look up", 1)
	local datum = get_info(namespace_name_or_id, item)
	if type(datum) == "boolean" then
		return datum and "1" or ""
	elseif not (type(datum) == "string" or type(datum) == "number") then
		error("Unimplemented")
	end
end

return export