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


function p.main(frame)
-- Function to fetch content from a specified page
     local args = frame:getParent().args
function p.getPageContent(pageTitle)
    local categoryName = args['categoryName']
     -- Check if a page title is provided
    local limit = tonumber(args['limit']) or 10
     if not pageTitle or pageTitle == "" then
 
         return "Error: No page title provided."
     if not categoryName then
        return "Error: Please provide a category name."
    end
 
    -- Fetch category members
    local category = mw.title.new("Category:" .. categoryName)
    if not category then
         return "Error: Invalid category name."
     end
     end


     local members = {}
    -- Use the MediaWiki API to fetch the page content
    for member in category:members() do
     local status, content = pcall(function()
         if #members >= limit then
         return mw.title.new(pageTitle):getContent()
            break
     end)
        end
        table.insert(members, "* [[" .. member.fullText .. "]]")
     end


     if #members == 0 then
    -- Handle errors or return content
         return "No pages found in the category."
     if not status or not content then
         return "Error: Unable to fetch content for page '" .. pageTitle .. "'."
     end
     end


     return table.concat(members, "\n")
     return content or "Error: Page content is empty."
end
end


return p
-- Function to be invoked from templa

Revision as of 07:01, 22 December 2024

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

local p = {}
local mw = require('mw')

-- Function to fetch content from a specified page
function p.getPageContent(pageTitle)
    -- Check if a page title is provided
    if not pageTitle or pageTitle == "" then
        return "Error: No page title provided."
    end

    -- Use the MediaWiki API to fetch the page content
    local status, content = pcall(function()
        return mw.title.new(pageTitle):getContent()
    end)

    -- Handle errors or return content
    if not status or not content then
        return "Error: Unable to fetch content for page '" .. pageTitle .. "'."
    end

    return content or "Error: Page content is empty."
end

-- Function to be invoked from templa