Module:RecursiveSelectiveList
Documentation for this module may be created at Module:RecursiveSelectiveList/doc
local p = {}
local mw = require('mw')
local json = require('json') -- Ensure the JSON library is available
function p.main(frame)
-- Retrieve parameters from the template invocation
local args = frame.args
local categoryName = args.categoryName
-- Check if 'categoryName' is provided
if not categoryName or categoryName == "" then
return "Error: 'categoryName' parameter is missing or empty."
end
-- Ensure the category name starts with 'Category:'
if not categoryName:match("^Category:") then
categoryName = "Category:" .. categoryName
end
-- Prepare the API request parameters
local params = {
action = "query",
list = "categorymembers",
cmtitle = categoryName,
cmlimit = "max",
format = "json"
}
-- Make the API request
local result = mw.site.api(params)
if not result then
return "Error: Failed to retrieve data from the API."
end
-- Parse the JSON response
local data = json.decode(result)
if not data or not data.query or not data.query.categorymembers then
return "Error: Invalid API response."
end
-- Extract page titles from the response
local pages = {}
for _, page in ipairs(data.query.categorymembers) do
table.insert(pages, page.title)
end
-- Handle cases where the category has no members
if #pages == 0 then
return "The category '" .. categoryName .. "' has no pages."
end
-- Return the list of page titles as a comma-separated string
return table.concat(pages, ", ")
end
return p