Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


function p.main(frame)
function p.getCategoryMembers(categoryName, limit)
     local args = frame:getParent().args
     -- Validate input
     local categoryName = args['categoryName']
    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: Please provide a category name."
    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


     -- Use mw.site's API wrapper to fetch category members
     -- Parse the JSON response
    local members = {}
     local data = mw.text.jsonDecode(response.body)
     local limit = 500  -- Fetch up to 500 results at a time
    if not data then
 
         return "Error: Failed to parse JSON response."
    -- Fetch members iteratively
    for page in mw.site.categoryMembers('Category:' .. categoryName, 'all', limit) do
         table.insert(members, page.text)
     end
     end


     -- Check if any members are found
     -- Check for query errors
     if #members == 0 then
     if not data.query or not data.query.categorymembers then
         return "No pages found in the category."
         return "Error: No category members found or invalid category."
     end
     end


     -- Create output list
     -- Generate a bulleted list of category members
     local output = {}
     local output = {}
     for _, member in ipairs(members) do
     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