Module:CategoryCheck

From Kingdom Come: Deliverance Wiki
Jump to navigation Jump to search

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

local p = {}

-- Recursive function to check if a category or its subcategories match the target
local function isInCategory(frame, title, targetCategory)
    local categoryPage = mw.title.new(title)

    if not categoryPage then
        return false
    end

    -- Get the categories of the current page
    local categories = categoryPage:getCategories()
    
    -- Check if the current page is in the target category
    for _, category in ipairs(categories) do
        if category == 'Category:' .. targetCategory then
            return true
        end
    end

    -- Recursively check subcategories
    for _, category in ipairs(categories) do
        if string.sub(category, 1, 9) == 'Category:' then
            if isInCategory(frame, category, targetCategory) then
                return true
            end
        end
    end

    return false
end

-- This function checks if the current page belongs to the target category or any of its subcategories
function p.isInCategory(frame)
    local targetCategory = frame.args[1] or ''
    local page = mw.title.getCurrentTitle()
    return isInCategory(frame, page.text, targetCategory)
end

return p