Modulo:mchkparametrojn: Malsamoj inter versioj

Enhavo forigita Enhavo aldonita
Taylor 49 (diskuto | kontribuoj)
ÅÅ
Taylor 49 (diskuto | kontribuoj)
plibonigo
Linio 3:
MODULE "mchkparametrojn" (check parametrojn)
 
"eo.wiktionary.org/wiki/Modulo:mchkparametrojn" <!--20182019-NovJun-xx27-->
"id.wiktionary.org/wiki/Modul:mchkparametrojn"
 
BenefitPurpose: performs 2 comparisons in order to check validity
of the anonymous parameters submitted the caller, even empty
empty parameters docan count as valid with the "E" option, otherwise
they are considered as an error, can check availability
of obligatory named parameters too
 
Utilo: efektivigas 2 komparojn por kontroli validecon
de la anonimaj parametroj transdonitaj al la vokanto, ecx malplenaj
malplenaj parametroj estaspovas esti alsumigataj kun la "E" opcio, alimaniere
ili estas pritraktataj kiel eraro, povas ankaux kontroli disponeblon
de devigaj nomitaj parametroj
 
Manfaat: ... mengontrol parameter dikirim kepada templat ...
 
Used by templates / Uzata far sxablonoj:
Linio 21 ⟶ 26:
- none / neniuj
 
Incoming: - 2 or 3...7 anonymous parameters
- 2 integers with lower limit (must be 1 digit 0...9) and upper
upper limit (must be bigger1 or equal2 ONE,digits bigger or equal
the lower limit, and less thanor 64equal 63)
- optional "E" string to allow empty parameters
- optional 0...4 names of obligatory named parameters
Returned: - string "1" if the parameters are accepted, string "0" if
the parameters supplied to the caller are rejected, or this
Linio 40 ⟶ 46:
calling template must be aware of this.
 
CannotCan theoretically be used to ensure that ZERO parameters have been submitted,
submitted, but this preferably can be done by a simple "#if"-statement:
 
NeTeorie estas uzebla por certigi ke NUL parametroj estis havigitaj,
sed tio preferinde estas farebla per simpla "#if"-ordono:
 
<!--enkonduko#
#protekto-->{{#if:<!--o-->{{{1|}}}<!--o-->|<!--then-->...{{#inv
oke:mwhineplendu|ek}}]<!--o-->|<!--else--><!--protekto#
#kerno-->"Hello world!" from template "contoh"<!--kerno#
#protekto-->}}<!--endif--><!--protekto#
Linio 54 ⟶ 61:
<!--enkonduko#
#protekto-->{{#if:<!--o-->{{{1|}}}<!--o-->|<!--th
en-->&nbsp;-=&nbsp;<!--o-->|<!--else--><!--protekto#
#kerno-->"Hello world!" from template "contoh"<!--kerno#
#protekto-->}}<!--endif--><!--protekto#
Linio 72 ⟶ 79:
 
"{{#invoke:mchkparametrojn|ek|0|1}}"
 
"{{#invoke:mchkparametrojn|ek|0|0}}" (deprecated usage / malpreferinda uzo)
 
"{{#invoke:mchkparametrojn|ek|0|0|nama|umur}}"
 
"<!--enkonduko#
#protekto-->{{#ifeq:<!--o-->{{#invoke:mchkparamet
rojn|ek|1|2}}<!--o-->|<!--equal-->0<!--o-->|<!--ththen-->{{#inv
en-->...<!--o-->oke:mwhineplendu|ek}}]<!--endif-->{{#ifeq:<!--o-->{{#invoke:mchkparamet
rojn|ek|1|2}}<!--o-->|<!--equal-->1<!--o-->|<!--then--><!--protekto#
#kerno-->"Hello world!" from template "contoh"<!--kerno#
Linio 86 ⟶ 97:
local chkparametrojn = {}
 
------------------------------------------------------------------------
local function lfdec1digit (num1digit) -- converts 1 digit to decimal
 
num1digit = num1digit - 48 ; -- may become invalid
---- ORDINARY LOCAL LOW LEVEL FUNCTIONS ----
 
------------------------------------------------------------------------
 
-- Local function LFDEC1DIGIT
 
-- Convert ASCII code of 1 decimal digit to integer
 
local function lfdec1digit (num1digit)
num1digit = num1digit - 48 -- may become invalid
if ((num1digit<0) or (num1digit>9)) then
num1digit = 255 ;
end--if
return num1digit
end--function lfdec1digit
 
------------------------------------------------------------------------
function chkparametrojn.ek (frame)
 
-- Local function LFSTR12DIG2INT
local strtmp = "" ; -- temp for decimal conversion
local strret = "0" ; -- output string preset to "rejected"
local numlong = 0 ; -- temp for decimal conversion (length of parameter)
local numlowl = 255 ; -- lower limit (preset to invalid)
local numuppl = 255 ; -- upper limit (preset to invalid)
local numuppm = 0 ; -- temp for decimal conversion (no need to preset)
local numiter = 0 ; -- index iterating through parameters of the caller
local booemp = false ; -- flag allow empty parameters (default is disallow)
 
-- Convert a strig with 1 or 2 decimal digits to integer
if ((frame.args [1]) and (frame.args [2])) then
 
strtmp = frame.args [1] ; -- lower limit (1 digit)
local function lfstr12dig2int (strmasuk)
numlong = string.len (strtmp) ; -- temp for decimal conversion
local numleen = 0
local numaa = 255 -- preset to "error"
local numbb = 0
numleen = string.len (strmasuk)
if ((numleen==1) or (numleen==2)) then
numaa = string.byte (strmasuk,1,1)
numaa = lfdec1digit (numaa) -- 255 if invalid, ZERO would be valid
end--if
if ((numaa~=255) and (numleen==2)) then
numbb = string.byte (strmasuk,2,2)
numbb = lfdec1digit (numbb) -- 255 if invalid, ZERO would be valid
if (numbb==255) then
numaa = 255 -- 255 is invalid, note that ZERO would be valid
else
numaa = numaa * 10 + numbb -- valid integer number 0...99 now
end--if
end--if
return numaa
end--function lfstr12dig2int
 
------------------------------------------------------------------------
 
---- MAIN EXPORTED FUNCTION ----
 
------------------------------------------------------------------------
 
function chkparametrojn.ek (arxframent)
 
-- general unknown type
 
local vartmp = 0 -- variable without type
 
-- general table
 
local tabnamed = {} -- named params to check
 
-- special type "args" AKA "arx"
 
local arxourown = 0 -- metaized "args" from our own "frame"
local arxcaller = 0 -- metaized "args" from caller's "frame"
 
-- general "str"
 
local strtmp = "" -- temp
local strret = "" -- output string
 
-- general "num"
 
local numlong = 0 -- temp
local numlowl = 0 -- lower limit
local numuppl = 0 -- upper limit
local numiter = 0 -- index iterating through parameters 2 times
local numjuml = 0 -- amount of expected named params
 
-- general "boo"
 
local booerr = false -- fatal error flag
local booemp = false -- flag allow empty parameters (default is disallow)
 
---- SEIZE 2...7 PARAMETERS SENT FROM CALLER TO US ----
 
arxourown = arxframent.args -- "args" from our own "frame"
 
if ((arxourown[1]==nil) or (arxourown[2]==nil) or (arxourown[8])) then
booerr = true -- need 2 obligatory params, 8 are not appreciated
else
strtmp = arxourown[1] -- lower limit (1 digit)
numlong = string.len (strtmp)
if (numlong==1) then
numlowl = string.byte (strtmp,1,1) ;
numlowl = lfdec1digit (numlowl) ; -- 255 if invalid
else
booerr = true
end--if
strtmp = frame.args arxourown[2] ; -- upper limit (1 or 2 digits)
numlongnumuppl = string.lenlfstr12dig2int (strtmp) ; -- temp0...99 or for255 decimalif conversioninvalid
if ((numlong==1numlowl>9) or (numlong==2numuppl<numlowl) or (numuppl>63)) then
numupplbooerr = string.byte (strtmp,1,1)true
numuppl = lfdec1digit (numuppl) ; -- 255 if invalid
end--if
end--if
if (numlong==2) then
 
numuppm = string.byte (strtmp,2,2) ;
if (booerr==false) then
numuppm = lfdec1digit (numuppm) ; -- 255 if invalid
numiter = 3 -- ONE-based
if (numuppm==255) then
numupplnumjuml = 255 ;0 -- 255 is invalid, note that ZERO would be valid/ONE-based
vartmp = arxourown[3] -- risk of "nil"
else
if (type(vartmp)=="string") then
numuppl = numuppl * 10 + numuppm ; -- valid integer number 0...99 now
if (vartmp=="E") then
booemp = true -- flag to allow empty parameters
numiter = 4 -- start searching named params after the "E"
end--if
end--if
while (true) do -- iterate over expected named params via anonymous params
if (frame.args [3]) then
vartmp = arxourown[numiter] -- risk of "nil"
if ((frame.args [3])=="E") then
if (type(vartmp)=="string") then
booemp = true ; -- flag allow empty parameters
if (numjuml==4) then
booerr = true -- 7 params and no "E" this is criminal
break
end--if
numjuml = numjuml + 1 -- ONE-based -- found expected named parameter
tabnamed[numjuml] = vartmp
numiter = numiter + 1 -- ONE-based
else
break -- OK -- "numjuml" has the seized amount 0...4
end--if
end--ifwhile
end--if
 
---- CARRY OUT THE HARD WORK ----
if ((numlowl<10) and (numuppl>=1) and (numuppl>=numlowl) and (numuppl<64)) then
 
while (true) do -- iterate through parameters of the caller
arxcaller = if (mw.getCurrentFrame()arxframent:getParent().args[numiter+1]) then-- "args" from caller's "frame"
 
if ((booemp==false) and ((mw.getCurrentFrame():getParent().args[numiter+1])=="")) then
if (booerr==false) then
numiter = 255 ;
numiter = 1 -- ONE-based
break ; -- empty parameter found, this fatal, "numiter" 255 invalid
while (true) do -- iterate through anonymous parameters of the caller
end--if
vartmp = arxcaller[numiter]
if (vartmp==nil) then
numiter = numiter - 1 -- can be ZERO
break -- no more parameters, "numiter" holds amount of those found
else
if ((booemp==false) and (vartmp=="")) then
break ; -- no more parameters, "numiter" holds amount of those found
booerr = true
break -- empty parameter found, this is fatal
end--if
end--if
numiter = numiter + 1 ;
end--while
if ((numiter>=<numlowl) andor (numiter<=>numuppl)) then
booerr = true
strret = "1" ; -- set output string to report "accepted"
end--if
end--if
 
if (booerr==false) then
return strret
numiter = 1 -- ONE-based
while (true) do -- iterate through expected named parameters of the caller
if (numiter>numjuml) then
break -- done
end--if
strtmp = tabnamed[numiter]
vartmp = arxcaller[strtmp]
if (vartmp==nil) then
booerr = true
break -- NOT found, this is fatal
else
if ((booemp==false) and (vartmp=="")) then
booerr = true
break -- empty parameter found, this is fatal too
end--if
end--if
numiter = numiter + 1
end--while
end--if
 
strret = "1" -- preset output string to report "accepted"
if (booerr) then
strret = "0" -- set output string to report "rejected"
end--if
 
---- RETURN THE JUNK STRING ----
 
return strret -- can be empty
 
end--function
 
---- RETURN THE JUNK LUA TABLE ----
 
return chkparametrojn