Module:Autochangecat
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Autochangecat/doc
--<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")
local function preprocess(sText)
if mw.getCurrentFrame() then
sText = mw.getCurrentFrame():preprocess(sText)
end
return sText
end
function p.main(frame)
local oArgTable, oChildArgs = u.getArgs(frame, "both")
local function getParams(argName)
if oChildArgs and oChildArgs[argName] then
return oChildArgs[argName]
elseif oArgTable and oArgTable[argName] then
return oArgTable[argName]
end
end
if oChildArgs or oArgTable 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
return p
--[[Category:Lua modules]]