Jump to content

Module:TopPages

From No Subject

Documentation for this module may be created at Module:TopPages/doc

local p = {}

function p.list(frame)
    -- Check if the library exists before calling it to avoid the "nil" error
    if not mw.ext.analytics then
        return '<span class="error">Analytics library not found. Check LocalSettings.php</span>'
    end

    local limit = tonumber(frame.args.limit) or 10
    local days = tonumber(frame.args.days) or 7
    
    -- Attempting the call
    local status, data = pcall(function() 
        return mw.ext.analytics.getPageViews{ limit = limit, days = days } 
    end)
    
    if not status or not data then
        return '<span class="error">Failed to fetch data. Data might be empty or function name is different.</span>'
    end
    
    local output = '{| class="wikitable sortable"\n! Page !! Views\n'
    for _, row in ipairs(data) do
        -- row.title might need to be row.page_title in some versions
        local title = row.title or row.page_title or "Unknown"
        local views = row.views or row.view_count or 0
        output = output .. '|-\n| [[' .. title .. ']] || ' .. views .. '\n'
    end
    output = output .. '|}'
    
    return output
end

return p