יחידה:ParamValidator: הבדלים בין גרסאות בדף

תוכן שנמחק תוכן שנוסף
החלפת המנוע. הוספת פונקציה calculateViolations( frame, subpages ) שאפשר לקרוא לה ממודול אחר.
תיעוד
שורה 5:
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.
it exports two functions: validateParams(), called from page using {{#invoke: }} and returns a string,
param1, param2 etc. are either the names of parameter passed to the template, or defined in templatedata.
and calculateViolations(), called from another module, and returns a table with a list of violations, keyed on violation type as described below.
value1, value2 etc. are the values passed to the template, or an empty string if no such parameter was passed.
both use the metadata in the templatedata object to validate the parameters. the list of detected violations appear below:
 
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
שורה 19 ⟵ 28:
* "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
 
calculateViolations() returns a table, keyed on violation type, whose values are tables of parameter name : parameter value
* "any": at least one of the above properies has non-empty value, and the corresponding error actually exists.
* "multiple": more than one of the above properies (not including "any") has non-empty value, and the corresponding error actually exists.
 
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:
it retrieves the template's templatedata, and uses it to validate the parameters passed to the template.
if any violations found, it returns am appropriate string. this string will typically include some maintenance category, and/or error message.
the operation of the module is controlled by a single parameter passed to it. this string should be valid JSON describing the actions.
the fields of this structure has the following keys, with string or null value:
if any of these error-conditions exists, and "options" has non-empty value for the corresponding key, will be appended to the output,
after replacing the follwing tokens:
-- "templatename" is replaced with the actual template name,
-- "tname_naked" is replaced with the naked template name, i.e. without colon and namespace
-- "paramname" is replaced by a comma-separated list of parameters offending the rule.
-- "paramandvalue" is replaced by comma-separated list of "name: value" pairs of parameters and values
 
<includeonly>{{#invoke:ParamValidatoe | validateParams | options = {{PV Defaultdefault Valuesoptions}} }}</includeonly>
if the value of some keys is null, this error condition will be ignored, and not counted when calculating "any" and "multiple" conditions.
 
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 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" is replaced with the naked : template name, i.e. without colon and namespace.
-- * "paramname": is replaced by a comma-separated list of parameters offending the rule.
-- * "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:
* "ignore": list (in square brackets) of parameter names to ignore, both in templatedata and actual parameters. all names should be quoted.
* "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 specialseparate template, and retrieved for the module-use likeas so:shown above.
<includeonly>{{#invoke:ParamValidatoe | validateParams | options = {{PV Default Values}} }}</includeonly>
<includeonly>{{#invoke:ParamValidatoe | validateParams | options = {{PV default options}} | options1 = {"key":"value"} }}</includeonly>
this code is to be placed at the very beginning of the page of the template whose uses are to be validated.
"key" can override any of the options fields described above.
 
several such options pages may exist, so the user can decide which validation regime to use with each template, by invoking it and passing
the appropriate one for the "options" parameter.
 
]=]
 
function empty( s )
return not s or type( s ) == 'string' and mw.text.trim( s ) == ''
end
 
שורה 163 ⟵ 197:
local ignore = function( p_name )
for _, pattern in ipairs( options['ignore'] or {} ) do
if stringmw.ustring.match( p_name, '^' .. pattern .. '$' ) then return true end
end
return false