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, cmtype, continue)
 
local function fetchCategoryMembers(categoryName)
     local url = mw.uri.fullUrl('api.php', {
     local url = mw.uri.fullUrl('api.php', {
         action = 'query',
         action = 'query',
         list = 'categorymembers',
         list = 'categorymembers',
         cmtitle = 'Category:' .. categoryName,
         cmtitle = 'Category:' .. categoryName,
        cmtype = cmtype,
         cmlimit = 'max',
         cmlimit = 'max',
         format = 'json',
         format = 'json'
        cmcontinue = continue
     })
     })


Line 21: Line 20:
     end
     end


     return data
     return data.query.categorymembers
end
 
local function getPagesRecursively(categoryName, visitedCategories, pages, limit)
    if visitedCategories[categoryName] then
        return
    end
    visitedCategories[categoryName] = true
 
    -- Fetch subcategories
    local continue
    repeat
        local data, err = fetchCategoryMembers(categoryName, 'subcat', continue)
        if not data then
            return nil, err
        end
 
        for _, member in ipairs(data.query.categorymembers) do
            getPagesRecursively(member.title:match('Category:(.+)'), visitedCategories, pages, limit)
            if limit and #pages >= limit then
                return
            end
        end
 
        continue = data.continue and data.continue.cmcontinue
    until not continue
 
    -- Fetch pages in the main namespace
    continue = nil
    repeat
        local data, err = fetchCategoryMembers(categoryName, 'page', continue)
        if not data then
            return nil, err
        end
 
        for _, member in ipairs(data.query.categorymembers) do
            if member.ns == 0 then
                table.insert(pages, member.title)
                if limit and #pages >= limit then
                    return
                end
            end
        end
 
        continue = data.continue and data.continue.cmcontinue
    until not continue
end
end


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'])


     if not categoryName then
     if not categoryName then
Line 79: Line 31:
     end
     end


     local visitedCategories = {}
     -- Fetch category members
     local pages = {}
     local members, err = fetchCategoryMembers(categoryName)
 
     if not members then
    local success, err = pcall(function()
        getPagesRecursively(categoryName, visitedCategories, pages, limit)
    end)
 
     if not success then
         return "Error: " .. err
         return "Error: " .. err
     end
     end


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


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


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

Revision as of 06:48, 22 December 2024

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

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)
    local args = frame:getParent().args
    local categoryName = args['categoryName']

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

    -- Fetch category members
    local members, err = fetchCategoryMembers(categoryName)
    if not members then
        return "Error: " .. err
    end

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

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

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

return p