Jump to content

Module:T-Process

Revision as of 16:22, 26 April 2025 by MarkWD (talk | contribs) (// via Wikitext Extension for VSCode)

Documentation for this module may be created at Module:T-Process/doc

--Module:T-Process
-- Renders the "Process" template for governance processes and initiatives, making use of ICANNWiki's "Template Blueprint Framework"

local p = {}

-- ========== Required modules ==========
local Blueprint = require('Module:LuaTemplateBlueprint')
local ConfigRepository = require('Module:ConfigRepository')
local ErrorHandling = require('Module:ErrorHandling')
local LinkParser = require('Module:LinkParser')

-- Blueprint default: Module-level cache for lazy-loaded modules
local moduleCache = {}

-- Blueprint default: Lazy module loader
local function lazyRequire(moduleName)
    return function()
        if not moduleCache[moduleName] then
            moduleCache[moduleName] = require(moduleName)
        end
        return moduleCache[moduleName]
    end
end

-- Blueprint default: Modules to lazy load
    -- local getTemplateHelpers = lazyRequire('')

-- ========== Helper Functions ==========
-- Blueprint default: Create error context for the module
local errorContext = ErrorHandling.createContext("T-Process")

-- ================================================================================

-- IMPORTANT! TEMPLATE BLUEPRINT FRAMEWORK INSTRUCTIONS
-- CONTROL OF TEMPLATE FEATURES: THIS LIST SPECIFIES IN AN EXPLICIT MANNER WHAT FEATURES ARE TO BE CALLED/RENDERED BY THE TEMPLATE.
local template = Blueprint.registerTemplate('Process', {
    features = {
        title = true,
        logo = true,
        fields = true,
        socialMedia = true,
        semanticProperties = true,
        categories = true,
        errorReporting = true
    }
})

-- Initialize the standard configuration
Blueprint.initializeConfig(template)

-- ================================================================================

-- ========== Preprocessors ==========
-- Basic preprocessors
Blueprint.addPreprocessor(template, 'setPageIdField')
Blueprint.addPreprocessor(template, 'deriveRegionFromCountry')

-- Helper function to extract plain text from wiki links for semantic properties
local TemplateHelpers = require('Module:TemplateHelpers')
local function extractSemanticValue(fieldValue, fieldName)
    return TemplateHelpers.extractSemanticValue(fieldValue, fieldName, errorContext)
end

-- Get the SemanticCategoryHelpers module
local SemanticCategoryHelpers = require('Module:SemanticCategoryHelpers')

-- Override the skipProperties to ensure we handle the process_connection property
template.config.semantics = template.config.semantics or {}
template.config.semantics.skipProperties = template.config.semantics.skipProperties or {}
template.config.semantics.skipProperties[require('Module:ConfigRepository').semanticProperties.process_connection] = true

-- Add a debug preprocessor to log information about the process connections
Blueprint.addPreprocessor(template, function(template, args)
    -- Only add debug info if debug mode is enabled
    if args.debug == "true" then
        -- Create debug info
        local debugInfo = "Process: " .. tostring(args.process or "")
        debugInfo = debugInfo .. ", Previous: " .. tostring(args.has_previous or "")
        debugInfo = debugInfo .. ", Next: " .. tostring(args.has_next or "")
        
        -- Add as a non-critical error that will be visible in the template output
        ErrorHandling.addError(
            errorContext,
            "ProcessConnectionDebug",
            "Debug information for process connections",
            debugInfo,
            false -- Not critical
        )
    end
    
    return args
end)

-- Register a direct property provider for process connections
-- This approach bypasses the standard property handling and gives us full control
Blueprint.registerPropertyProvider(template, function(template, args)
    local properties = {}
    local processConnectionProperty = require('Module:ConfigRepository').semanticProperties.process_connection
    
    -- Collect all process connections
    local connections = {}
    local seen = {}
    
    -- Helper function to add a connection if valid and not duplicate
    local function addConnection(value, fieldName)
        if value and value ~= "" then
            local extractedValue = extractSemanticValue(value, fieldName)
            if extractedValue and extractedValue ~= "" and not seen[extractedValue] then
                seen[extractedValue] = true
                table.insert(connections, extractedValue)
            end
        end
    end
    
    -- Add connections from all relevant fields
    addConnection(args.process, "Process")
    addConnection(args.has_previous, "Preceded By")
    addConnection(args.has_next, "Succeeded By")
    
    -- Set the property if we have connections
    if #connections > 0 then
        properties[processConnectionProperty] = #connections == 1 and connections[1] or connections
    end
    
    -- Add debug info as a non-critical error if debug mode is enabled
    if args.debug == "true" then
        local connectionsInfo = "Total connections: " .. tostring(#connections)
        if #connections > 0 then
            connectionsInfo = connectionsInfo .. " - Values: "
            for i, conn in ipairs(connections) do
                connectionsInfo = connectionsInfo .. conn
                if i < #connections then
                    connectionsInfo = connectionsInfo .. ", "
                end
            end
        end
        
        ErrorHandling.addError(
            errorContext,
            "ProcessConnectionValues",
            "Process connection values",
            connectionsInfo,
            false -- Not critical
        )
    end
    
    return properties
end)

-- ========== Main Render Function ==========
-- Blueprint default: Main render function
function p.render(frame)
    return ErrorHandling.protect(
        errorContext,
        "render",
        function()
            return template.render(frame)
        end,
        ErrorHandling.getMessage("TEMPLATE_RENDER_ERROR"),
        frame
    )
end

return p