Module:RecursiveSelectiveList: Difference between revisions
EnWikiAdmin (talk | contribs) No edit summary |
EnWikiAdmin (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p. | function p.getCategoryMembers(categoryName, limit) | ||
-- Validate input | |||
if not categoryName or categoryName == "" then | |||
return "Error: Please provide a valid category name." | |||
end | |||
if not categoryName then | -- Set default limit if not provided | ||
return "Error: | limit = limit or 10 | ||
-- Prepare API request parameters | |||
local params = { | |||
action = "query", | |||
list = "categorymembers", | |||
cmtitle = "Category:" .. categoryName, | |||
cmlimit = tostring(limit), | |||
format = "json" | |||
} | |||
-- Make the API request | |||
local apiUrl = mw.uri.fullUrl('api.php', params) | |||
local response = mw.http.get(apiUrl) | |||
-- Check for HTTP request errors | |||
if response.status ~= 200 then | |||
return "Error: HTTP request failed with status code " .. response.status | |||
end | end | ||
-- | -- Parse the JSON response | ||
local data = mw.text.jsonDecode(response.body) | |||
local | if not data then | ||
return "Error: Failed to parse JSON response." | |||
end | end | ||
-- Check | -- Check for query errors | ||
if | if not data.query or not data.query.categorymembers then | ||
return "No | return "Error: No category members found or invalid category." | ||
end | end | ||
-- | -- Generate a bulleted list of category members | ||
local output = {} | local output = {} | ||
for _, member in ipairs( | for _, member in ipairs(data.query.categorymembers) do | ||
table.insert(output, "* [[" .. member .. "]]") | table.insert(output, "* [[" .. member.title .. "]]") | ||
end | end | ||
-- Return the concatenated list | |||
return table.concat(output, "\n") | return table.concat(output, "\n") | ||
end | end | ||
return p | return p |
Revision as of 06:52, 22 December 2024
Documentation for this module may be created at Module:RecursiveSelectiveList/doc
local p = {}
function p.getCategoryMembers(categoryName, limit)
-- Validate input
if not categoryName or categoryName == "" then
return "Error: Please provide a valid category name."
end
-- Set default limit if not provided
limit = limit or 10
-- Prepare API request parameters
local params = {
action = "query",
list = "categorymembers",
cmtitle = "Category:" .. categoryName,
cmlimit = tostring(limit),
format = "json"
}
-- Make the API request
local apiUrl = mw.uri.fullUrl('api.php', params)
local response = mw.http.get(apiUrl)
-- Check for HTTP request errors
if response.status ~= 200 then
return "Error: HTTP request failed with status code " .. response.status
end
-- Parse the JSON response
local data = mw.text.jsonDecode(response.body)
if not data then
return "Error: Failed to parse JSON response."
end
-- Check for query errors
if not data.query or not data.query.categorymembers then
return "Error: No category members found or invalid category."
end
-- Generate a bulleted list of category members
local output = {}
for _, member in ipairs(data.query.categorymembers) do
table.insert(output, "* [[" .. member.title .. "]]")
end
-- Return the concatenated list
return table.concat(output, "\n")
end
return p