Module:TemplateStarter: Difference between revisions

// via Wikitext Extension for VSCode
Maintenance update // via Wikitext Extension for VSCode
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:TemplateStarter
--[[
-- Generates configurable template structures for new pages using ConfigRepository
* Name: TemplateStarter
* Author: Mark W. Datysgeld
* Description: Generates configurable template structures for new pages using ConfigRepository with support for variants and placeholder replacement
* Notes: Template generation from ConfigRepository configs; support for campaign variants; placeholder replacement in boilerplate text; creator field management; caching of template lists; includes error handling and validation
]]


local p = {}
local p = {}
Line 148: Line 152:
      
      
     templateType = mw.text.trim(tostring(templateType))
     templateType = mw.text.trim(tostring(templateType))
   
    -- Handle "None" special case
    if templateType == "None" then
        return ""
    end
      
      
     -- Parse template to check if it's a variant
     -- Parse template to check if it's a variant
Line 190: Line 199:
end
end


-- Get list of available templates including variants
-- Get list of available templates including campaign combinations
function p.getAvailableTemplatesWithVariants()
function p.getAvailableTemplatesWithVariants()
     -- Get the filtered list of base templates that are allowed for page creation
     -- Get the filtered list of base templates that are allowed for page creation
     local baseTemplates = p.getAvailableTemplates()
     local baseTemplates = p.getAvailableTemplates()
     local templatesWithVariants = {}
     local templatesWithCampaigns = {}
      
      
     -- Use a map to ensure uniqueness before adding to the final list
     -- Use a map to ensure uniqueness before adding to the final list
Line 204: Line 213:
     end
     end
      
      
     -- Iterate through the allowed base templates to find their variants
     -- Iterate through campaigns to find applicable templates
     for _, templateName in ipairs(baseTemplates) do
     if ConfigRepository.campaigns then
        local config = ConfigRepository.getConfig(templateName)
        for campaignId, campaign in pairs(ConfigRepository.campaigns) do
        if config and config.variants then
            if campaign.applicable_templates then
            for _, variant in pairs(config.variants) do
                for _, templateName in ipairs(campaign.applicable_templates) do
                -- A variant is included if it's active and not explicitly excluded
                    -- Only include if the base template is allowed for page creation
                if variant.state and variant.name and (not variant.meta or variant.meta.page_creator ~= false) then
                    if templateMap[templateName] then
                    templateMap[variant.name] = true
                        local campaignTemplateName = templateName .. " (" .. campaign.name .. ")"
                        templateMap[campaignTemplateName] = true
                    end
                 end
                 end
             end
             end
Line 220: Line 231:
     local index = 1
     local index = 1
     for templateName, _ in pairs(templateMap) do
     for templateName, _ in pairs(templateMap) do
         templatesWithVariants[index] = templateName
         templatesWithCampaigns[index] = templateName
         index = index + 1
         index = index + 1
     end
     end
      
      
     -- Sort the final combined list
     -- Sort the final combined list
     table.sort(templatesWithVariants)
     table.sort(templatesWithCampaigns)
      
      
     return templatesWithVariants
    -- Add "None" option at the beginning
    table.insert(templatesWithCampaigns, 1, "None")
   
     return templatesWithCampaigns
end
end


-- Parse variant template name to get base template and variant info
-- Parse campaign template name to get base template and campaign info
function p.parseVariantTemplate(templateName)
function p.parseVariantTemplate(templateName)
     if not templateName or templateName == "" then
     if not templateName or templateName == "" or templateName == "None" then
         return nil
         return nil
     end
     end
      
      
     -- Check if this is a variant by looking for the pattern in all templates
     -- Check if this is a campaign combination by looking for the pattern "Template (Campaign Name)"
     if ConfigRepository.templates then
     if ConfigRepository.campaigns then
         for baseTemplateName, config in pairs(ConfigRepository.templates) do
         for campaignId, campaign in pairs(ConfigRepository.campaigns) do
             if config.variants then
             local expectedName = " (" .. campaign.name .. ")"
                for variantKey, variant in pairs(config.variants) do
            if templateName:sub(-#expectedName) == expectedName then
                    if variant.state and variant.name == templateName then
                local baseTemplateName = templateName:sub(1, -#expectedName - 1)
                        return {
                -- Verify this is a valid combination
                            baseTemplate = baseTemplateName,
                if campaign.applicable_templates then
                            variantKey = variantKey,
                    for _, applicableTemplate in ipairs(campaign.applicable_templates) do
                            variant = variant
                        if applicableTemplate == baseTemplateName then
                         }
                            return {
                                baseTemplate = baseTemplateName,
                                variantKey = campaignId,
                                variant = {
                                    name = templateName,
                                    campaign_template = campaign.json_config
                                }
                            }
                         end
                     end
                     end
                 end
                 end
Line 253: Line 275:
     end
     end
      
      
     -- Not a variant, return as base template
     -- Not a campaign combination, return as base template
     return {
     return {
         baseTemplate = templateName,
         baseTemplate = templateName,
Line 311: Line 333:
-- Get creator fields for a specific template type
-- Get creator fields for a specific template type
function p.getCreatorFields(templateType)
function p.getCreatorFields(templateType)
     if not templateType or templateType == "" then
     if not templateType or templateType == "" or templateType == "None" then
         return {}
         return {}
     end
     end