|
|
| Line 1: |
Line 1: |
| local p = {} | | local p = {} |
|
| |
| -- Get total views for a page
| |
| local function getViews(pageName)
| |
| local data = mw.ext.analytics.getViewsData({
| |
| page = pageName
| |
| })
| |
|
| |
| if not data then
| |
| return 0
| |
| end
| |
|
| |
| local total = 0
| |
|
| |
| for _, views in pairs(data) do
| |
| total = total + tonumber(views) or 0
| |
| end
| |
|
| |
| return total
| |
| end
| |
|
| |
| -- Get pages in a category
| |
| local function getCategoryPages(categoryName)
| |
| local category = mw.title.new("Category:" .. categoryName)
| |
|
| |
| if not category then
| |
| return {}
| |
| end
| |
|
| |
| local pages = {}
| |
|
| |
| -- Get category members
| |
| for page in mw.site.categories.getMembers(categoryName) do
| |
| if page.ns == 0 then
| |
| table.insert(pages, page)
| |
| end
| |
| end
| |
|
| |
| return pages
| |
| end
| |
|
| |
|
| function p.main(frame) | | function p.main(frame) |
| | local category = frame.args.category or "Theses" |
|
| |
|
| local args = frame.args | | local count = mw.site.stats.pagesInCategory( |
| | | category, |
| local category = args.category or "Theses"
| | "pages" |
| local limit = tonumber(args.limit) or 10
| | ) |
| | |
| local pages = getCategoryPages(category)
| |
| local results = {}
| |
| | |
| -- Get views for every page
| |
| for _, page in ipairs(pages) do
| |
| | |
| local views = getViews(page.prefixedText) | |
| | |
| table.insert(results, {
| |
| title = page.prefixedText,
| |
| displayTitle = page.subpageText or page.text,
| |
| views = views,
| |
| url = page.fullUrl()
| |
| })
| |
| | |
| end
| |
| | |
| -- Sort highest views first
| |
| table.sort(results, function(a, b)
| |
| return a.views > b.views
| |
| end)
| |
| | |
| -- Limit results
| |
| local output = {}
| |
| | |
| table.insert(output, '<div class="popular-articles">')
| |
| table.insert(output, '<h2>🔥 Popular Articles</h2>')
| |
| table.insert(output, '<ol>')
| |
| | |
| local count = math.min(limit, #results)
| |
| | |
| for i = 1, count do
| |
| | |
| local item = results[i] | |
| | |
| table.insert(
| |
| output,
| |
| string.format(
| |
| '<li><a href="%s">%s</a> <span class="popular-views">(%d views)</span></li>',
| |
| item.url,
| |
| mw.text.encode(item.displayTitle),
| |
| item.views
| |
| )
| |
| )
| |
| | |
| end
| |
| | |
| table.insert(output, '</ol>')
| |
| table.insert(output, '</div>') | |
|
| |
|
| return table.concat(output, "\n") | | return "Pages in Category:" .. category .. " = " .. tostring(count) |
| end | | end |
|
| |
|
| return p | | return p |
Documentation for this module may be created at Module:PopularArticles/doc
local p = {}
function p.main(frame)
local category = frame.args.category or "Theses"
local count = mw.site.stats.pagesInCategory(
category,
"pages"
)
return "Pages in Category:" .. category .. " = " .. tostring(count)
end
return p