Module:CategoryReporter

From Freephile Wiki
Revision as of 09:54, 6 June 2025 by Admin (talk | contribs) (additional function to use with ExternalData)

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

-- a module that leverages mw.Api to query Categories

local p = {}

-- Function to get all subcategories of a category
function p.getSubcategories(frame)
    local categoryName = frame.args[1] or frame:getParent().args[1]
    if not categoryName then
        return "Error: Category name must be provided"
    end
    
    -- Remove "Category:" prefix if present
    categoryName = categoryName:gsub("^Category:", "")
    
    local api = mw.Api.new()
    local data = api:get({
        action = 'query',
        list = 'categorymembers',
        cmtitle = 'Category:' .. categoryName,
        cmtype = 'subcat',
        cmlimit = 500
    })
    
    local result = ""
    if data and data.query and data.query.categorymembers then
        result = result .. "== Subcategories of '" .. categoryName .. "' ==\n"
        result = result .. "{| class=\"wikitable sortable\"\n"
        result = result .. "! Category Name !! Pages !! Subcategories\n"
        
        for _, category in ipairs(data.query.categorymembers) do
            local catName = category.title:gsub("^Category:", "")
            local pageCount = p.getCategoryPageCount({catName})
            local subCatCount = p.getSubcategoryCount({catName})
            
            result = result .. "|-\n"
            result = result .. "| [[Category:" .. catName .. "|" .. catName .. "]] || " .. pageCount .. " || " .. subCatCount .. "\n"
        end
        
        result = result .. "|}\n"
    else
        result = "No subcategories found for '" .. categoryName .. "'"
    end
    
    return result
end

-- Function to get the count of pages in a category
function p.getCategoryPageCount(frame)
    local categoryName = frame.args and frame.args[1] or frame
    if type(categoryName) == "table" then
        categoryName = categoryName[1]
    end
    
    -- Remove "Category:" prefix if present
    categoryName = categoryName:gsub("^Category:", "")
    
    local api = mw.Api.new()
    local data = api:get({
        action = 'query',
        list = 'categorymembers',
        cmtitle = 'Category:' .. categoryName,
        cmtype = 'page',
        cmlimit = 500
    })
    
    if data and data.query and data.query.categorymembers then
        return #data.query.categorymembers
    else
        return 0
    end
end

-- Function to get the count of subcategories in a category
function p.getSubcategoryCount(frame)
    local categoryName = frame.args and frame.args[1] or frame
    if type(categoryName) == "table" then
        categoryName = categoryName[1]
    end
    
    -- Remove "Category:" prefix if present
    categoryName = categoryName:gsub("^Category:", "")
    
    local api = mw.Api.new()
    local data = api:get({
        action = 'query',
        list = 'categorymembers',
        cmtitle = 'Category:' .. categoryName,
        cmtype = 'subcat',
        cmlimit = 500
    })
    
    if data and data.query and data.query.categorymembers then
        return #data.query.categorymembers
    else
        return 0
    end
end

-- Function to list pages in a category
function p.listCategoryPages(frame)
    local categoryName = frame.args[1] or frame:getParent().args[1]
    if not categoryName then
        return "Error: Category name must be provided"
    end
    
    -- Remove "Category:" prefix if present
    categoryName = categoryName:gsub("^Category:", "")
    
    local api = mw.Api.new()
    local data = api:get({
        action = 'query',
        list = 'categorymembers',
        cmtitle = 'Category:' .. categoryName,
        cmtype = 'page',
        cmlimit = 500
    })
    
    local result = ""
    if data and data.query and data.query.categorymembers then
        result = result .. "== Pages in Category '" .. categoryName .. "' ==\n"
        result = result .. "<ul>\n"
        
        for _, page in ipairs(data.query.categorymembers) do
            result = result .. "<li>[[" .. page.title .. "]]</li>\n"
        end
        
        result = result .. "</ul>\n"
    else
        result = "No pages found in category '" .. categoryName .. "'"
    end
    
    return result
end

-- Function to generate a category tree
function p.categoryTree(frame)
    local categoryName = frame.args[1] or frame:getParent().args[1]
    local depth = tonumber(frame.args.depth or frame:getParent().args.depth or 2)
    
    if not categoryName then
        return "Error: Category name must be provided"
    end
    
    -- Remove "Category:" prefix if present
    categoryName = categoryName:gsub("^Category:", "")
    
    return p.renderCategoryTree(categoryName, depth, 0)
end

-- Helper function for category tree rendering
function p.renderCategoryTree(categoryName, maxDepth, currentDepth)
    if currentDepth >= maxDepth then
        return ""
    end
    
    local api = mw.Api.new()
    local data = api:get({
        action = 'query',
        list = 'categorymembers',
        cmtitle = 'Category:' .. categoryName,
        cmtype = 'subcat',
        cmlimit = 500
    })
    
    local result = ""
    if data and data.query and data.query.categorymembers and #data.query.categorymembers > 0 then
        result = result .. "<ul>\n"
        
        for _, category in ipairs(data.query.categorymembers) do
            local catName = category.title:gsub("^Category:", "")
            result = result .. "<li>[[Category:" .. catName .. "|" .. catName .. "]]"
            
            if currentDepth + 1 < maxDepth then
                result = result .. p.renderCategoryTree(catName, maxDepth, currentDepth + 1)
            end
            
            result = result .. "</li>\n"
        end
        
        result = result .. "</ul>\n"
    end
    
    return result
end

-- Function to get category info and combine with the External Data extension
function p.categoryWithExternalData(frame)
    local categoryName = frame.args[1] or frame:getParent().args[1]
    if not categoryName then
        return "Error: Category name must be provided"
    end
    
    -- Remove "Category:" prefix if present
    categoryName = categoryName:gsub("^Category:", "")
    
    -- Get category pages via API
    local api = mw.Api.new()
    local data = api:get({
        action = 'query',
        list = 'categorymembers',
        cmtitle = 'Category:' .. categoryName,
        cmtype = 'page',
        cmlimit = 500
    })
    
    local result = ""
    if data and data.query and data.query.categorymembers then
        result = result .. "== Category '" .. categoryName .. "' with External Data ==\n"
        result = result .. "{| class=\"wikitable sortable\"\n"
        result = result .. "! Page Title !! Last Modified !! External Reference\n"
        
        for _, page in ipairs(data.query.categorymembers) do
            -- For each page, we'll use its title to generate an external data reference
            -- This part would be customized based on the actual external data source
            local pageTitle = page.title
            
            -- This is where we would add ExternalData functionality
            -- The actual implementation depends on what external data you want to integrate
            result = result .. "|-\n"
            result = result .. "| [[" .. pageTitle .. "]] || {{#external_value:lastmodified|page=" .. pageTitle .. "}} || {{#external_value:reference|page=" .. pageTitle .. "}}\n"
        end
        
        result = result .. "|}\n"
    else
        result = "No pages found in category '" .. categoryName .. "'"
    end
    
    return result
end

return p