FANDOM


-- //Contains many miscellaneous functions
p = {}
 
-- Gets Parent/child frame 
function p.getframe(bParent)
    local oFrame  = mw.getCurrentFrame()
    if bParent  and oFrame~=nil and oFrame:getParent() then 
      oFrame = mw.getCurrentFrame():getParent()
    end 
    return oFrame
end
 
-- Put all arguments into a real table - Returns Parent, child args table
function p.getArgs(frame)
    local tParent = frame:getParent()
    local tChild = p.fetchArgs(frame.args)
    if frame  and tParent then
        tParent = p.fetchArgs(tParent.args)
    end
    return tParent or tChild
end
 
function p.fetchArgs(tTmp)
    if not(tTmp) then 
        return 
    end
    for name, value in  pairs( tTmp ) do
        if(name) then
            return tTmp
        end
    end
end
 
--Check if args are enough for script to run
function p.checkArgs(frame, iArgs, bParent)
    local iCount =0
    local  tParent, tChild = p.getArgs(frame)
    local tArgTable = tParent
    if not(bParent) then
        tArgTable = tChild
    end
    for name, value in  pairs(tArgTable) do 
        iCount = iCount + 1
        if (iCount) == tonumber(iArgs) then  
          return true      
        end      
    end
    return false
end
 
-- add days to date
function p.addDays(sDate,iDays,bReturnTable, sFormat)
    if (iDays and tonumber(iDays)) then
        iDayToSecs = iDays  * 24 * 60 *60
        if (bReturnTable) then
            return os.date("*t",p.getdate(sDate,sFormat) + iDayToSecs)
        end
        return os.date("%Y-%m-%d",p.getdate(sDate,sFormat) + iDayToSecs)
    end
    return sDate
end
--Check if date is in appropriate format --Iso YYYY-MM-DD
 
function p.checkdate(sDate,sPat)
    local sPat=sPat or"(%d+)-(%d+)-(%d+)"
    local year,month,day=sDate:match(sPat)
    if year and month and day then 
        return true
    end
    return false
end
 
-- Check if table is empty
 
function p.isempty(table)
    if next(table) == nil then
        return true
    end
    return false
end
 
-- round a number
 
function p.round(num, idp)
    return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
 
-- Calculate difference between two dates
function p.datediff( sDate1, sDate2)
    if p.checkdate(sDate1) and p.checkdate(sDate2) then
        sDate1 = string.sub(sDate1,0,10)
        sDate2 = string.sub(sDate2,0,10)
        local tDate1 =p.getdate(sDate1)
        local tDate2 =p.getdate(sDate2)
        local iDays = math.abs((os.difftime(tDate1,tDate2))/3600/24)
 
        return iDays
    end
    return 0
end
 
--Get time using custom format
function p.getdate(sDate)
    if not(sDate) then 
        sDate = os.date("%Y-%m-%d") 
    end
 
    local sPat="(%d%d%d%d)-(%d%d)-(%d%d)"
    local year,month,day=sDate:match(sPat)
    local tDate 
 
    if year and month and day then
       tDate = os.time({month=month,year=year,day=day}) 
    end
 
    return tDate
end
 
--
function p.table_print (tt, indent, done)
    done = done or {}
    indent = indent or 0
    if type(tt) == "table" then
        local sb = {}
        for key, value in pairs (tt) do
            table.insert(sb, string.rep (" ", indent)) -- indent it
            if type (value) == "table" and not done [value] then
                done [value] = true
                table.insert(sb, '["'..key..'"]'.."={\n");
                table.insert(sb, p.table_print (value, indent + 2, done))
                table.insert(sb, string.rep (" ", indent)) -- indent it
                table.insert(sb, "},\n");
            elseif "number" == type(key) then
                table.insert(sb, string.format("\"%s\"\n", tostring(value))..",")
            else
                table.insert(sb, string.format(
                "%s = \"%s\",\n", tostring ('["'..key..'"]'), tostring(value)) )
            end
        end
        return table.concat(sb)
    else
        return tt
    end
end
 
function p.debug(oFunc,bShowErr)
     local bStatus,bResults =xpcall(oFunc, debug.traceback)
     if (not(bShowErr) or bStatus) then
        return bResults
     end
end
 
function p.log(oData)
    local sOutput = oData
    if type(oData)=="table" then
        sOutput = "table ={\n" .. p.table_print(oData,4) .. "}"
    end
    mw.log(sOutput)
    return sOutput
end
 
return p
-- [[Category:Lua modules]]

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.