Module:RecursiveSelectiveList

From Ikwipedia
Revision as of 06:48, 22 December 2024 by EnWikiAdmin (talk | contribs)

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

local p = {}

local function fetchCategoryMembers(categoryName)
    local url = mw.uri.fullUrl('api.php', {
        action = 'query',
        list = 'categorymembers',
        cmtitle = 'Category:' .. categoryName,
        cmlimit = 'max',
        format = 'json'
    })

    local response = mw.http.get(url)
    if response.status ~= 200 then
        return nil, "HTTP request failed with status: " .. response.status
    end

    local data = mw.json.decode(response.body)
    if not data then
        return nil, "Failed to decode JSON response."
    end

    return data.query.categorymembers
end

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

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

    -- Fetch category members
    local members, err = fetchCategoryMembers(categoryName)
    if not members then
        return "Error: " .. err
    end

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

    -- Generate a bulleted list
    local output = {}
    for _, member in ipairs(members) do
        table.insert(output, "* [[" .. member.title .. "]]")
    end

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

return p