Module:LuaTemplateBlueprint: Difference between revisions
// via Wikitext Extension for VSCode Tag: Manual revert |
MAJOR REVISION: BETTER PROPERTY HANDLING AND LIMITING // via Wikitext Extension for VSCode Tag: Reverted |
||
| Line 901: | Line 901: | ||
transform = transforms | transform = transforms | ||
} | } | ||
-- CRITICAL: Early bounds checking | |||
local PROPERTY_LIMIT = 150 -- Conservative limit below SMW's 200 | |||
local VALUE_LIMIT = 20 -- Per-property value limit below SMW's 25 | |||
local propertyCount = 0 | |||
local valueCounts = {} | |||
-- Helper function to check if we can add more properties | |||
local function canAddProperty(propertyName, valueCount) | |||
valueCount = valueCount or 1 | |||
-- Check total property limit | |||
if propertyCount >= PROPERTY_LIMIT then | |||
return false, "Property limit reached (" .. PROPERTY_LIMIT .. ")" | |||
end | |||
-- Check per-property value limit | |||
local currentCount = valueCounts[propertyName] or 0 | |||
if currentCount + valueCount > VALUE_LIMIT then | |||
return false, "Value limit reached for " .. propertyName .. " (" .. VALUE_LIMIT .. ")" | |||
end | |||
return true | |||
end | |||
-- Helper function to safely add a property | |||
local function safeAddProperty(allProps, propName, value) | |||
if not value or value == '' then return end | |||
-- Count values to add | |||
local valuesToAdd = 1 | |||
if type(value) == "table" then | |||
valuesToAdd = #value | |||
end | |||
-- Check if we can add | |||
local canAdd, reason = canAddProperty(propName, valuesToAdd) | |||
if not canAdd then | |||
-- Log warning but don't crash | |||
if template._errorContext then | |||
ErrorHandling.addError( | |||
template._errorContext, | |||
'SemanticPropertyLimit', | |||
reason, | |||
propName, | |||
false | |||
) | |||
end | |||
return | |||
end | |||
-- Add the property | |||
if allProps[propName] then | |||
-- Convert to array if needed | |||
if type(allProps[propName]) ~= "table" then | |||
allProps[propName] = {allProps[propName]} | |||
valueCounts[propName] = 1 | |||
end | |||
-- Add new values with limit checking | |||
if type(value) == "table" then | |||
for _, v in ipairs(value) do | |||
if valueCounts[propName] < VALUE_LIMIT then | |||
table.insert(allProps[propName], v) | |||
valueCounts[propName] = valueCounts[propName] + 1 | |||
end | |||
end | |||
else | |||
if valueCounts[propName] < VALUE_LIMIT then | |||
table.insert(allProps[propName], value) | |||
valueCounts[propName] = valueCounts[propName] + 1 | |||
end | |||
end | |||
else | |||
-- New property | |||
allProps[propName] = value | |||
propertyCount = propertyCount + 1 | |||
valueCounts[propName] = type(value) == "table" and #value or 1 | |||
end | |||
end | |||
-- Collect all properties in a single batch for complete deduplication | -- Collect all properties in a single batch for complete deduplication | ||
| Line 908: | Line 988: | ||
for property, param in pairs(properties) do | for property, param in pairs(properties) do | ||
-- Perform case-insensitive lookup for the parameter key | -- Perform case-insensitive lookup for the parameter key | ||
local keyName, | local keyName, value = TemplateHelpers.getFieldValue(args, { key = param }) | ||
allProperties | if value and value ~= '' then | ||
safeAddProperty(allProperties, property, value) | |||
end | |||
end | end | ||
| Line 922: | Line 1,004: | ||
local transformed = applyTransform(rawValue, property, transform, args, template) | local transformed = applyTransform(rawValue, property, transform, args, template) | ||
if transformed and transformed ~= '' then | if transformed and transformed ~= '' then | ||
safeAddProperty(allProperties, property, transformed) | |||
end | end | ||
end | end | ||
| Line 965: | Line 1,029: | ||
-- Skip if explicitly marked to skip (double-check) | -- Skip if explicitly marked to skip (double-check) | ||
if not skipProperties[property] then | if not skipProperties[property] then | ||
safeAddProperty(allProperties, property, values) | |||
end | end | ||
end | end | ||
| Line 1,010: | Line 1,052: | ||
-- Skip properties marked to skip | -- Skip properties marked to skip | ||
if not skipProperties[property] then | if not skipProperties[property] then | ||
safeAddProperty(allProperties, property, value) | |||
end | end | ||
end | end | ||
| Line 1,088: | Line 1,068: | ||
]] | ]] | ||
-- Override raw country/region with normalized names | -- Override raw country/region with normalized names | ||
local cr = require('Module:ConfigRepository') | if args.country and args.country ~= '' then | ||
local cr = require('Module:ConfigRepository') | |||
local cd = require('Module:CountryData') | |||
local norm = p.protectedExecute( | |||
template, | |||
'CountryData_Override', | |||
function() return cd.getSemanticCountryRegionProperties(args.country) end, | |||
{}, | |||
args.country | |||
) | |||
local countryKey = cr.semanticProperties.country | |||
local regionKey = cr.semanticProperties.region | |||
-- Use safe property addition for the override | |||
if norm[countryKey] then | |||
-- Remove existing values first to ensure clean override | |||
allProperties[countryKey] = nil | |||
valueCounts[countryKey] = nil | |||
propertyCount = propertyCount - 1 | |||
safeAddProperty(allProperties, countryKey, norm[countryKey]) | |||
end | |||
if norm[regionKey] then | |||
-- Remove existing values first to ensure clean override | |||
allProperties[regionKey] = nil | |||
valueCounts[regionKey] = nil | |||
propertyCount = propertyCount - 1 | |||
safeAddProperty(allProperties, regionKey, norm[regionKey]) | |||
end | |||
end | |||
local semanticOutput = SemanticAnnotations.setSemanticProperties( | local semanticOutput = SemanticAnnotations.setSemanticProperties( | ||
args, | args, | ||