Module:RecursiveSelectiveList: Difference between revisions
EnWikiAdmin (talk | contribs) No edit summary Tag: Manual revert |
EnWikiAdmin (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local mw = require('mw') | local mw = require('mw') | ||
local json = require('json') -- Ensure the JSON library is available | |||
function p.main(frame) | function p.main(frame) | ||
-- Retrieve parameters from the template invocation | -- Retrieve parameters from the template invocation | ||
local args = frame.args | local args = frame.args | ||
local | local categoryName = args.categoryName | ||
-- Check if ' | -- Check if 'categoryName' is provided | ||
if not | if not categoryName or categoryName == "" then | ||
return "Error: ' | return "Error: 'categoryName' parameter is missing or empty." | ||
end | end | ||
-- | -- Ensure the category name starts with 'Category:' | ||
local | 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 | -- Handle cases where the category has no members | ||
if | if #pages == 0 then | ||
return " | return "The category '" .. categoryName .. "' has no pages." | ||
end | end | ||
return | -- Return the list of page titles as a comma-separated string | ||
return table.concat(pages, ", ") | |||
end | end | ||
return p | return p |
Revision as of 07:21, 22 December 2024
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