Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
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
     -- Retrieve parameters from the template invocation
     local args = frame.args
     local args = frame.args
    -- Get the 'categoryName' parameter from the template
     local categoryName = args.categoryName
     local categoryName = args.categoryName


Line 20: Line 17:
     end
     end


     -- Debugging: Display the final category name being used
     -- Fetch the category page content
     local debugInfo = "Debug: Fetching members for '" .. categoryName .. "'\n"
     local categoryPage = mw.title.new(categoryName)
 
     if not categoryPage then
    -- Fetch category members using the built-in method
         return "Error: Invalid category name '" .. categoryName .. "'."
    local members = p.getCategoryMembers(categoryName, args.limit)
 
    -- 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
     end


     -- Return the list of category members as a comma-separated string
     local content = categoryPage:getContent()
    return debugInfo .. "Members: " .. table.concat(members, ", ")
end


-- Function to fetch category members using the getCategoryMembers method
     -- Handle cases where the page has no content
function p.getCategoryMembers(categoryName, limit)
     if not content then
     -- Table to store the member titles
         return "The category '" .. categoryName .. "' does not exist or has no content."
     local members = {}
 
    -- Define default limit if not provided
    local cmlimit = tonumber(limit) or 500  -- MediaWiki allows up to 500 for bots and 50 for regular users
 
    -- Get the category title object
    local categoryTitle = mw.title.new(categoryName)
    if not categoryTitle then
         mw.log("Error: Invalid category title '" .. categoryName .. "'.")
        return members
     end
     end


     -- Fetch category members using the method on the category title
     -- Return the raw content of the category page
     local iterator = categoryTitle:getCategoryMembers(cmlimit)
     return "Raw content of category '" .. categoryName .. "':\n\n" .. content
 
    -- Iterate over the category members and collect their titles
    for _, member in ipairs(iterator) do
        table.insert(members, member.title)
    end
 
    -- Debugging: Log the number of members fetched
    mw.log("Fetched " .. tostring(#members) .. " members from category '" .. categoryName .. "'.")
 
    return members
end
end


return p
return p

Revision as of 07:59, 22 December 2024

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

local p = {}
local mw = require('mw')

function p.main(frame)
    -- Retrieve parameters from the template invocation
    local args = frame.args
    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 not categoryName:match("^Category:") then
        categoryName = "Category:" .. categoryName
    end

    -- Fetch the category page content
    local categoryPage = mw.title.new(categoryName)
    if not categoryPage then
        return "Error: Invalid category name '" .. categoryName .. "'."
    end

    local content = categoryPage:getContent()

    -- Handle cases where the page has no content
    if not content then
        return "The category '" .. categoryName .. "' does not exist or has no content."
    end

    -- Return the raw content of the category page
    return "Raw content of category '" .. categoryName .. "':\n\n" .. content
end

return p