Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
No edit summary
Line 5: Line 5:
     local limit = tonumber(frame.args.limit) or 10 -- Default limit
     local limit = tonumber(frame.args.limit) or 10 -- Default limit


     -- Fetch category members
     -- Get the category title
     local members = mw.site.categoryMembers(categoryName, nil, limit)
     local category = mw.title.new(categoryName)
      
     if not category or not category.exists then
     -- Build the output
        return "Error: Category does not exist or is invalid."
    end
 
     -- Retrieve the pages in the category
    local pages = category:members()
     local results = {}
     local results = {}
     for _, member in ipairs(members) do
    local count = 0
         table.insert(results, string.format("* [[%s]]", member.title))
 
     for page in pages do
         table.insert(results, string.format("* [[%s]]", page.title))
        count = count + 1
        if count >= limit then
            break
        end
     end
     end


     -- Return the results as a bulleted list
     -- Return the results as a bulleted list
    if #results == 0 then
        return "No pages found in this category."
    end
     return table.concat(results, "\n")
     return table.concat(results, "\n")
end
end


return p
return p

Revision as of 07:24, 22 December 2024

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

local p = {}

function p.main(frame)
    local categoryName = frame.args.categoryName or "Category:Example" -- Default category
    local limit = tonumber(frame.args.limit) or 10 -- Default limit

    -- Get the category title
    local category = mw.title.new(categoryName)
    if not category or not category.exists then
        return "Error: Category does not exist or is invalid."
    end

    -- Retrieve the pages in the category
    local pages = category:members()
    local results = {}
    local count = 0

    for page in pages do
        table.insert(results, string.format("* [[%s]]", page.title))
        count = count + 1
        if count >= limit then
            break
        end
    end

    -- Return the results as a bulleted list
    if #results == 0 then
        return "No pages found in this category."
    end
    return table.concat(results, "\n")
end

return p