Modulo:mchkparametrojn: Malsamoj inter versioj
Enhavo forigita Enhavo aldonita
ÅÅ |
plibonigo |
||
Linio 3:
MODULE "mchkparametrojn" (check parametrojn)
"eo.wiktionary.org/wiki/Modulo:mchkparametrojn" <!--
"id.wiktionary.org/wiki/Modul:mchkparametrojn"
of the anonymous parameters submitted the caller, even empty
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
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
- 2 integers with lower limit (must be 1 digit 0...9) and
upper limit (must be
the lower limit, and less
- 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.
submitted, but this preferably can be done by a simple "#if"-statement:
sed tio preferinde estas farebla per simpla "#if"-ordono:
<!--enkonduko#
#protekto-->{{#if:<!--o-->{{{1|}}}<!--o-->|<!--then-->
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-->
#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-->|<!--
rojn|ek|1|2}}<!--o-->|<!--equal-->1<!--o-->|<!--then--><!--protekto#
#kerno-->"Hello world!" from template "contoh"<!--kerno#
Linio 86 ⟶ 97:
local chkparametrojn = {}
------------------------------------------------------------------------
---- 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
------------------------------------------------------------------------
-- Local function LFSTR12DIG2INT
-- Convert a strig with 1 or 2 decimal digits to integer
local function lfstr12dig2int (strmasuk)
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)
else
booerr = true
end--if
strtmp =
if ((
end--if
end--if
if (booerr==false) then
numiter = 3 -- ONE-based
vartmp = arxourown[3] -- risk of "nil"
if (type(vartmp)=="string") then
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
vartmp = arxourown[numiter] -- risk of "nil"
if (type(vartmp)=="string") then
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--
end--if
---- CARRY OUT THE HARD WORK ----
arxcaller =
if (booerr==false) then
numiter = 1 -- ONE-based
while (true) do -- iterate through anonymous parameters of the caller
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
booerr = true
break -- empty parameter found, this is fatal
end--if
end--if
numiter = numiter + 1
end--while
if ((numiter
booerr = true
end--if
end--if
if (booerr==false) then
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
|