Modul:Check for unknown parameters

Z Wikipedie, otevřené encyklopedie

Check for unknown parameters může být použit ke kontrole neznámých parametrů v libovolné šabloně.

Použití[editovat zdroj]

{{#invoke:Check for unknown parameters|check}}

Pokud šablona volá pouze modul (tj. z kódu šablony nelze vyčíst seznam parametrů), je zařazena do kategorie Monitoring:Chybná volání modulu Check for unknown parameters. To znamená, že je nutné vyplnit i seznam parametrů následovně:

{{#invoke:Check for unknown parameters|check|1|2|parametr3|parametr4|regexp1 = parametr[%d]}}

Pokud se ve volání dané šablony vyskytne neznámý parametr, je stránka zařazena do kategorie Monitoring:Neznámý parametr výšk v šabloně|čtverec. Seznam takových kategorií je k vidění zde.

Podrobnější nápovědu naleznete na stránce Wikipedie:Monitoring.
-- This module may be used to compare the arguments passed to the template
-- with a list of known template arguments, returning a specified result
-- if an argument is not on the list
local p = {}

local function trim(s)
	return s:match('^%s*(.-)%s*$')
end

local function isnotempty(s)
	return s and trim(s) ~= ''
end

function p.check (frame)
	local args = frame.args
	local pargs = frame:getParent().args
	local ignoreblank = isnotempty(frame.args['ignorovat prázdné'])
	local showblankpos = isnotempty(frame.args['zobrazit prázdné'])
	local knownargs = {}
	local template = mw.title.new(frame:getParent():getTitle())
	local unknown = frame.args['chyba'] or '[[Kategorie:Monitoring:Neznámý parametr _VALUE_ v infoboxu|' .. string.gsub(template.text, '^Infobox[%- ]+', '') .. ']]'
	local preview = frame.args['náhled']

	local values = {}
	local res = {}
	local regexps = {}

	-- create the automatic list of known params
	local lines = template:getContent()
	lines = string.gsub(lines, '{{{', '\n{{{')
	for param in lines:gmatch('[^\r\n]+') do
		if string.find(param, '{{{') then
			param = string.gsub(param, '{{{', '')
			param = string.gsub(param, '[<|}][^\n]*', '')
			knownargs[param] = 1
		end
	end

	-- add entered params, and regular expressions
	for k, v in pairs(args) do
		if type(k) == 'number' then
			v = trim(v)
			knownargs[v] = 1
		elseif k:find('^regexp[1-9][0-9]*$') then
			table.insert(regexps, '^' .. v .. '$')
		end
	end

	local knownargsi = {}
	for k, v in pairs(knownargs) do knownargsi[v] = k end
	if #knownargsi == 0 and template == mw.title.getCurrentTitle() then
		table.insert(res, '[[Kategorie:Monitoring:Chybné volání modulu ' .. mw.title.new(frame:getTitle()).text .. ']]')
	end

	-- create the return string
	if isnotempty(preview) then 
		preview = '<div class="hatnote" style="color:red"><strong>Chyba:</strong> ' .. preview .. ' (tato chybová hláška je zobrazena pouze v náhledu).</div>'
	elseif preview == nil then
		preview = unknown
	end

	-- loop over the parent params, and make sure they are on the list
	for k, v in pairs(pargs) do
		if type(k) == 'string' and knownargs[k] == nil then
			local knownflag = false
			for i, regexp in ipairs(regexps) do
				if mw.ustring.match(k, regexp) then
					knownflag = true
					break
				end
			end
			if not knownflag and ( not ignoreblank or isnotempty(v) ) then
				k = mw.ustring.gsub(k, '[^%w\-_ ]', '?')
				table.insert(values, k)
			end
		elseif type(k) == 'number' and 
			knownargs[tostring(k)] == nil and
			( showblankpos or isnotempty(v) )
		then
			local vlen = mw.ustring.len(v)
			v = mw.ustring.sub(v, 1, (vlen < 25) and vlen or 25) 
			v = mw.ustring.gsub(v, '[^%w\-_ ]', '?')
			table.insert(values, k .. ' = ' .. v .. ((vlen >= 25) and ' ...' or ''))
		end
	end

	-- add results to the output tables
	if #values > 0 then
		if frame:preprocess( "{{REVISIONID}}" ) == "" then
			unknown = preview
		end
		for k, v in pairs(values) do
			if v == '' then
			-- Fix odd bug for | = which gets stripped to the empty string and
			-- breaks category links
			v = ' '
			end
			local r = unknown:gsub('_VALUE_', v)
			table.insert(res, r)
		end
	end

	return table.concat(res)
end

return p