Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
Created page with "local p = {} local function getPagesFromCategory(categoryName, limit, visitedCategories, pages) local categoryTitle = mw.title.new("Category:" .. categoryName) if not categoryTitle or not categoryTitle.exists then return pages -- Return the list if the category doesn't exist end -- Prevent infinite loops in case of circular category references if visitedCategories[categoryName] then return pages end visitedCategories[category..."
 
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


local function getPagesFromCategory(categoryName, limit, visitedCategories, pages)
-- Function to get pages recursively
local function getPagesFromCategory(categoryName, limit, visited, output)
     local categoryTitle = mw.title.new("Category:" .. categoryName)
     local categoryTitle = mw.title.new("Category:" .. categoryName)
     if not categoryTitle or not categoryTitle.exists then
     if not categoryTitle or not categoryTitle.exists then
         return pages -- Return the list if the category doesn't exist
         table.insert(output, "<!-- Debug: Category '" .. categoryName .. "' does not exist -->")
        return
     end
     end


     -- Prevent infinite loops in case of circular category references
     -- Debug: Add to HTML comments
     if visitedCategories[categoryName] then
    table.insert(output, "<!-- Debug: Processing category '" .. categoryName .. "' -->")
         return pages
 
    -- Get pages directly in the category
     local members = mw.site.categories[categoryName]
    for _, member in ipairs(members) do
        if member.ns == 0 and not visited[member.prefixedText] then
            visited[member.prefixedText] = true
            table.insert(output, "* [[" .. member.prefixedText .. "]]")
         end
     end
     end
    visitedCategories[categoryName] = true


     -- Get all members of the category
     -- Get subcategories and process them recursively
     local members = categoryTitle:linkedPages()
     local subcategories = {}
     for _, member in ipairs(members) do
     for _, member in ipairs(members) do
         if member.namespace == 0 then -- Only include Main namespace pages
         if member.ns == 14 and not visited[member.prefixedText] then
             table.insert(pages, member)
             visited[member.prefixedText] = true
        elseif member.namespace == 14 then -- If it's a subcategory
             table.insert(subcategories, member.title)
             getPagesFromCategory(member.text, limit, visitedCategories, pages)
         end
         end
    end
    -- Debug: Log subcategories being processed
    table.insert(output, "<!-- Debug: Found " .. #subcategories .. " subcategories in '" .. categoryName .. "' -->")


        -- Stop if we've reached the limit
    for _, subcategory in ipairs(subcategories) do
         if limit and #pages >= limit then
         if limit and #output >= limit then
             return pages
             break
         end
         end
        getPagesFromCategory(subcategory, limit, visited, output)
     end
     end
    return pages
end
end


-- Main function
function p.main(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local categoryName = args["categoryName"]
     local categoryName = args["categoryName"]
     local limit = tonumber(args["limit"])
     local limit = tonumber(args["limit"])
    local debugOutput = {} -- For storing debug comments


     if not categoryName then
     if not categoryName then
Line 40: Line 52:
     end
     end


     local pages = getPagesFromCategory(categoryName, limit, {}, {})
     -- Debug: Add initial input validation to comments
    if #pages == 0 then
    table.insert(debugOutput, "<!-- Debug: Starting with category '" .. categoryName .. "' -->")
        return "No pages found."
    end


    -- Generate a bulleted list
     local output = {}
     local output = {}
     for _, page in ipairs(pages) do
     getPagesFromCategory(categoryName, limit, {}, output)
        table.insert(output, "* [[" .. page.prefixedText .. "]]")
    end


     return table.concat(output, "\n")
     -- Join output and debug comments
    local result = table.concat(debugOutput, "\n") .. "\n" .. table.concat(output, "\n")
    return result
end
end


return p
return p

Revision as of 06:42, 22 December 2024

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

local p = {}

-- Function to get pages recursively
local function getPagesFromCategory(categoryName, limit, visited, output)
    local categoryTitle = mw.title.new("Category:" .. categoryName)
    if not categoryTitle or not categoryTitle.exists then
        table.insert(output, "<!-- Debug: Category '" .. categoryName .. "' does not exist -->")
        return
    end

    -- Debug: Add to HTML comments
    table.insert(output, "<!-- Debug: Processing category '" .. categoryName .. "' -->")

    -- Get pages directly in the category
    local members = mw.site.categories[categoryName]
    for _, member in ipairs(members) do
        if member.ns == 0 and not visited[member.prefixedText] then
            visited[member.prefixedText] = true
            table.insert(output, "* [[" .. member.prefixedText .. "]]")
        end
    end

    -- Get subcategories and process them recursively
    local subcategories = {}
    for _, member in ipairs(members) do
        if member.ns == 14 and not visited[member.prefixedText] then
            visited[member.prefixedText] = true
            table.insert(subcategories, member.title)
        end
    end

    -- Debug: Log subcategories being processed
    table.insert(output, "<!-- Debug: Found " .. #subcategories .. " subcategories in '" .. categoryName .. "' -->")

    for _, subcategory in ipairs(subcategories) do
        if limit and #output >= limit then
            break
        end
        getPagesFromCategory(subcategory, limit, visited, output)
    end
end

-- Main function
function p.main(frame)
    local args = frame:getParent().args
    local categoryName = args["categoryName"]
    local limit = tonumber(args["limit"])
    local debugOutput = {} -- For storing debug comments

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

    -- Debug: Add initial input validation to comments
    table.insert(debugOutput, "<!-- Debug: Starting with category '" .. categoryName .. "' -->")

    local output = {}
    getPagesFromCategory(categoryName, limit, {}, output)

    -- Join output and debug comments
    local result = table.concat(debugOutput, "\n") .. "\n" .. table.concat(output, "\n")
    return result
end

return p