Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
No edit summary
Line 9: Line 9:
     end
     end


     -- Fetch category members
     -- Use mw.site's API wrapper to fetch category members
     local categoryTitle = "Category:" .. categoryName
     local members = {}
     local categoryPage = mw.title.new(categoryTitle)
     local limit = 500  -- Fetch up to 500 results at a time
    if not categoryPage or not categoryPage.exists then
 
        return "Error: Category does not exist."
    -- Fetch members iteratively
    for page in mw.site.categoryMembers('Category:' .. categoryName, 'all', limit) do
        table.insert(members, page.text)
     end
     end


     local members = categoryPage:members()
     -- Check if any members are found
     if not members then
     if #members == 0 then
         return "Error: Unable to fetch category members."
         return "No pages found in the category."
     end
     end


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



Revision as of 06:51, 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

    -- Use mw.site's API wrapper to fetch category members
    local members = {}
    local limit = 500  -- Fetch up to 500 results at a time

    -- Fetch members iteratively
    for page in mw.site.categoryMembers('Category:' .. categoryName, 'all', limit) do
        table.insert(members, page.text)
    end

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

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

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

return p