Přeskočit na obsah

Modul:CheckParameters

Z Wikipedie, otevřené encyklopedie
local p = {}

local function inSequence(param, sequence)
	for _, value in pairs(sequence) do
		if param == value then
			return true
		end
	end
	return false
end

function p.checkParameters(frame)
	local parent = frame:getParent()
	local template = mw.title.new(parent:getTitle())
	local template_content = template:getContent()
	local TemplateDataJSON
	if mw.ustring.match(template_content, '<templatedata>') then
		TemplateDataJSON = mw.text.trim(mw.ustring.match(template_content, '<templatedata>(.-)</templatedata>'))
	else
		local doc = mw.title.new(template.fullText .. '/doc')
		if doc.exists then
			local doc_content = doc:getContent()
			if mw.ustring.match(doc_content, '<templatedata>') then
				TemplateDataJSON = mw.text.trim(mw.ustring.match(doc_content, '<templatedata>(.-)</templatedata>'))
			end
		end
	end
	if not TemplateDataJSON then
		mw.log(mw.ustring.format("TemplateData šablony %s nenalezena", template.fullText))
		return nil
	end

	local TemplateData = mw.text.jsonDecode(TemplateDataJSON)
	local used_params = parent.args or {}
	local all_params = {}
	if TemplateData.params then
		for param, data in pairs(TemplateData.params) do
			table.insert(all_params, param)
			if data.aliases then
				for _, alias in pairs(data.aliases) do
					table.insert(all_params, alias)
				end
			end
		end
	end
	for param in pairs(used_params) do
		if not inSequence(param, all_params) then
			mw.log(mw.ustring.format('Parametr %s nenalezen v TemplateData šablony %s', param, template.fullText))
			return mw.ustring.format(
				'[[Kategorie:Údržba:Neznámý parametr v %s|%s]]',
				function()
					if mw.ustring.sub(template.text, 1, 10) == 'Infobox - ' then
						return 'infoboxu ' .. mw.ustring.sub(template.text, 11)
					else
						return 'šabloně ' .. template.text
					end
				end,
				mw.getCurrentTitle()
			)
		end
	end
	return nil
end

return p