Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
function p.callAPI(params)
local mw = require('mw')
    local success, response = pcall(function()
        -- Call the MediaWiki API and return the raw response
        return mw.site.api(params)
    end)


-- Main function: Entry point for the module
     if success then
function p.main(frame)
        return true, response
     -- Retrieve the template arguments
     else
    local args = frame.args
         return false, "Error: " .. tostring(response)
 
    -- 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
     end
    -- Ensure the category name starts with "Category:"
    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
end


-- Function to fetch category members using the MediaWiki API
function p.main(frame)
function p.getCategoryMembers(categoryName)
    -- Table to store the member titles
    local members = {}
 
    -- Debug: Log the API parameters
     local params = {
     local params = {
         action = "query", -- Query action for fetching data
         action = "query",
         list = "categorymembers", -- Specify that we want category members
         list = "categorymembers",
         cmtitle = categoryName, -- The full title of the category
         cmtitle = "Category:Books",
         cmlimit = "max", -- Fetch the maximum number of members allowed (500 or server limit)
         cmlimit = "max",
         format = "json" -- Specify that the API response should be in JSON format
         format = "json"
     }
     }


    -- Debugging: Log the parameters being sent to the API
     local success, response = p.callAPI(params)
    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
        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
     if success then
         -- Check for API errors
         return response -- Return raw JSON response for debugging
        if result.error then
            mw.log("API Error: " .. result.error.info)
            return false, nil
        end
        return true, result
     else
     else
         mw.log("Error in API call: " .. tostring(result)) -- Log the error message
         return response -- Return error message
        return false, nil
     end
     end
end
end


return p
return p

Revision as of 07:45, 22 December 2024

Documentation for this module may be created at Module:RecursiveSelectiveList/doc

function p.callAPI(params)
    local success, response = pcall(function()
        -- Call the MediaWiki API and return the raw response
        return mw.site.api(params)
    end)

    if success then
        return true, response
    else
        return false, "Error: " .. tostring(response)
    end
end

function p.main(frame)
    local params = {
        action = "query",
        list = "categorymembers",
        cmtitle = "Category:Books",
        cmlimit = "max",
        format = "json"
    }

    local success, response = p.callAPI(params)
    if success then
        return response -- Return raw JSON response for debugging
    else
        return response -- Return error message
    end
end

return p