Module:RecursiveSelectiveList

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

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

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

function p.main(frame)
    local args = frame.args
    local categoryName = args.categoryName

    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 category members
    local members = p.getCategoryMembers(categoryName)

    -- If no members, return a message
    if #members == 0 then
        return "The category '" .. categoryName .. "' has no members."
    end

    -- Return the list of members as a comma-separated string
    return table.concat(members, ", ")
end

function p.getCategoryMembers(categoryName)
    local category = mw.title.new(categoryName)
    if not category then
        return {} -- Return an empty table if the category doesn't exist
    end

    -- Use the MediaWiki API to get category members
    local members = {}
    for member in category:members() do
        table.insert(members, member.title)
    end

    return members
end

return p