模組:Article count by project and class
外观
require("strict")
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local class_data = mw.loadData('Module:Class/data')
--- Return the number of pages in a given category.
--- Uses the Mediawiki parser function `{{PAGESINCATEGORY}}` to avoid
--- S/T Chinese counting issues present in mw.site.stats.pagesInCategory.
---
--- @param category string @Category name to count (without the "Category:" namespace).
--- @return number @Number of pages in the category.
local function pages_in_category(category)
local frame = mw.getCurrentFrame()
local result = frame:callParserFunction('PAGESINCATEGORY', { category, 'pages', 'R' })
return result
end
--- Get the class display name from a class code using Module:Class/data.
--- If no match is found, returns the original input unchanged.
---
--- @param __code string @Class code (case-insensitive), e.g., "fa", "ga".
--- @return string @Corresponding class name, or the original code if not found.
local function get_class_name(__code)
local code = string.lower(__code)
for _, v in pairs(class_data) do
if code == v.code then
return v.name
end
end
return __code -- If not found, return as-is
end
--- Format a number with thousands separators.
---
--- @param num number @The number to format.
--- @return string @The formatted number.
local function format_number(num)
return mw.language.new('en'):formatNum(num)
end
local p = {}
--- Entry point for template invocation.
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
--- Core implementation of `p.main`.
function p._main(args)
local category_pattern = "%s级" .. (args.project or '') .. '条目'
local total = 0
for _, v in ipairs(args) do
local category = mw.ustring.format(category_pattern, get_class_name(v))
total = total + pages_in_category(category)
end
return yesno(args.formatnum) and format_number(total) or tostring(total)
end
return p