Module:SemanticAnnotations: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 4: | Line 4: | ||
local p = {} | local p = {} | ||
local NormalizationText = require('Module:NormalizationText') | local NormalizationText = require('Module:NormalizationText') | ||
-- Limits to prevent server crashes | |||
local TOTAL_LIMIT = 200 -- per page render, tune later | |||
local VALUE_LIMIT = 25 -- per individual property | |||
-- Trim whitespace helper | -- Trim whitespace helper | ||
| Line 106: | Line 110: | ||
} | } | ||
return p.generateAnnotations(parentArgs, mappings, options) | return p.generateAnnotations(parentArgs, mappings, options) | ||
end | |||
-- Prune properties to prevent server crashes | |||
local function prune(properties) | |||
local total = 0 | |||
for prop,val in pairs(properties) do | |||
if type(val)=='table' then | |||
-- dedup array | |||
local seen, compact = {}, {} | |||
for _,v in ipairs(val) do if not seen[v] then seen[v]=true; compact[#compact+1]=v end end | |||
properties[prop] = compact | |||
end | |||
-- per-property cap | |||
if type(properties[prop])=='table' and #properties[prop] > VALUE_LIMIT then | |||
properties[prop] = { unpack(properties[prop],1,VALUE_LIMIT) } | |||
end | |||
-- global counter | |||
total = total + (type(properties[prop])=='table' and #properties[prop] or 1) | |||
end | |||
if total > TOTAL_LIMIT then return nil, "SMW limit hit: "..total end | |||
return properties | |||
end | end | ||
| Line 349: | Line 374: | ||
end | end | ||
end | end | ||
end | |||
-- Before setting properties, prune them | |||
local pruned, err = prune(properties) | |||
if not pruned then | |||
-- Add HTML comment with error message | |||
semanticOutput = semanticOutput .. "\n<!-- " .. err .. " -->" | |||
return semanticOutput | |||
end | end | ||
-- Set properties if any exist | -- Set properties if any exist | ||
if next( | if next(pruned) then | ||
local success, result = pcall(function() return mw.smw.set( | local success, result = pcall(function() return mw.smw.set(pruned) end) | ||
if success then return semanticOutput | if success then return semanticOutput | ||
else return p.generateEnhancedAnnotations(args, mappings, options) .. semanticOutput | else return p.generateEnhancedAnnotations(args, mappings, options) .. semanticOutput | ||