Module:RecursiveSelectiveList: Difference between revisions

From Ikwipedia
No edit summary
No edit summary
Line 3: Line 3:


function p.main(frame)
function p.main(frame)
     -- Call the debug function to display all information
     local args = frame.args
     return p.debug(frame)
     local categoryName = args.categoryName
end
 
    if not categoryName or categoryName == "" then
        return "Error: 'categoryName' parameter is missing or empty."
    end


function p.debug(frame)
    -- Ensure the category name starts with "Category:"
     local html = mw.html.create("div"):addClass("debug-output")
     if not categoryName:match("^Category:") then
        categoryName = "Category:" .. categoryName
    end


     -- Retrieve parameters from the template invocation
     -- Fetch category members
     local args = frame.args
     local members = p.getCategoryMembers(categoryName)
    html:tag("h3"):wikitext("Debugging Information")


     -- Add each parameter as a key-value pair
     -- If no members, return a message
     html:tag("h4"):wikitext("Template Arguments:")
     if #members == 0 then
    local argsList = html:tag("ul")
         return "The category '" .. categoryName .. "' has no members."
    for key, value in pairs(args) do
         argsList:tag("li"):wikitext(key .. " = " .. (value or "nil"))
     end
     end


     -- Example of debugging mw.title.new
     -- Return the list of members as a comma-separated string
     local pageName = args.pageName or "No pageName provided"
     return table.concat(members, ", ")
    html:tag("h4"):wikitext("Page Name Provided:")
end
    html:tag("p"):wikitext(pageName)


     if pageName ~= "No pageName provided" then
function p.getCategoryMembers(categoryName)
        local titleObject = mw.title.new(pageName)
     local category = mw.title.new(categoryName)
        if titleObject then
    if not category then
            html:tag("h4"):wikitext("Title Object Debugging:")
        return {} -- Return an empty table if the category doesn't exist
            local titleList = html:tag("ul")
    end
            titleList:tag("li"):wikitext("Full Text: " .. (titleObject.fullText or "nil"))
            titleList:tag("li"):wikitext("Namespace: " .. (titleObject.namespace or "nil"))
            titleList:tag("li"):wikitext("ID: " .. (titleObject.id or "nil"))


            -- Try to get content
    -- Use the MediaWiki API to get category members
            local pageContent = titleObject:getContent()
    local members = {}
            if pageContent then
    for member in category:members() do
                html:tag("h4"):wikitext("Page Content:")
        table.insert(members, member.title)
                html:tag("pre"):wikitext(pageContent)
            else
                html:tag("h4"):wikitext("Page Content:"):tag("p"):wikitext("No content found or page does not exist.")
            end
        else
            html:tag("h4"):wikitext("Title Object:"):tag("p"):wikitext("Failed to create title object for page name.")
        end
     end
     end


     return tostring(html)
     return members
end
end


return p
return p

Revision as of 07:34, 22 December 2024

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

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

function p.main(frame)
    local args = frame.args
    local categoryName = args.categoryName

    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

    -- Fetch category members
    local members = p.getCategoryMembers(categoryName)

    -- If no members, return a message
    if #members == 0 then
        return "The category '" .. categoryName .. "' has no members."
    end

    -- Return the list of members as a comma-separated string
    return table.concat(members, ", ")
end

function p.getCategoryMembers(categoryName)
    local category = mw.title.new(categoryName)
    if not category then
        return {} -- Return an empty table if the category doesn't exist
    end

    -- Use the MediaWiki API to get category members
    local members = {}
    for member in category:members() do
        table.insert(members, member.title)
    end

    return members
end

return p