Module:RecursiveSelectiveList: Difference between revisions
EnWikiAdmin (talk | contribs) 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..." |
EnWikiAdmin (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function getPagesFromCategory(categoryName, limit, | -- 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 | ||
table.insert(output, "<!-- Debug: Category '" .. categoryName .. "' does not exist -->") | |||
return | |||
end | end | ||
-- | -- Debug: Add to HTML comments | ||
if | 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 | end | ||
-- Get | -- Get subcategories and process them recursively | ||
local | local subcategories = {} | ||
for _, member in ipairs(members) do | for _, member in ipairs(members) do | ||
if member. | if member.ns == 14 and not visited[member.prefixedText] then | ||
visited[member.prefixedText] = true | |||
table.insert(subcategories, member.title) | |||
end | 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 # | if limit and #output >= limit then | ||
break | |||
end | end | ||
getPagesFromCategory(subcategory, limit, visited, output) | |||
end | end | ||
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 | ||
-- Debug: Add initial input validation to comments | |||
table.insert(debugOutput, "<!-- Debug: Starting with category '" .. categoryName .. "' -->") | |||
local output = {} | 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 | 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