Амодуль:Category

Аматериал Авикипедиа аҟынтә - зхы иақәиҭу аенциклопедиа

require "Амодуль:No globals"

local p = {}

local function checkNamespace(namespace)
	if not namespace or namespace == '' then
		namespace = 0
	end

	if type(namespace) == 'string' then
		namespace = mw.text.split(namespace, ',')
	elseif type(namespace) ~= 'table' then
		namespace = { namespace }
	end

	local namespace_map = mw.site.namespaces
	local title = mw.title.getCurrentTitle()
	for _, namespace in ipairs(namespace) do
		if tonumber(namespace) then
			if title.namespace == tonumber(namespace) then
				return true
			end
		end
		local namespace_data = namespace_map[namespace]
		if namespace_data then
			if title.namespace == namespace_data.id then
				return true
			end
		end
	end
	return false
end

function p.makeCategory(catname, namespace, key)
	if not catname or catname == '' then
		return ''
	end

	if not checkNamespace(namespace) then
		return ''
	end

	if key and key ~= '' then
		return "[[Акатегориа:" .. catname .. "|" .. key .. "]]"
	else
		return "[[Акатегориа:" .. catname .. "]]"
	end
end

function p.fromTemplate(frame)
	local parent = frame:getParent()
	local params = parent.args

	return p.makeCategory(params[1], params[2], params[3])
end

return p