Module:checkparams: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
cleanup warning string
handle all return values provided by process()
Line 134: Line 134:


template_name = get_parent_title(frame)
template_name = get_parent_title(frame)
return export.process(frame:getParent(), params, template_name, nocat, noattn)
valid_args, invalid_args, invalid_args_warning = export.process(frame:getParent(), params, template_name, nocat, noattn)
return invalid_args_warning
end
end



Revision as of 18:34, 10 March 2024

Editor info

If you're seen a warning that "The template does not use the parameter(s)" while editing a page, the error was generated by this module. This module can detect all of the parameters that are used by a template and will generate a warning when a template is called with an unexpected parameter. In many cases, the error might be generated by a typo in the parameter name (eg, "pge" instead of "page") and can simply be corrected by consulting the template's documentation.

If you're absolutely certain that a template should allow the parameter even though it's not going to actually use the parameter, you can override the automatic parameter detection by adding a | plus a comma separated list of additional parameters that should be accepted to the {{#invoke:checkparams|warn}} or the {{#invoke:checkparams|error}} line at the top of the template.

For example, to allow parameters "3" and "comment" in the following template

{{#invoke:checkparams|warn}}<!-- Validate template parameters
-->Written by {{{1}}} and {{{2}}} in {{{year}}}

change it to:

{{#invoke:checkparams|warn|3,comment}}<!-- Validate template parameters
The parameters '3' and 'comment' are not used, but are explicitly allowed because ....
-->Written by {{{1}}} and {{{2}}} in {{{year}}}

Developer information

This module generates warnings when passing bad parameters to a template. It adds a user-visible warning to the preview, an {{attention}} warning to the page, and categorizes the page into Category:Pages using bad params when calling a template

Parameters

|1=
A comma separated list of additional allowed parameters, in addition to those automatically detected by the module.
|nocat= (optional)
Suppress categorization.

local export = {}

local rsplit = mw.text.split
local rfind = mw.ustring.find

local export = {}

function get_parent_title(frame)
	return frame:getParent():getTitle()
end

function get_parent_content(frame)
	local title = get_parent_title(frame)
	return mw.title.new( title ):getContent()
end

function get_template_code(text)
    res = ""
    matched = false
    for part in string.gfind(text, "<onlyinclude>(.-)</onlyinclude>") do
        res = res .. part
        matched = true
    end
    code = matched and res or text

    res = ""
    matched = false
    for part in string.gfind(code, "<includeonly>(.-)</includeonly>") do
        res = res .. part
        matched = true
    end
    if matched then
        code = res
    end

    return string.gsub(code, "<noinclude>(.-)</noinclude>", "")
end

function get_param(param)
    param = mw.text.trim(param)

    -- convert numeric to numbers
    if string.find(param, "^[0-9]+$") then
        return tonumber(param)
    end

    return param
end

function get_parent_params(frame)
	local parent_content = get_parent_content(frame)
    parent_content = get_template_code(parent_content)

	local MAGIC_WORD = {
		["FULLPAGENAME"] = true,
		["PAGENAME"] = true,
		["BASEPAGENAME"] = true,
		["NAMESPACE"] = true,
		["SUBPAGENAME"] = true,
		["SUBJECTSPACE"] = true,
		["TALKPAGENAME"] = true,
		["!"] = true,
	}

	local params = {}

	-- count any string between {{{ and | or } (minus MAGIC_WORDS) as a parameter name
	for param in string.gfind(parent_content, "{{{([^=|{}<>]-)[|}]") do
        param = get_param(param)
		if not MAGIC_WORD[param] then
			params[param] = {}
		end  
	end

	return params
end

function export.process(frame, allowed_params, template_name, nocat, noattn)
   -- This is desgined to be called by other Lua modules instead of calling Module:parameters.process()
   -- frame - the frame contaning the arguments to be checked
   -- params - a table of valid arguments
   -- template_name - the name of the calling template, used in warning message and category
   -- nocat - if specified, will not included category in warning_text
   -- noattn - if specified, will not include attention seeking span in in warning_text
   -- returns valid_args, invalid_args, warning_text

	local valid_args, invalid_args = require("Module:parameters").process(frame.args, allowed_params, "return unknown")

	local msg = ""
	for k, v in pairs(invalid_args) do
		if msg == "" then
            msg = "Invalid params in call to " .. template_name .. ": "
        else
            msg = msg .. "; "
        end
		msg = msg .. k .. "=" .. v
	end

	if msg == "" then
        return valid_args, invalid_args, ""
    end

    -- show warning in previewer
    warn = '<sup class="error previewonly"><small>' .. msg .. '</small></sup>'

    -- add attentionseeking message
    attn = noattn and "" or '<span class="attentionseeking">' .. msg .. '</span>'

    -- categorize
    cat = nocat and "" or "[[Category:Pages using bad params when calling " .. template_name .. "]]"

    result = warn .. attn .. cat
    return valid_args, invalid_args, result

end

function export.warn(frame)
   -- This is designed to be called by non-Lua templates using "{{#invoke:checkparams|warn}}"
   -- 1= (optional) a comma separated list of allowed parameters
   -- if 1= is not specified, allows all parameters referenced by the template

	local params
	if frame.args[1] then
		params = {}
		for _, param in ipairs(rsplit(allowed_params, "%s*,%s*")) do
            param = get_param(param)
			params[param] = {}
		end
	else
		params = get_parent_params(frame)
	end
    local nocat = frame.args["nocat"] or false
    local noattn = frame.args["noattn"] or false

    template_name = get_parent_title(frame)
    valid_args, invalid_args, invalid_args_warning = export.process(frame:getParent(), params, template_name, nocat, noattn)
    return invalid_args_warning
end

return export