Module:Wikimedia engineering project date categories

Module documentation

Module to output Category:WMF Projects subcategories as appropriate for a start and end date, to be used in {{Wikimedia engineering project information }}.

UsageUsage

{{#invoke:Wikimedia engineering project date categories|datesToCategories|startdate=date|enddate=date}}

-- Module to output [[:Category:WMF Projects]] subcategories
-- as appropriate for a start and end date, to be used in
-- {{Wikimedia engineering project information}}

local yn = require("Module:Yesno")

local p = {}
local lang = mw.language.getContentLanguage()

local function makeCategory( catdate, trail )
    local quarter, year
    quarter = math.ceil( tonumber( lang:formatDate( "m", catdate ) ) / 3 )
    year = lang:formatDate( "Y", catdate )
    return "[[Category:WMF Projects " .. year .. "q" .. tostring( quarter ) .. trail .. "]]"
end

function p.datesToCategories( frame )
	return p._datesToCategories( frame.args, frame );
end

function p._datesToCategories( args, frame )
	frame = frame or mw.getCurrentFrame();

    local startdate, curdate, enddate
    local categories = ""
	local trail = frame:callParserFunction("#translation:")

	local nocat = yn(args.nocat)
	if (nocat == nil) then
		local title = mw.title.getCurrentTitle()
		nocat = (title and title.subpageText and mw.language.isSupportedLanguage(title.subpageText) and not (title.subpageText == mw.language.getContentLanguage():getCode())) or false
	end

    if (not args.startdate) or args.startdate == "" then
        return nocat and "" or "[[Category:WMF Projects missing start date" .. trail .. "]]"
    else
        curdate = args.startdate
        if string.len(curdate) < 4 then
        	return nocat and "" or "[[Category:WMF Projects missing start date" .. trail .. "]]"
    	end
        if string.len(curdate) == 4 then
        	curdate = curdate .. "-01"
    	end
    end

	local now = lang:formatDate("U")
    if (not args.enddate) or args.enddate == "" then
        -- Ends now
        enddate = now
    else
        enddate = args.enddate
        if string.len(enddate) == 4 then
        	enddate = enddate .. "-12"
        end
		enddate = lang:formatDate("U", enddate)
    end
    if enddate >= now then
    	 -- Current project 
    	 -- In unusual circumstances like [[Extension:Chart/Project]] a project can have a known future end date
    	 -- treat that the same way as a current end date for categorization
        categories = categories .. "[[Category:WMF Projects" .. trail .. "]]"
        enddate = now
    end

	if nocat then
		-- Don't populate WMF quarter categories with translations of every page
		return categories
	end

	trail = ""

    while lang:formatDate( "U", curdate ) < enddate do
        categories = categories .. makeCategory( curdate, trail )
        curdate = lang:formatDate( "Y-m-d", curdate .. "+3 months" )
    end
    categories = categories .. makeCategory( "@"..enddate, trail )
    return categories
end

return p
Category:Modules Category:Pages using the JsonConfig extension