Module:RecursiveSelectiveList: Difference between revisions

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


-- Function to fetch category members
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
     local limit = tonumber(args['limit']) or 10
        return "Error: Please provide a valid category name."
     end


     -- Set default limit if not provided
     if not categoryName then
    limit = limit or 10
         return "Error: Please provide a category name."
 
    -- 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
     end


     -- Check for query errors
     -- Fetch category members
     if not data.query or not data.query.categorymembers then
     local category = mw.title.new("Category:" .. categoryName)
         return "Error: No category members found or invalid category."
    if not category then
         return "Error: Invalid category name."
     end
     end


     -- Generate a bulleted list of category members
     local members = {}
    local output = {}
     for member in category:members() do
     for _, member in ipairs(data.query.categorymembers) do
        if #members >= limit then
         table.insert(output, "* [[" .. member.title .. "]]")
            break
        end
         table.insert(members, "* [[" .. member.fullText .. "]]")
     end
     end


     -- Return the concatenated list
     if #members == 0 then
    return table.concat(output, "\n")
         return "No pages found in the category."
end
 
-- Main function for `#invoke`
function p.main(frame)
    local args = frame:getParent().args
    local categoryName = args['categoryName']
    local limit = tonumber(args['limit'])
 
    if not categoryName then
         return "Error: Please provide a category name."
     end
     end


    -- Call the function to fetch category members
     return table.concat(members, "\n")
     return p.getCategoryMembers(categoryName, limit)
end
end


return p
return p

Revision as of 06:55, 22 December 2024

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

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local categoryName = args['categoryName']
    local limit = tonumber(args['limit']) or 10

    if not categoryName then
        return "Error: Please provide a category name."
    end

    -- Fetch category members
    local category = mw.title.new("Category:" .. categoryName)
    if not category then
        return "Error: Invalid category name."
    end

    local members = {}
    for member in category:members() do
        if #members >= limit then
            break
        end
        table.insert(members, "* [[" .. member.fullText .. "]]")
    end

    if #members == 0 then
        return "No pages found in the category."
    end

    return table.concat(members, "\n")
end

return p