Modul:Homokozó/Pepo41/ChuckISBN

A Wikipédiából, a szabad enciklopédiából

Homokozó/Pepo41/ChuckISBN[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

local args = {} 
local lang = mw.getContentLanguage()

local ou = ''
function pop(mi,mv1,mv2,mv3,mv4,mv5)  -- idézőjeles rész és 5 bővítmény az ou számára
    	if not mi then return else ou = ou..mi..'\n ' end
      if not mv1 then return else ou = ou..mv1..'\n' end
	if not mv2 then return else ou = ou..mv2..'\n' end
	if not mv3 then return else ou = ou..mv3..'\n' end
	if not mv4 then return else ou = ou..mv4..'\n' end
	if not mv5 then return else ou = ou..mv5..'\n' end
end

 
local function trim(str)
    if type( str ) ~= "string" or str == nil then 
    return ''
    else return (str:gsub("^%s*(.-)%s*$", "%1"))
    end        -- extra brackets are necessary because gsub returns multiple values
end

function val_to_str(v) 
    if type(v) == 'string' then 
        v = mw.ustring.gsub(v, '\n', '\\n')
        if mw.ustring.match(mw.ustring.gsub(v, '[^\'"]', ''), '^"+$') then
            return "'" .. v .. "'"
        end
        return '"' .. mw.ustring.gsub(v, '"', '\\"' ) .. '"'
    else 
        return type(v) == 'table' and table_to_str(v) or tostring(v)
    end
end 

hibavan = false  
kategorizalas_van = false  

 function error(kategorizalas_kell,about )
    -- Enclose errorMsg with <span>
    -- Precondition:
    --     about  -- string
    hibavan = true
    if kategorizalas_kell then kategorizalas_van = true end  
    local r = about
    if type( about ) == "string" then
        if #about == 0 then
            r = "Error in Lua"
        end
    else
        r = tostring( about )
    end
    return  "<small><font color=\"red\">" ..' ('.. r..') ' .. "</font></small> "
end -- error()



local function getArg(le,arg,alternativarg, assign ) 
    -- Retrieve template argument
    -- Precondition:
    -- 	    le true értékkel jelzi, hogy levágható a paraméter végéről a felesleges vessző és pont
    --     arg     -- string or number; argument identifier
    --     alternativarg -- a paraméternek a másik neve
    --     assign  -- any, optional; default value
    
    local r1 = args[ arg ] 
    local r2 = nil
    if alternativarg ~= nil then  r2 = args[ alternativarg ] end
    if r1 == '' or r1 ==nil then r =  r2  else r = r1 end
    if r  == '' then r =  nil  end
    if type( r ) ~= "string" then
        if type( assign ) == nil then
            r = "{{{<" .. arg .. ">}}}"  --ha r nem string tipus, azaz és assign nil érték
        else
            r = assign -- ha r nem string tipus, és assign nem nil
        end
    else -- r típusa string
    	if #r > 1 then -- ha r strint típus és legalább két karaktert tartalmaz
    		local utso_karakter = string.sub(r,-1,-1); 
    		if le then 
    			if utso_karakter == '.' or utso_karakter == ',' then r=string.sub(r,1,-2) end --lecsípjük az utólsó pontot, vagy vesszőt 
    		end
    	end
    end
    return r  
end

local function getConf(conf,alternativconf, assign ) -- itt eredetileg a frame nem paraméter
    -- Retrieve template argument
    -- Precondition:
    -- 	    le true értékkel jelzi, hogy levágható a paraméter végéről a felesleges vessző és pont
    --     arg     -- string or number; argument identifier
    --     alternativarg -- a paraméternek a másik neve
    --     assign  -- any, optional; default value
    
    local r1 = config[ conf ]
    local r2 = nil
    if alternativconf ~= nil then  r2 = config[ alternativconf ] end
    if r1 == '' or r1 ==nil then r =  r2  else r = r1 end
    if r  == '' then r =  nil  end
    if type( r ) ~= "string" then
        if type( assign ) == nil then
            r = "{{{<" .. conf .. ">}}}"  --ha r nem string tipus, azaz és assign nil érték
        else
            r = assign -- ha r nem string tipus, és assign nem nil
        end
    else -- r típusa string
    	
    end
    return r  
end

local function isISBN10( str)
    local v,chsum,chnum,chd,i,j,chnumdo,eloirt_hossz
    v=''
    for w in string.gmatch(str, "%d") do -- a decimális jegyeken kívül mindent figyelmen kívül hagyunk
         v=v..w
    end    
    pop("10",v)
    chd =  string.sub (str,-1,-1); -- check digit az utólsó
    pop(chd)
    if chd == 'X' or tonumber(chd) ~= nil then
        if chd == 'X' then chnum = 10; eloirt_hossz=9 else chnum = string.byte(chd,1) - string.byte('0',1);eloirt_hossz=10 end 
      else
      return false
    end
    pop(chd,chnum)
 
    if #v ~= eloirt_hossz then return false end -- kötőjelek nélkül a hossz 10 kell,hogy legyen
    v=string.sub(v,1,9) -- 
 
    pop(v, chd)
 
    pop(chnum)
    chsum = 0
    for j = 10,2,-1 do   
      local elsbyte = string.byte(v,(11 - j))
      chsum = chsum + (elsbyte - string.byte('0',1)) * (11-j)
    end -- j ciklus
    pop(chsum)
    if chsum % 11 == 0 then chnumdo=0 else chnumdo = (chsum % 11) end  
    pop(chsum%11)
    pop(chnumdo)
    if chnumdo == chnum then return true else return false end
 
 end -- isISBN10
 


local function isISBN13( str)
    local v,chsum,chnum,chd,i,j,chnumdo,eloirt_hossz
    v=''
    for w in string.gmatch(str, "%d") do -- a decimális jegyeken kívül mindent figyelmen kívül hagyunk
         v=v..w
    end    
    pop("v_1",v)
    chd =  string.sub (str,-1,-1); -- check digit az utólsó
    pop("chd_2",chd)
    if tonumber(chd) ~= nil then
         chnum = string.byte(chd,1) - string.byte('0',1) 
      else
      return false
    end
    pop("chd_3, chnum_1",chd,chnum)
 
    if #v ~= 13 then return false end -- kötőjelek nélkül a hossz 10 kell,hogy legyen
    v=string.sub(v,1,12) -- 
 
    pop(v, chd)
 
    local elsbyte
    chsum = 0
    for j = 1,12 do   -- z_13 = (10 - Summa (i=1,12) (z_i*(3^((i+1)%2))%10) %10
      elsbyte = string.byte(v,j);
      --pop(elsbyte)
      chsum = chsum + (elsbyte - string.byte('0',1)) * 3^((j+1)%2)
      --pop(3^((j+1)%2))
    end -- j ciklus
    pop(chsum)
    chnumdo= chsum%10
    chnumdo=(10-chnumdo) %10
    pop(chnumdo)
    if chnumdo == chnum then pop("true");return true else pop("true"); return false end
 
 end -- isISBN13


local function isI(str)
    local v, eloirt_hossz, i,j, str1, str2, vizsgalandok, mindketto, elso, masodik
    v=''
    for w in string.gmatch(str, "[%dX]") do -- a decimális jegyeken kívül mindent figyelmen kívül hagyunk
         v=v..w
    end   
    pop(v) 
    eloirt_hossz=10 
    pop("előírt hossza",eloirt_hossz)
    if #v == eloirt_hossz then 
  pop("tizes");return isISBN10(str)
    elseif #v > eloirt_hossz then 
  pop("tizenhármas");return isISBN13(str)
    elseif #v < eloirt_hossz then return false 
    end
end   --function isI(str)      


local function isISBN( str)
    local  i,j
    local vizsgalandok={}
    str=string.gsub(str,'[%a<>%[%]%-%(%)%/%=%"]','') 
    str=string.gsub(str,"ó",'')
    pop("str a ballaszt nélkül",str)
    str=string.gsub(str,"%:",',')
    pop("str: a kettőspont cserélve",str)
     for w in string.gmatch(str, "[^%,]+") do
       w=trim(w)
       pop("w",w.."\n")  
       
       table.insert(vizsgalandok,w);
     end
    local s=''
    local h='' 
    local helyes=true
    for i=1,#vizsgalandok do
      	if isI(vizsgalandok[i]) then s=s..' ISBN '..vizsgalandok[i]..','
      		pop("helyes előrehaladása",s)
  	 	else helyes=false;h=h..' '..error(true,"helytelen ISBN kód")..':'..vizsgalandok[i]..','
  	 		pop("hibás előrehaladása",h)
  		end 
    end
    return helyes, string.sub(s,1,-2) , string.sub(h,1,-2)
end -- isISBN   

    
local function _checkISBN(frame)
	local jo_lista, mind
	local alkategoriak = {}
	local isbn=nil  
	local hiba_jelzes = ''
	isbn = getArg(true,1)
	milyen_lista = getArg(true,2)
	
	
	pop("\na kapott argumentum", isbn)
   -- ISBN lekezelése
   if isbn then 
	    pop("van isbn paraméter", isbn)
	    --isbn=string.gsub(isbn,"ISBN",'') -- a beleírt ISBN szövegeket kivesszük
	    local helyes=true
	    local isbn_str, hiba_str=''
	    helyes,isbn_str, hiba_str= isISBN(isbn) 
		  isbn = isbn_str
	      hiba_jelzes=hiba_jelzes..hiba_str
 		  
	    if helyes then pop ("helyesek  a paraméterek") end                       
	end        -- ISBN lekezelése
   
	if milyen_lista then 
		mind=false
	    if milyen_lista == 'jólista' then jo_lista = true;  else jo_lista = false;  end
	else mind=true 
	end
 -- output összeállítása
 --  local siteserver= mw.site.server:match( "[^/]*$" ) --> hu.wikipedia.org
	s=''
	if isbn then
		if hiba_jelzes then
			if mind then s=isbn..' '  
				if hibavan then s = s ..' '..hiba_jelzes..' ' 
			-- a fölérendelt kategória marad hiba esetén
					if kategorizalas_van then s = s .. ' '.. "[[Kategória:Lapok helytelen ISBN kóddal]]" end
				end
			else  
				if jo_lista then  s = isbn..' '  
				else 
					if hibavan then s = hiba_jelzes..' ' 
			        -- a fölérendelt kategória marad hiba esetén
						if kategorizalas_van then s = s .. ' '.. "[[Kategória:Lapok helytelen ISBN kóddal]]" end
					end 
		        end
			end
		end	
	end
	--if ou  then s = s..ou  end  --ez csak nyomkövetésnél kell, de akkor nagyon
	return s 
 end

 
function run(frame)
	local pframe = frame:getParent()
    args = pframe.args -- azok a paraméterek, amelyek a sablont kisérik a Pepo41/CCitPer               	--sablon hivása oldalon
    config = frame.args -- az #invoke utasításban átadott paraméterek 
 return _checkISBN(frame)
end

local p = {
    run = run, -- az egyetlen átadadndó funkció a táblából
    isISBN = isISBN
}

return p