FANDOM


--<pre>Automatically changes categories/templates after xx days 
-- based on the last date the page was saved/edited
-- use in template or directly
-- Syntax = {{#invoke:Autochangecat|main|days=2|category1=Dev|category2=Developers|template=true}}
-- Syntax = {{#invoke:Autochangecat|main|date=2015-05-06|category1=Dev|category2=Developers|template=true}}
local p = {}
local u = require("Dev:Utility")
 
 
function p.main(frame)
    local oArgTable,oChildArgs = u.getArgs(frame,"both")
 
    local function getParams(argName)
        if oArgTable and oArgTable[argName] then
            return oArgTable[argName]
        else
            if (oChildArgs and oChildArgs[argName]) then
                return oChildArgs[argName]
            end
        end
    end
    if (oArgTable or oChildArgs) then
        local iDays = getParams("days")
        local endDate = getParams("date")
        local sArg1 = getParams(1) or getParams("page1") 
        local sArg2 = getParams(2) or getParams("page2")
        local bTemplate = getParams("template")
        return p.changeCategory(iDays,sArg1,sArg2,bTemplate,endDate)
    end
end
 
function p.changeCategory(iDays,sArg1,sArg2,bChangeTemplate,endDate)
    local sCat =""
 
    if (iDays or endDate) and sArg1 and sArg2 then
        local revDate = preprocess("{{REVISIONYEAR}}-{{REVISIONMONTH}}-{{REVISIONDAY2}}")
        local iFutureDate = u.addDays (revDate,iDays)
        local todayDate = os.date("%Y-%m-%d")
        local iDaysLeft = u.datediff(iFutureDate,todayDate)
        local sPrefix = "[[".. "Category:"
        local sSuffix = "]]"
        local isValidDate = u.checkdate(endDate)
 
        if isValidDate then
            iDaysLeft = u.datediff(endDate,os.date("%Y-%m-%d"))
        end
 
        if (bChangeTemplate=="true") then
            sPrefix = "{{"
            sSuffix = "}}"
        end
 
        if iDaysLeft then
            if iDaysLeft<1 then
                sCat = preprocess(sPrefix .. sArg2 .. sSuffix)
            else
                sCat = preprocess(sPrefix .. sArg1 .. sSuffix)
            end
        end
    end
    return sCat
end
 
 
 
function preprocess(sText)
    if mw.getCurrentFrame() then
        sText = mw.getCurrentFrame():preprocess(sText)
    end
 
    return sText
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.