Jump to content

Module:WikiContestTracker

ⵙⴳ ⵡⵉⴽⵉⴱⵉⴷⵢⴰ
-- Module:WikiContestTracker
local p = {}

-- This function returns a hex color code for the progress bar based on the percentage.
local function getProgressBarColor(percent)
    if percent <= 0 then return 'E82F37'     -- Red
    elseif percent <= 10 then return 'F05F2A'
    elseif percent <= 20 then return 'F3901B'
    elseif percent <= 30 then return 'F8C10E'
    elseif percent <= 40 then return 'FDF100' -- Yellow
    elseif percent <= 50 then return 'D7E40E'
    elseif percent <= 60 then return 'ADDA1F'
    elseif percent <= 70 then return '7FCC28'
    elseif percent <= 80 then return '56C137'
    elseif percent <= 90 then return '2EB445'
    else return '07AA59'                      -- Green
    end
end

function p.main(frame)
    local output = {}
    local args = frame.args
    
    -- Table Header
    table.insert(output, '{| class="wikitable sortable" style="width: 90%; margin:auto; font-size:95%; text-align:center;"\n')
    table.insert(output, '! style="width:10px;" | #\n')
    table.insert(output, '! | ⴰⵎⴰⴳⵔⴰⴷ\n')
    table.insert(output, '! | ⴰⵙⴰⴳⵎ\n')
    table.insert(output, '! style="width:150px;" | ⵜⵉⴷⴷⵉ ⵜⴰⵙⴰⵍⴰⵏⵜ\n')
    table.insert(output, '! style="width:150px;" | ⵜⵉⴷⴷⵉ (ⵜⴰⵡⵜⵜⴰⵙⵜ / ⵜⴰⵎⵉⵔⴰⵏⵜ)\n')
    table.insert(output, '! style="width:125px;" | ⴰⵏⵙⵙⵎⵔⵙ\n')
    table.insert(output, '! style="width:155px;" | ⴰⵍⴰⵢ\n')
    table.insert(output, '! style="width:25px;" | ⴰⴷⴷⴰⴷ\n')
    
    -- Process each row
    local i = 1
    -- The parameter for the row number is 'num_i' (e.g., num_1, num_2)
    while args['ⵓⵟⵟⵓⵏ_' .. i] do
        -- The parameter for the article name is 'article_i' (e.g., article_1, article_2)
        local article = args['ⴰⵎⴰⴳⵔⴰⴷ_' .. i] or ''
        
        if article ~= '' then
            -- The parameter for the original size is 'original_size_i'
            local originalSize = tonumber(args['ⵜⵉⴷⴷⵉ_ⵜⴰⵙⴰⵍⴰⵏⵜ_' .. i]) or 0
            -- The parameter for the target size is 'target_size_i'
            local targetSize = tonumber(args['ⵜⵉⴷⴷⵉ_ⵜⴰⵡⵜⵜⴰⵙⵜ' .. i])
            local sizeRequirement = 4000  -- The default size increase required
            
            -- Allow a custom size requirement per article if provided using 'size_requirement_i'
            local customRequirement = tonumber(args['ⵜⴰⴼⴰⴷⴰ_ⵏ_ⵜⵉⴷⴷⵉ_' .. i])
            if customRequirement then
                sizeRequirement = customRequirement
            end

            -- If no target size is specified, calculate it based on the original size and requirement
            if not targetSize then
                targetSize = originalSize + sizeRequirement
            end

            -- Get the current size of the article page in raw bytes
            local currentSize = tonumber(mw.getCurrentFrame():preprocess('{{PAGESIZE:' .. article .. '|R}}')) or 0
            local percent = (targetSize > 0) and (currentSize / targetSize) * 100 or 0

            table.insert(output, '|-\n')
            -- Parameters: num_i, article_i, source_i, user_i
            table.insert(output, '| ' .. (args['ⵓⵟⵟⵓⵏ_' .. i] or '') .. '\n')
            table.insert(output, '| [[' .. article .. ']]\n')
            table.insert(output, '| ' .. (args['ⴰⵙⴰⴳⵎ_' .. i] or '') .. '\n')
            table.insert(output, '| ' .. originalSize .. '\n')
            table.insert(output, '| ' .. targetSize .. " / '''" .. currentSize .. "''' bytes" .. '\n')
            
            -- Get the username
            local username = args['ⴰⵏⵙⵙⵎⵔⵙ_' .. i] or ''
            local userCellContent = ''
            
            -- If a username is provided, format it with links to the user page and talk page
            if username:match('%S') then -- Check if username is not empty or just whitespace
                username = username:match('^%s*(.-)%s*$') -- Trim whitespace
                userCellContent = '[[user:' .. username .. '|' .. username .. ']] ([[user talk:' .. username .. '|ⵙⴰⵡⵍ]])'
            end
            table.insert(output, '| ' .. userCellContent .. '\n')
            
            -- Create the HTML for the progress bar
            local barWidth = math.min(100, percent)
            local barColor = getProgressBarColor(percent)
            local progressBarHtml = string.format(
                '<div>%0.1f%%</div>' ..
                '<div style="border:1px solid #AAA; background-color:#F0F0F0; height:10px; width:150px;">' ..
                '<div style="width:%f%%; height:100%%; background-color:#%s;"></div></div>',
                percent, barWidth, barColor
            )
            table.insert(output, '| ' .. progressBarHtml .. '\n')

            -- Determine the status icon based on progress
            local statusIcon
            if currentSize == 0 then
                statusIcon = '[[File:No Cross.svg|20px|link=|Not created yet!]]'
            elseif currentSize >= targetSize then
                statusIcon = '[[File:Green Checkmark Circle.svg|20px|link=|Done!]]'
            else
                statusIcon = '[[File:Writing Circle Yellow.svg|20px|link=|In progress!]]'
            end
            table.insert(output, '| ' .. mw.getCurrentFrame():preprocess(statusIcon) .. '\n')
        end
        
        i = i + 1
    end

    table.insert(output, '|}')
    return table.concat(output)
end

return p