Module:RecursiveSelectiveList

From Ikwipedia
Revision as of 07:44, 22 December 2024 by EnWikiAdmin (talk | contribs)

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 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
        local response = mw.site.api(params)
        mw.log("Raw API response: " .. tostring(response))
        return mw.text.jsonDecode(response)
    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