Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local mw = require('mw')
local json = require('json') -- Ensure the JSON library is available


function p.main(frame)
function p.main(frame)
    -- Retrieve parameters from the template invocation
     local categoryName = frame.args.categoryName or "Category:Example" -- Default category
     local args = frame.args
     local limit = tonumber(frame.args.limit) or 10 -- Default limit
     local categoryName = args.categoryName


     -- Check if 'categoryName' is provided
     -- Fetch category members
     if not categoryName or categoryName == "" then
    local members = mw.site.categoryMembers(categoryName, nil, limit)
         return "Error: 'categoryName' parameter is missing or empty."
      
    -- Build the output
    local results = {}
    for _, member in ipairs(members) do
         table.insert(results, string.format("* [[%s]]", member.title))
     end
     end


    -- Ensure the category name starts with 'Category:'
     -- Return the results as a bulleted list
    if not categoryName:match("^Category:") then
     return table.concat(results, "\n")
        categoryName = "Category:" .. categoryName
    end
 
    -- Prepare the API request parameters
    local params = {
        action = "query",
        list = "categorymembers",
        cmtitle = categoryName,
        cmlimit = "max",
        format = "json"
    }
 
    -- Make the API request
    local result = mw.site.api(params)
    if not result then
        return "Error: Failed to retrieve data from the API."
    end
 
    -- Parse the JSON response
    local data = json.decode(result)
    if not data or not data.query or not data.query.categorymembers then
        return "Error: Invalid API response."
    end
 
    -- Extract page titles from the response
    local pages = {}
    for _, page in ipairs(data.query.categorymembers) do
        table.insert(pages, page.title)
    end
 
    -- Handle cases where the category has no members
    if #pages == 0 then
        return "The category '" .. categoryName .. "' has no pages."
    end
 
     -- Return the list of page titles as a comma-separated string
     return table.concat(pages, ", ")
end
end


return p
return p

Revision as of 07:23, 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

    -- Fetch category members
    local members = mw.site.categoryMembers(categoryName, nil, limit)
    
    -- Build the output
    local results = {}
    for _, member in ipairs(members) do
        table.insert(results, string.format("* [[%s]]", member.title))
    end

    -- Return the results as a bulleted list
    return table.concat(results, "\n")
end

return p