יחידה:ParamValidator
This module is based on idea and original code ofUser:IKhitron.
the source of this module is in //he.wikipedia.org/wiki/Module:ParamValidator
This module exports two functions: calculateViolations( frame, subpages ), and validateParams( frame ).
calculateViolations( frame, subpages ) finds templatedata, in template page or in one of its subpages in the list, if provided. it returns a table with the violations. if there are none, the table is empty. otherwise it has the structure
{ violation1 = { param1 = value1, param2 = value2 }, violation2 = {... }, ... }
violation1, violation2 etc. are one of the names of specific violations, as described below. param1, param2 etc. are either the names of parameter passed to the template, or defined in templatedata. value1, value2 etc. are the values passed to the template, or an empty string if no such parameter was passed.
the different violations are as follow:
- no-templatedata
no valid tempaltedata was found in tempalte page, or documentation subpage - undeclared
named parameters with non-empty value, does not exist in templatedata - empty-undeclared
named parameters with empty value, does not exist in templatedata - undeclared-numeric
numeric parameters with non-empty value, does not exist in templatedata - empty-undeclared-numeric
numeric parameters with empty value, does not exist in templatedata - deprecated
parameters with non-empty value, marked as "deprecated" in tempaltedata - empty-deprecated
parameters with empty value, marked as "deprecated" in tempaltedata - empty-required
missing or empty parameter marked as "required" in tempaltedata - incompatible
a non-empty parameter passed to the template, incompatible with the parameter type defined in templatedata
The second function, validateParams( frame ), can be called from the tempalte' using #invoke.
it expects a parameter named "options", which contains the definition of the output. typically, it's used by placing something like so:
<includeonly>{{#invoke:ParamValidator | validateParams | options = {{PV default options}} }}</includeonly>
at the top of the template (be mindful not to add extra spaces and newlines to the template).
the options parameter should be a JSON-encoded string, defining the output, and some special behaviors.
the example above assumes that a wiki page namedTemplate:PV default options
exists, and contains valid JSON string.
for each of the violations defined above, "options" may define an output string, so basically, "options" looks like so:
{ violation1: outputstring1, violation2: outputstring2, ...., behavior1: some value, .... }
not all violations have to be defined. a violation not defined in "options" will be ignored.
when invoked, it extract "subpages" from the options parameter, and calls:
- calculateViolations( frame, subpages )
if the returned table is empty, no violation were found, and an empty string is returned and nothing else happens.
otherwise, for each of the violations, i.e., the keys of the returned table, when "options" contains this key, the corresonding value is appended to the output.
some further processing is done:
- several tokens are replaced with calculated values. these are described below.
- some "meta" violations are calculated: when any none-ignored violation occured, the "any" meta-violation is added to the output in the same way, i.e. the string keyed by "any" in the options is appended to output with appropriate substitutions. similarly, "multiple" meta-violation is created when more than one type of non-ignored violations occured.
- if the output is not empty, a prefix and suffix strings are prepended and appended to it.
these are the tokens and the replacement.
- templatename
full template name, including namespace. - tname_naked
template name without namespace. - paramname
comma-separated list of parameters - paramandvalue
is replaced by comma-separated list of "name: value" pairs of parameters and values
the first two are applied to the whole output, including the suffux and prefix, and the rest are applied to the individual violations, each with its own list of offending parameters and values.
the rest of the if the value of some keys is null, this error condition will be ignored, and not counted when calculating "any" and "multiple" conditions.
some other optional fields can be passed via options:
- doc-subpage
can be either a string, or a list (in square bracktes) of strings, indicating subpages of the template that may contain templatedata. - ignore
list of patterns. any parameter whose name matches any pattern, will not considered in violation of any of the rules. - skip-empty-numeric
if a quoted number, the module will ignore non-declared empty numeric parameters up to this number - wrapper-prefix
openning wrapper element of outpot (defaults to<div class = 'paramvalidator-wrapper'>
) - wrapper-suffix
closing wrapper element of output (defaults to "</div>" )
additional option parameters, named options1, options2, etc. can be passed. any entry defined in these options will override the previous value. a typical use may be like so:
typically, this JSON structure will be placed in a separate template, and retrieved for the module-use as shown above.
<includeonly>{{#invoke:ParamValidator | validateParams | options = {{PV default options}} | options1 = { "key": "value" } }}</includeonly>
"key" can override any of the options fields described above.
--[=[
This module is based on idea and original code of [[User:IKhitron]].
the source of this module is in //he.wikipedia.org/wiki/Module:ParamValidator
main purpose: use "templatedata" to verify the parameters passed to a template
Terminology: "numeric parameter" means order-based parameter. e.g. if the template is transcluded like so {{x | k | | a = m | b = }}
"a" and "b" are "named" parameters, and there are 2 "numeric", or order based parameters, 1 and 2.
we say that the value of a is "m", the value of 1 is "k", and "b" and 2 are "empty".
This module exports two functions: calculateViolations( frame, subpages ), and validateParams( frame ).
calculateViolations( frame, subpages ) finds templatedata, in template page or in one of its subpages in the list, if provided.
it returns a table with the violations. if there are none, the table is empty. otherwise it has the structure
{
violation1 = { param1 = value1, param2 = value2 },
violation2 = {... },
...
}
violation1, violation2 etc. are one of the names of specific violations, as described below.
param1, param2 etc. are either the names of parameter passed to the template, or defined in templatedata.
value1, value2 etc. are the values passed to the template, or an empty string if no such parameter was passed.
the different violations are as follow:
* "no-templatedata": no valid tempaltedata was found in tempalte page, or documentation subpage
* "undeclared": named parameters with non-empty value, does not exist in templatedata
* "empty-undeclared": named parameters with empty value, does not exist in templatedata
* "undeclared-numeric": numeric parameters with non-empty value, does not exist in templatedata
* "empty-undeclared-numeric": numeric parameters with empty value, does not exist in templatedata
* "deprecated": parameters with non-empty value, marked as "deprecated" in tempaltedata
* "empty-deprecated": parameters with empty value, marked as "deprecated" in tempaltedata
* "empty-required": missing or empty parameter marked as "required" in tempaltedata
* "incompatible": a non-empty parameter passed to the template, incompatible with the parameter type defined in templatedata
* "duplicate": a value is passed for the same parameter (or any of its aliases) more than once
The second function, validateParams( frame ), can be called from the tempalte' using #invoke.
it expects a parameter named "options", which contains the definition of the output. typically, it's used by placing something like so:
<includeonly>{{#invoke:ParamValidator | validateParams | options = {{PV default options}} }}</includeonly>
at the top of the template (be mindful not to add extra spaces and newlines to the template).
to bypass some mediawiki limitation, it is also possible to pass the options as "module", like so (use one of the two, but not both):
<includeonly>{{#invoke:ParamValidator | validateParams | module_options = Module:PV default options}} }}</includeonly>
the first form expects a template named "Template:PV default options" which contains the options, and the 2nd form expects a module,
suitable for mw.loadData(), which returns a map of namespace => options (i.e. { [0] = <options>, [2] => <options> }.... )
the options parameter should be a JSON-encoded string, defining the output, and some special behaviors.
the example above assumes that a wiki page named [[Template:PV default options]] exists, and contains valid JSON string.
for each of the violations defined above, "options" may define an output string, so basically, "options" looks like so:
{
violation1: outputstring1,
violation2: outputstring2,
....,
behavior1: some value,
....
}
not all violations have to be defined. a violation not defined in "options" will be ignored.
when invoked, it extract "subpages" from the options parameter, and calls:
calculateViolations( frame, subpages )
if the returned table is empty, no violation were found, and an empty string is returned and nothing else happens.
otherwise, for each of the violations, i.e., the keys of the returned table, when "options" contains this key,
the corresonding value is appended to the output.
some further processing is done:
1) several tokens are replaced with calculated values. these are described below.
2) some "meta" violations are calculated: when any none-ignored violation occured,
the "any" meta-violation is added to the output in the same way,
i.e. the string keyed by "any" in the options is appended to output with appropriate substitutions.
similarly, "multiple" meta-violation is created when more than one type of non-ignored violations occured.
3) if the output is not empty, a prefix and suffix strings are prepended and appended to it.
these are the tokens and the replacement.
* "templatename": full template name, including namespace.
* "tname_naked": template name without namespace.
* "paramname": comma-separated list of parameters
* "paramandvalue": is replaced by comma-separated list of "name: value" pairs of parameters and values
the first two are applied to the whole output, including the suffux and prefix,
and the rest are applied to the individual violations, each with its own list of offending parameters and values.
the rest of the if the value of some keys is null, this error condition will be ignored, and not counted when calculating "any" and "multiple" conditions.
some other optional fields can be passed via options:
* "doc-subpage": can be either a string, or a list (in square bracktes) of strings, indicating subpages of the template
that may contain templatedata.
* "ignore": list of patterns. any parameter whose name matches any pattern, will not considered in violation of any of the rules.
* "skip-empty-numeric": if a quoted number, the module will ignore non-declared empty numeric parameters up to this number
* "wrapper-prefix": openning wrapper element of outpot (defaults to "<div class = 'paramvalidator-wrapper'>" )
* "wrapper-suffix": closing wrapper element of output (defaults to "</div>" )
additional option parameters, named options1, options2, etc. can be passed. any entry defined in these options will
override the previous value. a typical use may be like so:
typically, this JSON structure will be placed in a separate template, and retrieved for the module-use as shown above.
<includeonly>{{#invoke:ParamValidator | validateParams | options = {{PV default options}} | options1 = { "key": "value" } }}</includeonly>
"key" can override any of the options fields described above.
]=]
localutil={
empty=function(s)
returns==nilortype(s)=='string'and(mw.text.trim(s)==''ors=='-')-- compatible with module:תבנית מידע
end
,
extract_options=function(frame,optionsPrefix)
optionsPrefix=optionsPrefixor'options'
localoptions,n,more={}
ifframe.args['module_options']then
localmodule_options=mw.loadData(frame.args['module_options'])
iftype(module_options)~='table'thenreturn{}end
localtitle=mw.title.getCurrentTitle()
locallocal_ptions=module_options[title.namespace]ormodule_options[title.nsText]or{}
fork,vinpairs(local_ptions)dooptions[k]=vend
end
repeat
ok,more=pcall(mw.text.jsonDecode,frame.args[optionsPrefix..(nor'')])
ifokandtype(more)=='table'then
fork,vinpairs(more)dooptions[k]=vend
end
n=(nor0)+1
untilnotok
returnoptions
end
,
build_namelist=function(template_name,sp)
localres={template_name}
ifspthen
iftype(sp)=='string'thensp={sp}end
for_,pinipairs(sp)dotable.insert(res,template_name..'/'..p)end
end
returnres
end
,
table_empty=function(t)-- normally, test if next(t) is nil, but for some perverse reason, non-empty tables returned by loadData return nil...
iftype(t)~='table'thenreturntrueend
fora,binpairs(t)doreturnfalseend
returntrue
end
,
}
localfunction_readTemplateData(templateName)
localtitle=mw.title.makeTitle(0,templateName)
localtemplateContent=titleandtitle.existsandtitle:getContent()-- template's raw content
localcapture=templateContentandmw.ustring.match(templateContent,'<templatedata%s*>(.*)</templatedata%s*>')-- templatedata as text
-- capture = capture and mw.ustring.gsub( capture, ' "(%d+)" ', tonumber ) -- convert "1": {} to 1: {}. frame.args uses numerical indexes for order-based params.
localtrailingComma=captureandmw.ustring.find(capture,',%s*[%]%}]')-- look for,] or,}: jsonDecode allows it, but it's verbotten in json
ifcaptureandnottrailingCommathenreturnpcall(mw.text.jsonDecode,capture)end
returnfalse
end
localfunctionreadTemplateData(templateName)
iftype(templateName)=='string'then
templateName={templateName,templateName..'/'..docSubPage}
end
iftype(templateName)=="table"then
for_,nameinipairs(templateName)do
localtd,result=_readTemplateData(name)
iftdthenreturnresultend
end
end
returnnil
end
-- this is the function to be called by other modules. it expects the frame, and then an optional list of subpages, e.g. { "Documentation" }.
-- if second parameter is nil, only tempalte page will be searched for templatedata.
localfunctioncalculateViolations(frame,subpages)
-- used for parameter type validy test. keyed by TD 'type' string. values are function(val) returning bool.
localtype_validators={
['number']=function(s)returnmw.language.getContentLanguage():parseFormattedNumber(s)end
}
localfunctioncompatible(typ,val)
localfunc=type_validators[typ]
returntype(func)~='function'orutil.empty(val)orfunc(val)
end
localfunctionlist_empty_or_contains(ar,searched)
ifnotaror#ar==0thenreturntrueend
for_,valinipairs(ar)doifval==searchedthenreturntrueendend
returnfalse
end
localt_frame=frame:getParent()
localt_args,template_name=t_frame.args,t_frame:getTitle()
localtd_source=util.build_namelist(template_name,subpages)
localtemplatedata=readTemplateData(td_source)
localtd_params=templatedataandtemplatedata.params
localall_aliases,all_series={},{}
ifnottd_paramsthenreturn{['no-templatedata']={['']=''}}end
-- from this point on, we know templatedata is valid.
localres={}-- before returning to caller, we'll prune empty tables
-- allow for aliases
for_,pinpairs(td_params)dofor_,aliasinipairs(p.aliasesor{})do
all_aliases[alias]=p
iftonumber(alias)thenall_aliases[tonumber(alias)]=pend
endend
-- handle undeclared and deprecated
localalready_seen={}
localseries=frame.args['series']
forp_name,valueinpairs(t_args)do
localtp_param,noval,numeric,table_name=td_params[p_name]orall_aliases[p_name],util.empty(value),tonumber(p_name)
localhasval=notnoval
ifnottp_paramandseriesthen-- 2nd chance. check to see if series
fors_name,pinpairs(td_params)do
ifmw.ustring.match(p_name,'^'..s_name..'%d+'..'$')then
-- mw.log('found p_name '.. p_name.. ' s_name:'.. s_name, ' p is:', p) debugging series support
tp_param=p
end-- don't bother breaking. td always correct.
end
end
ifnottp_paramthen-- not in TD: this is called undeclared
-- calculate the relevant table for this undeclared parameter, based on parameter and value types
table_name=
novalandnumericand'empty-undeclared-numeric'or
novalandnotnumericand'empty-undeclared'or
hasvalandnumericand'undeclared-numeric'or
'undeclared'-- tzvototi nishar.
else-- in td: test for deprecation and mistype. if deprecated, no further tests
table_name=tp_param.deprecatedandhasvaland'deprecated'
ortp_param.deprecatedandnovaland'empty-deprecated'
ornotcompatible(tp_param.type,value)and'incompatible'
ornotseriesandalready_seen[tp_param]andhasvaland'duplicate'
orhasvalandnotlist_empty_or_contains(tp_param.suggestedvalues,value)and'unsuggested-value'
already_seen[tp_param]=hasval
end
-- report it.
iftable_namethen
res[table_name]=res[table_name]or{}
res[table_name][p_name]=value
end
end
-- test for empty/missing paraeters declared "required"
forp_name,paraminpairs(td_params)do
ifparam.requiredandutil.empty(t_args[p_name])then
localis_alias
for_,aliasinipairs(param.aliasesor{})dois_alias=is_aliasornotutil.empty(t_args[alias])end
ifnotis_aliasthen
res['empty-required']=res['empty-required']or{}
res['empty-required'][p_name]=''
end
end
end
returnres
end
-- wraps report in hidden frame
localfunctionwrapReport(report,template_name,options)
ifutil.empty(report)thenreturn''end
localnaked=mw.title.new(template_name)['text']
mw.log(report)
report=(options['wrapper-prefix']or"<div class = 'paramvalidator-wrapper'><span class='paramvalidator-error'>")
..report
..(options['wrapper-suffix']or"</span></div>")
report=mw.ustring.gsub(report,'tname_naked',naked)
report=mw.ustring.gsub(report,'templatename',template_name)
returnreport
end
-- this is the "user" version, called with {{#invoke:}} returns a string, as defined by the options parameter
localfunctionvalidateParams(frame)
-- for purple pages:
ifframe:getParent().args['skip parameters validation']thenreturn'[[ קטגוריה:דפים עם שגיאות פרמטריות שקיבלו חנינה]]'end
localoptions,report,template_name=util.extract_options(frame),'',frame:getParent():getTitle()
localignore=function(p_name)
for_,patterninipairs(options['ignore']or{})do
ifmw.ustring.match(p_name,'^'..pattern..'$')thenreturntrueend
end
returnfalse
end
localreplace_macros=function(s,param_names)
localfunctionconcat_and_escape(t)
locals=table.concat(t,', ')
return(mw.ustring.gsub(s,'%%','%%%%'))
end
ifsand(type(param_names)=='table')then
localk_ar,kv_ar={},{}
fork,vinpairs(param_names)do
table.insert(k_ar,k)
table.insert(kv_ar,k..': '..v)
end
s=mw.ustring.gsub(s,'paramname',concat_and_escape(k_ar))
s=mw.ustring.gsub(s,'paramandvalue',concat_and_escape(kv_ar))
end
returns
end
localreport_params=function(key,param_names)
localres=replace_macros(options[key],param_names)
report=report..(resor'')
returnres
end
-- no option no work.
ifutil.table_empty(options)thenreturn''end
-- get the errors.
localviolations=calculateViolations(frame,options['doc-subpage'])
-- special request of bora: use skip_empty_numeric
ifviolations['empty-undeclared-numeric']then
fori=1,tonumber(options['skip-empty-numeric'])or0do
violations['empty-undeclared-numeric'][i]=nil
end
end
-- handle ignore list, and prune empty violations - in that order!
localoffenders=0
forname,tabinpairs(violations)do
-- remove ignored parameters from all violations
forpnameinpairs(tab)doifignore(pname)thentab[pname]=nilendend
-- prune empty violations
ifutil.table_empty(tab)thenviolations[name]=nilend
-- WORK IS DONE. report the errors.
-- if report then count it.
ifviolations[name]andreport_params(name,tab)thenoffenders=offenders+1end
end
ifoffenders>1thenreport_params('multiple')end
ifoffenders~=0thenreport_params('any')end-- could have tested for empty( report ), but since we count them anyway...
returnwrapReport(report,template_name,options)
end
return{
['validateparams']=validateParams,
['calculateViolations']=calculateViolations,
['wrapReport']=wrapReport
}