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

local function parseTemplateData(content)
	return mw.ustring.match(content, '<templatedata>')
		and mw.text.trim(mw.ustring.match(content, '<templatedata>(.-)</templatedata>'))
end

function p.checkParameters(frame)
	local parent = frame:getParent()
	local template = mw.title.new(parent:getTitle())
	local TemplateDataJSON = parseTemplateData(template:getContent())
	if not TemplateDataJSON then
		local doc = mw.title.new(template.fullText .. '/doc')
		TemplateDataJSON = doc.exists and parseTemplateData(doc:getContent())
	end
	if not TemplateDataJSON then
		mw.log(mw.ustring.format("TemplateData šablony %s nenalezena", template.text))
		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.text))
			local relevant_title = 'šabloně ' .. template.text
			if mw.ustring.sub(template.text, 1, 10) == 'Infobox - ' then
				relevant_title = 'infoboxu ' .. mw.ustring.sub(template.text, 11)
			end
			return mw.ustring.format('[[Kategorie:Údržba:Neznámý parametr v %s|%s]]',
				relevant_title, mw.title.getCurrentTitle().text)
		end
	end
	return nil
end

return p