Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
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)
function p.main(frame)
Line 31: Line 9:
     end
     end


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


     if #members == 0 then
     -- Get category members
         return "No pages found in the category."
    local members = category:linkedPages()
    local output = {}
 
    for _, member in ipairs(members) do
         table.insert(output, "* [[" .. member.fullText .. "]]")
     end
     end


     -- Generate a bulleted list
     if #output == 0 then
    local output = {}
         return "No pages found in the category."
    for _, member in ipairs(members) do
         table.insert(output, "* [[" .. member.title .. "]]")
     end
     end



Revision as of 06:49, 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']

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

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

    -- Get category members
    local members = category:linkedPages()
    local output = {}

    for _, member in ipairs(members) do
        table.insert(output, "* [[" .. member.fullText .. "]]")
    end

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

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

return p