Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
Tag: Manual revert
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local mw = require('mw')
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
     -- Retrieve parameters from the template invocation
     local args = frame.args
     local args = frame.args
     local pageName = args.pageName
     local categoryName = args.categoryName


     -- Check if 'pageName' is provided
     -- Check if 'categoryName' is provided
     if not pageName or pageName == "" then
     if not categoryName or categoryName == "" then
         return "Error: 'pageName' parameter is missing or empty."
         return "Error: 'categoryName' parameter is missing or empty."
     end
     end


     -- Fetch content for the specified page
     -- Ensure the category name starts with 'Category:'
     local pageContent = mw.title.new(pageName):getContent()
    if not categoryName:match("^Category:") then
        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 page doesn't exist or has no content
     -- Handle cases where the category has no members
     if not pageContent then
     if #pages == 0 then
         return "Error: The page '" .. pageName .. "' does not exist or has no content."
         return "The category '" .. categoryName .. "' has no pages."
     end
     end


     return pageContent
    -- 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:21, 22 December 2024

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

local p = {}
local mw = require('mw')
local json = require('json') -- Ensure the JSON library is available

function p.main(frame)
    -- Retrieve parameters from the template invocation
    local args = frame.args
    local categoryName = args.categoryName

    -- Check if 'categoryName' is provided
    if not categoryName or categoryName == "" then
        return "Error: 'categoryName' parameter is missing or empty."
    end

    -- Ensure the category name starts with 'Category:'
    if not categoryName:match("^Category:") then
        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

return p