Module:RecursiveSelectiveList: Difference between revisions
EnWikiAdmin (talk | contribs) No edit summary |
EnWikiAdmin (talk | contribs) No edit summary |
||
Line 2: | Line 2: | ||
local mw = require('mw') | local mw = require('mw') | ||
-- Main function: Entry point for the module | |||
function p.main(frame) | function p.main(frame) | ||
-- | -- Retrieve the template arguments | ||
local args = frame.args | local args = frame.args | ||
-- Get the 'categoryName' parameter from the template | |||
local categoryName = args.categoryName | local categoryName = args.categoryName | ||
-- | -- Check if 'categoryName' is provided | ||
if not categoryName or categoryName == "" then | if not categoryName or categoryName == "" then | ||
return "Error: 'categoryName' parameter is missing or empty." | return "Error: 'categoryName' parameter is missing or empty." | ||
Line 13: | Line 16: | ||
-- Ensure the category name starts with "Category:" | -- Ensure the category name starts with "Category:" | ||
-- If the user provides "Books" instead of "Category:Books", fix it here | |||
if not categoryName:match("^Category:") then | if not categoryName:match("^Category:") then | ||
categoryName = "Category:" .. categoryName | categoryName = "Category:" .. categoryName | ||
end | end | ||
-- Debugging: Display the final category name being used | |||
local debugInfo = "Debug: Fetching members for '" .. categoryName .. "'\n" | |||
-- Fetch category members using the API | -- Fetch category members using the API | ||
local members = p.getCategoryMembers(categoryName) | local members = p.getCategoryMembers(categoryName) | ||
-- | -- Check if the category has members | ||
if not members or #members == 0 then | if not members or #members == 0 then | ||
return "The category '" .. categoryName .. "' has no members." | -- Add debug information about failure | ||
debugInfo = debugInfo .. "No members found for '" .. categoryName .. "'.\n" | |||
return debugInfo .. "The category '" .. categoryName .. "' has no members." | |||
end | end | ||
-- Return the list of members as a comma-separated string | -- Return the list of category members as a comma-separated string | ||
return table.concat(members, ", ") | return debugInfo .. "Members: " .. table.concat(members, ", ") | ||
end | end | ||
-- Function to fetch category members using the MediaWiki API | |||
function p.getCategoryMembers(categoryName) | function p.getCategoryMembers(categoryName) | ||
-- Table to store the member titles | |||
local members = {} | local members = {} | ||
-- | -- Debug: Log the API parameters | ||
local | local params = { | ||
action = "query", | action = "query", -- Query action for fetching data | ||
list = "categorymembers", | list = "categorymembers", -- Specify that we want category members | ||
cmtitle = categoryName, | cmtitle = categoryName, -- The full title of the category | ||
cmlimit = "max", -- | cmlimit = "max", -- Fetch the maximum number of members allowed (500 or server limit) | ||
format = "json" | format = "json" -- Specify that the API response should be in JSON format | ||
} | } | ||
-- Debugging: Log the parameters being sent to the API | |||
mw.logObject(params) | |||
-- Call the MediaWiki API | |||
local success, result = p.callAPI(params) | |||
-- Check if the API call succeeded | |||
if not success then | if not success then | ||
-- Log the failure to retrieve data | |||
mw.log("API call failed for category: " .. categoryName) | |||
return members -- Return an empty table on failure | return members -- Return an empty table on failure | ||
end | |||
-- Debugging: Log the raw API result | |||
mw.logObject(result) | |||
-- Check if the API result contains the 'query' and 'categorymembers' data | |||
if not result.query or not result.query.categorymembers then | |||
mw.log("Invalid API response structure for category: " .. categoryName) | |||
return members -- Return an empty table if the response is invalid | |||
end | end | ||
-- Extract member titles from the API result | -- Extract member titles from the API result | ||
for _, member in ipairs(result.query.categorymembers) do | for _, member in ipairs(result.query.categorymembers) do | ||
table.insert(members, member.title) | table.insert(members, member.title) -- Add the title of each member to the list | ||
end | end | ||
Line 53: | Line 82: | ||
end | end | ||
-- Function to call the MediaWiki API | |||
function p.callAPI(params) | function p.callAPI(params) | ||
-- | -- Use pcall to safely call the API and handle any errors | ||
local success, result = pcall(function() | local success, result = pcall(function() | ||
-- Call the MediaWiki API and return the JSON-decoded result | |||
return mw.ext.data.getJson(mw.site.api(params)) | return mw.ext.data.getJson(mw.site.api(params)) | ||
end) | end) | ||
-- Return whether the call succeeded and the result (or nil on failure) | |||
if success then | if success then | ||
return true, result | return true, result | ||
else | else | ||
mw.log("Error in API call: " .. tostring(result)) -- Log the error message | |||
return false, nil | return false, nil | ||
end | end |
Revision as of 07:37, 22 December 2024
Documentation for this module may be created at Module:RecursiveSelectiveList/doc
local p = {}
local mw = require('mw')
-- Main function: Entry point for the module
function p.main(frame)
-- Retrieve the template arguments
local args = frame.args
-- Get the 'categoryName' parameter from the template
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 the user provides "Books" instead of "Category:Books", fix it here
if not categoryName:match("^Category:") then
categoryName = "Category:" .. categoryName
end
-- Debugging: Display the final category name being used
local debugInfo = "Debug: Fetching members for '" .. categoryName .. "'\n"
-- Fetch category members using the API
local members = p.getCategoryMembers(categoryName)
-- Check if the category has members
if not members or #members == 0 then
-- Add debug information about failure
debugInfo = debugInfo .. "No members found for '" .. categoryName .. "'.\n"
return debugInfo .. "The category '" .. categoryName .. "' has no members."
end
-- Return the list of category members as a comma-separated string
return debugInfo .. "Members: " .. table.concat(members, ", ")
end
-- Function to fetch category members using the MediaWiki API
function p.getCategoryMembers(categoryName)
-- Table to store the member titles
local members = {}
-- Debug: Log the API parameters
local params = {
action = "query", -- Query action for fetching data
list = "categorymembers", -- Specify that we want category members
cmtitle = categoryName, -- The full title of the category
cmlimit = "max", -- Fetch the maximum number of members allowed (500 or server limit)
format = "json" -- Specify that the API response should be in JSON format
}
-- Debugging: Log the parameters being sent to the API
mw.logObject(params)
-- Call the MediaWiki API
local success, result = p.callAPI(params)
-- Check if the API call succeeded
if not success then
-- Log the failure to retrieve data
mw.log("API call failed for category: " .. categoryName)
return members -- Return an empty table on failure
end
-- Debugging: Log the raw API result
mw.logObject(result)
-- Check if the API result contains the 'query' and 'categorymembers' data
if not result.query or not result.query.categorymembers then
mw.log("Invalid API response structure for category: " .. categoryName)
return members -- Return an empty table if the response is invalid
end
-- Extract member titles from the API result
for _, member in ipairs(result.query.categorymembers) do
table.insert(members, member.title) -- Add the title of each member to the list
end
return members
end
-- Function to call the MediaWiki API
function p.callAPI(params)
-- Use pcall to safely call the API and handle any errors
local success, result = pcall(function()
-- Call the MediaWiki API and return the JSON-decoded result
return mw.ext.data.getJson(mw.site.api(params))
end)
-- Return whether the call succeeded and the result (or nil on failure)
if success then
return true, result
else
mw.log("Error in API call: " .. tostring(result)) -- Log the error message
return false, nil
end
end
return p