הבדלים בין גרסאות בדף "יחידה:ParamValidator"
קפיצה לניווט
קפיצה לחיפוש
מ (גרסה אחת יובאה: ייבוא מויקיפדיה. ראה ברשימת התורמים בגרסאות קודמות שם. משוחרר ברישיון זהה לרישיון שם) |
תגית: שוחזרה |
||
שורה 113: | שורה 113: | ||
end | end | ||
, | , | ||
extract_options = function ( frame, optionsPrefix ) | extract_options = function( frame, optionsPrefix ) | ||
optionsPrefix = optionsPrefix or 'options' | optionsPrefix = optionsPrefix or 'options' | ||
שורה 180: | שורה 180: | ||
-- 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: | שורה 203: | ||
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: | שורה 244: | ||
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: | שורה 255: | ||
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: | שורה 272: | ||
-- 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: | שורה 287: | ||
-- 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: | שורה 301: | ||
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, '%%', '%%%%' ) ) |