Jump to content

Module:User:Mxn/Archive timeline

From Wikipedia, the free encyclopedia
local p = {}
local m = require("Module:Math") -- [[Module:Math]]

function p.timeline(frame)
    local firstYear = tonumber(frame.args.first)
    local thisYear = tonumber(os.date("%Y"))
    local lastYear = tonumber(frame.args.last or thisYear - 1)
    local title = mw.title.getCurrentTitle()
    
    local years = {}
    local sizes = {}
    local maxSize = 0
    for year = firstYear, lastYear do
        local subTitle = mw.title.new(mw.ustring.format("%s:%s/%i",
            title.nsText, title.baseText, year))
        if subTitle.exists then
            table.insert(years, year)
            local size = m._round(tonumber(frame:callParserFunction(
                "PAGESIZE", subTitle.fullText, "R")) / 1024, 0)
            sizes[year] = size
            if size > maxSize then maxSize = size end
        end
    end
    
    -- Main talk page
    table.insert(years, lastYear + 1)
    local size = m._round(tonumber(frame:callParserFunction(
        "PAGESIZE", title.nsText .. ":" .. title.baseText, "R")) / 1024, 0)
    sizes[lastYear + 1] = size
    if size > maxSize then maxSize = size end
    
    local barData = {}
    for i, year in ipairs(years) do
        table.insert(barData, "  bar:" .. year)
    end
    
    local plotData = {}
    for i, year in ipairs(years) do
        local bar = mw.ustring.format("  bar:%i from:0 till:%i", year, sizes[year])
        if tonumber(title.subpageText) == year or
            (year == lastYear + 1 and not title.isSubpage) then
            bar = bar .. " color:nhanmanh"
        end
        table.insert(plotData, bar)
    end
    
    local labels = {}
    for i, year in ipairs(years) do
        local linkText = tostring(year)
        local link = title.nsText .. ":" .. title.baseText
        if year ~= lastYear + 1 then
            link = link .. "/" .. year
        elseif lastYear ~= thisYear - 1 then 
            linkText = linkText .. "+"
        end
        local label = mw.ustring.format("  bar:%i at:0 text:\"[[%s|%s]]\"",
            year, link, linkText)
        if tonumber(title.subpageText) == year or
            (year == lastYear + 1 and not title.isSubpage) then
            label = label .. " fontsize:M"
        end
        table.insert(labels, label)
    end
    
    return frame:expandTemplate{
        title = "User:Mxn/Archive timeline",
        args = {
            MaxPeriod = maxSize,
            BarData = table.concat(barData, "\n"),
            PlotData = table.concat(plotData, "\n"),
            Labels = table.concat(labels, "\n"),
        },
    }
end

return p