Module:Autochangecat

-- Automatically changes categories/templates after xx days -- based on the last date the page was saved/edited -- use in template or directly -- Syntax = -- Syntax = 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("--") 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 = "" 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 --