יחידה:ParamValidator – הבדלי גרסאות
יצירה מחדש |
חלוקת קונטרסים (שיחה | תרומות) אין תקציר עריכה |
||
| (2 גרסאות ביניים של 2 משתמשים אינן מוצגות) | |||
| שורה 1: | שורה 1: | ||
require('strict') | |||
--[=[ | --[=[ | ||
This module is based on idea and original code of | 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 | the source of this module is in //he.wikipedia.org/wiki/Module:ParamValidator | ||
| שורה 110: | שורה 111: | ||
local util = { | local util = { | ||
empty = function( s ) | empty = function( s ) | ||
return s == nil or type( s ) == 'string' and mw.text.trim( s ) == '' | return s == nil or type( s ) == 'string' and (mw.text.trim( s ) == '' or s == '-') -- compatible with module:תבנית מידע | ||
end | end | ||
, | , | ||
extract_options = function ( frame, optionsPrefix ) | extract_options = function( frame, optionsPrefix ) | ||
optionsPrefix = optionsPrefix or 'options' | optionsPrefix = optionsPrefix or 'options' | ||
local options, n | local options, n = {} | ||
if frame.args['module_options'] then | if frame.args['module_options'] then | ||
local module_options = mw.loadData( frame.args['module_options'] ) | local module_options = mw.loadData( frame.args['module_options'] ) | ||
| שורה 127: | שורה 128: | ||
repeat | repeat | ||
ok, more = pcall( mw.text.jsonDecode, frame.args[optionsPrefix .. ( n or '' )] ) | local ok, more = pcall( mw.text.jsonDecode, frame.args[optionsPrefix .. ( n or '' )] ) | ||
if ok and type( more ) == 'table' then | if ok and type( more ) == 'table' then | ||
for k, v in pairs( more ) do options[k] = v end | for k, v in pairs( more ) do options[k] = v end | ||
| שורה 148: | שורה 149: | ||
table_empty = function( t ) -- normally, test if next(t) is nil, but for some perverse reason, non-empty tables returned by loadData return nil... | table_empty = function( t ) -- normally, test if next(t) is nil, but for some perverse reason, non-empty tables returned by loadData return nil... | ||
if type( t ) ~= 'table' then return true end | if type( t ) ~= 'table' then return true end | ||
for | for _, _ in pairs( t ) do return false end | ||
return true | return true | ||
end | end | ||
| שורה 180: | שורה 181: | ||
-- this is the function to be called by other modules. it expects the frame, and then an optional list of subpages, e.g. { "Documentation" }. | -- 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. | -- if second parameter is nil, only tempalte page will be searched for templatedata. | ||
function calculateViolations( frame, subpages ) | local function calculateViolations( frame, subpages ) | ||
-- used for parameter type validy test. keyed by TD 'type' string. values are function(val) returning bool. | -- used for parameter type validy test. keyed by TD 'type' string. values are function(val) returning bool. | ||
local type_validators = { | local type_validators = { | ||
['number'] = function( s ) return mw.language.getContentLanguage():parseFormattedNumber( s ) end | ['number'] = function( s ) return mw.language.getContentLanguage():parseFormattedNumber( s ) end | ||
} | } | ||
function compatible( typ, val ) | |||
local function compatible( typ, val ) | |||
local func = type_validators[typ] | local func = type_validators[typ] | ||
return type( func ) ~= 'function' or util.empty( val ) or func( val ) | return type( func ) ~= 'function' or util.empty( val ) or func( val ) | ||
end | |||
local function list_empty_or_contains(ar, searched) | |||
if not ar or #ar == 0 then return true end | |||
for _, val in ipairs(ar) do if val == searched then return true end end | |||
return false | |||
end | end | ||
| שורה 196: | שורה 204: | ||
local td_params = templatedata and templatedata.params | local td_params = templatedata and templatedata.params | ||
local all_aliases, all_series = {}, {} | local all_aliases, all_series = {}, {} | ||
if not td_params then return { ['no-templatedata'] = { [''] = '' } } end | if not td_params then return { ['no-templatedata'] = { [''] = '' } } end | ||
| שורה 236: | שורה 245: | ||
or not compatible( tp_param.type, value ) and 'incompatible' | or not compatible( tp_param.type, value ) and 'incompatible' | ||
or not series and already_seen[tp_param] and hasval and 'duplicate' | or not series and already_seen[tp_param] and hasval and 'duplicate' | ||
or hasval and not list_empty_or_contains(tp_param.suggestedvalues , value) and 'unsuggested-value' | |||
already_seen[tp_param] = hasval | already_seen[tp_param] = hasval | ||
| שורה 245: | שורה 256: | ||
end | end | ||
end | end | ||
-- test for empty/missing paraeters declared "required" | -- test for empty/missing paraeters declared "required" | ||
for p_name, param in pairs( td_params ) do | for p_name, param in pairs( td_params ) do | ||
| שורה 262: | שורה 273: | ||
-- wraps report in hidden frame | -- wraps report in hidden frame | ||
function wrapReport(report, template_name, options) | local function wrapReport(report, template_name, options) | ||
if util.empty( report ) then return '' end | if util.empty( report ) then return '' end | ||
local naked = mw.title.new( template_name )['text'] | local naked = mw.title.new( template_name )['text'] | ||
| שורה 277: | שורה 288: | ||
-- this is the "user" version, called with {{#invoke:}} returns a string, as defined by the options parameter | -- this is the "user" version, called with {{#invoke:}} returns a string, as defined by the options parameter | ||
function validateParams( frame ) | local function validateParams( frame ) | ||
-- for purple pages: | |||
if frame:getParent().args['skip parameters validation'] then return '[[ קטגוריה:דפים עם שגיאות פרמטריות שקיבלו חנינה]]' end | |||
local options, report, template_name = util.extract_options( frame ), '', frame:getParent():getTitle() | local options, report, template_name = util.extract_options( frame ), '', frame:getParent():getTitle() | ||
| שורה 288: | שורה 302: | ||
local replace_macros = function( s, param_names ) | local replace_macros = function( s, param_names ) | ||
function concat_and_escape( t ) | local function concat_and_escape( t ) | ||
local s = table.concat( t, ', ' ) | local s = table.concat( t, ', ' ) | ||
return ( mw.ustring.gsub( s, '%%', '%%%%' ) ) | return ( mw.ustring.gsub( s, '%%', '%%%%' ) ) | ||