If you encounter a warning stating that "The template does not use the parameter(s)" while editing a page, the error is generated by this module. The module detects all parameters used by a template and issues 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 templateshould allow the parameter even though it's not going to actuallyuse the parameter, you can override the automatic parameter detection by adding|
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 parametersThe parameters '3' and 'comment' are not used, but are explicitly allowed because ....-->Written by {{{1}}} and {{{2}}} in {{{year}}}
This module generates warnings when passing invalid 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 invalid parameters when calling templates]]
.
|1=
|nocat=
(optional)localexport={}localcompare_module="Module:compare"localdebug_module="Module:debug"localmaintenance_category_module="Module:maintenance category"localparameters_module="Module:parameters"localtemplate_parser_module="Module:template parser"localutilities_module="Module:utilities"localconcat=table.concatlocalget_current_title=mw.title.getCurrentTitlelocalhtml_create=mw.html.createlocalmatch=string.matchlocalnew_title=mw.title.newlocalnext=nextlocalpairs=pairslocalrequire=requirelocalselect=selectlocalsort=table.sortlocaltostring=tostringlocaltype=typelocalfunctionfind_parameters(...)find_parameters=require(template_parser_module).find_parametersreturnfind_parameters(...)endlocalfunctionformat_categories(...)format_categories=require(utilities_module).format_categoriesreturnformat_categories(...)endlocalfunctionformatted_error(...)formatted_error=require(debug_module).formatted_errorreturnformatted_error(...)endlocalfunctionprocess_params(...)process_params=require(parameters_module).processreturnprocess_params(...)endlocalfunctionuses_hidden_category(...)uses_hidden_category=require(maintenance_category_module).uses_hidden_categoryreturnuses_hidden_category(...)endlocalcomparelocalfunctionget_compare()compare,get_compare=require(compare_module),nilreturncompareend-- Returns a table of all arguments in `template_args` which are not supported-- by `template_title` or listed in `additional`.localfunctionget_invalid_args(template_title,template_args,additional)localcontent=template_title:getContent()ifnotcontentthen-- This should only be possible if the input frame has been tampered with.error("Could not retrieve the page content of\""..template_title.prefixedText.."\".")endlocalallowed_params,seen={},{}-- Detect all params used by the parent template. param:get_name() takes the-- parent frame arg table as an argument so that preprocessing will take-- them into account, since it will matter if the name contains another-- parameter (e.g. the outer param in "{{{foo{{{bar}}}baz}}}" will change-- depending on the value for bar=). `seen` memoizes results based on the-- raw parameter text (which is stored as a string in the parameter object),-- which avoids unnecessary param:get_name() calls, which are non-trivial.forparaminfind_parameters(content)dolocalraw=param.rawifnotseen[raw]thenallowed_params[param:get_name(template_args)]=trueseen[raw]=trueendend-- Add any additional allowed arguments.ifadditionalthenlocali=0whiletruedoi=i+1localparam=additional[i]ifparam==nilthenbreakendallowed_params[param]=trueendendlocalinvalid_args=select(2,process_params(template_args,allowed_params,"return unknown"))ifnotnext(invalid_args)thenreturninvalid_argsend-- Some templates use params 1 and 3 without using 2, which means that 2-- will be in the list of invalid args when used as an empty placeholder-- (e.g. {{foo|foo||bar}}). Detect and remove any empty positional-- placeholder args.localmax_pos=0forparaminpairs(allowed_params)doiftype(param)=="number"andparam>max_posthenmax_pos=paramendendforparam,arginpairs(invalid_args)doif(type(param)=="number"andparam>=1andparam<max_posand-- Ignore if arg is empty, or only contains chars trimmed by-- MediaWiki when handling named parameters.match(arg,"^[%z\t-\v\r ]*$"))theninvalid_args[param]=nilendendreturninvalid_argsend-- Convert `args` into an array of sorted PARAM=ARG strings, using the parameter-- name as the sortkey, with numbered params sorted before strings.localfunctionargs_to_sorted_tuples(args)localmsg,i={},0fork,vinpairs(args)doi=i+1msg[i]={k,v}endsort(msg,compareorget_compare())forj=1,idomsg[j]=concat(msg[j],"=")endreturnmsgendlocalfunctionapply_pre_tag(frame,invalid_args)returnframe:extensionTag("pre",concat(invalid_args,"\n"))endlocalfunctionmake_message(template_name,invalid_args,no_link)localopen,closeifno_linkthenopen,close="",""elseopen,close="[[","]]"endreturn"The template "..open..template_name..close.." does not use the parameter(s): "..invalid_args.." Please see "..open.."Module:checkparams"..close.." for help with this warning."end-- Called by non-Lua templates using "{{#invoke:checkparams|warn}}". `frame`-- is checked for the following params:-- `1=` (optional) a comma separated list of additional allowed parameters-- `nowarn=` (optional) do not include preview warning in warning_text-- `noattn=` (optional) do not include attention seeking span in in warning_textfunctionexport.warn(frame)localparent=frame:getParent()localtemplate_name=parent:getTitle()localtemplate_title=new_title(template_name)localboolean={type="boolean"}localiargs=process_params(frame.args,{[1]={type="parameter",sublist=true},["nowarn"]=boolean,["noattn"]=boolean,})localinvalid_args=get_invalid_args(template_title,parent.args,iargs[1])-- If there are no invalid template args, return.ifnotnext(invalid_args)thenreturn""end-- Otherwise, generate "Invalid params" warning to be inserted onto the-- wiki page.localwarn,attn,catinvalid_args=args_to_sorted_tuples(invalid_args)-- Show warning in previewer.ifnotiargs.nowarnthenwarn=tostring(html_create("sup"):addClass("error"):addClass("previewonly"):tag("small"):wikitext(make_message(template_name,apply_pre_tag(frame,invalid_args))):allDone())end-- Add attentionseeking message. <pre> tags don't work in HTML attributes,-- so use semicolons as delimiters.ifnotiargs.noattnthenattn=tostring(html_create("span"):addClass("attentionseeking"):attr("title",make_message(template_name,concat(invalid_args,"; ")..".","no_link")):allDone())end-- Categorize if neither the current page nor the template would go in a hidden maintenance category.ifnot(uses_hidden_category(get_current_title())oruses_hidden_category(template_title))thencat=format_categories("Pages using invalid parameters when calling "..template_name,nil,"-",nil,"force_output")endreturn(warnor"")..(attnor"")..(cator"")end-- Called by non-Lua templates using "{{#invoke:checkparams|error}}". `frame`-- is checked for the following params:-- `1=` (optional) a comma separated list of additional allowed parametersfunctionexport.error(frame)localparent=frame:getParent()localtemplate_name=parent:getTitle()localadditional=process_params(frame.args,{[1]={type="parameter",sublist=true},})[1]localinvalid_args=get_invalid_args(new_title(template_name),parent.args,additional)-- Use formatted_error, so that we can use <pre> tags in error messages:-- any whitespace which isn't trimmed is treated as literal, so errors-- caused by double-spaces or erroneous newlines in inputs need to be-- displayed accurately.ifnext(invalid_args)thenreturnformatted_error(make_message(template_name,apply_pre_tag(frame,args_to_sorted_tuples(invalid_args))))endendreturnexport