Module:SemanticAnnotations: Difference between revisions
// via Wikitext Extension for VSCode Tag: Reverted |
// via Wikitext Extension for VSCode |
||
| (19 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- | --[[ | ||
* Name: SemanticAnnotations | |||
* Author: Mark W. Datysgeld | |||
* Description: Primary semantic integration module for generating semantic properties with transformation support and property limits | |||
* Notes: Implements batching, deduplication and property limits (200 total, 25 per property); supports simple, object, complex, and subobject mappings; falls back to parser functions when mw.smw unavailable; includes pruning to prevent server crashes | |||
]] | |||
local p = {} | local p = {} | ||
| Line 10: | Line 14: | ||
local VALUE_LIMIT = 25 -- per individual property | local VALUE_LIMIT = 25 -- per individual property | ||
--[[ Fallback for mw.smw.set using the #set parser function. | |||
--[[ | |||
@param args - Template parameters | @param args - Template parameters | ||
@param mappings - Property mappings: {["Property"] = "param"} or complex format | @param mappings - Property mappings: {["Property"] = "param"} or complex format | ||
| Line 82: | Line 80: | ||
-- Return result or empty string | -- Return result or empty string | ||
return propertyCount > 0 and table.concat(result, "\n") or "" | return propertyCount > 0 and table.concat(result, "\n") or "" | ||
end | end | ||
| Line 117: | Line 85: | ||
local function prune(properties) | local function prune(properties) | ||
local total = 0 | local total = 0 | ||
local TemplateHelpers = require('Module:TemplateHelpers') | |||
for prop,val in pairs(properties) do | for prop,val in pairs(properties) do | ||
if type(val)=='table' then | if type(val)=='table' then | ||
-- dedup array | -- dedup array using centralized removeDuplicates function | ||
properties[prop] = TemplateHelpers.removeDuplicates(val) | |||
properties[prop] = | |||
end | end | ||
-- per-property cap | -- per-property cap | ||
| Line 137: | Line 105: | ||
-- Process simple property mapping | -- Process simple property mapping | ||
local function processSimpleMapping(properties, propertyName, value, transformFunc, defaultValue) | local function processSimpleMapping(properties, propertyName, value, transformFunc, defaultValue) | ||
-- Apply transform if applicable | |||
if value and value ~= "" and transformFunc then | if value and value ~= "" and transformFunc then value = transformFunc(value) end | ||
-- Handle value setting with array conversion if needed | |||
if value and value ~= "" then | |||
if properties[propertyName] then | |||
-- Convert to array if first duplicate | |||
if type(properties[propertyName]) ~= "table" then | |||
properties[propertyName] = {properties[propertyName]} | |||
end | |||
table.insert(properties[propertyName], value) | |||
else | |||
properties[propertyName] = value | |||
end | |||
elseif defaultValue then | elseif defaultValue then | ||
properties[propertyName] = defaultValue | |||
properties[propertyName] = | |||
end | end | ||
end | end | ||
| Line 156: | Line 129: | ||
local param = mappingEntry.param | local param = mappingEntry.param | ||
local metadata = mappingEntry.metadata or {} | local metadata = mappingEntry.metadata or {} | ||
local value = args[param] | -- Case-insensitive lookup: try exact match first, then lowercase | ||
local value = args[param] or args[param:lower()] | |||
if value and value ~= "" then | if value and value ~= "" then | ||
| Line 200: | Line 174: | ||
end | end | ||
-- Enhanced | -- Enhanced fallback for mw.smw.set with complex mapping support. | ||
function p.generateEnhancedAnnotations(args, mappings, options) | function p.generateEnhancedAnnotations(args, mappings, options) | ||
args = args or {} | args = args or {} | ||
| Line 224: | Line 198: | ||
if type(mapping) == "string" then | if type(mapping) == "string" then | ||
-- Simple string mapping | -- Simple string mapping with case-insensitive lookup | ||
local value = args[mapping] or args[mapping:lower()] | |||
propertyCount = propertyCount + addSimplePropertyToResult(result, | propertyCount = propertyCount + addSimplePropertyToResult(result, | ||
fullPropertyName, | fullPropertyName, value, transform[property], default[property]) | ||
elseif type(mapping) == "table" then | elseif type(mapping) == "table" then | ||
if mapping.param then | if mapping.param then | ||
-- Object with param structure | -- Object with param structure with case-insensitive lookup | ||
local value = args[mapping.param] or args[mapping.param:lower()] | |||
propertyCount = propertyCount + addSimplePropertyToResult(result, | propertyCount = propertyCount + addSimplePropertyToResult(result, | ||
fullPropertyName, | fullPropertyName, value, transform[property], default[property]) | ||
elseif mapping.mappings then | elseif mapping.mappings then | ||
-- Complex mapping with multiple parameters | -- Complex mapping with multiple parameters | ||
| Line 237: | Line 213: | ||
local param = mappingEntry.param | local param = mappingEntry.param | ||
local metadata = mappingEntry.metadata or {} | local metadata = mappingEntry.metadata or {} | ||
local value = args[param] | -- Case-insensitive lookup | ||
local value = args[param] or args[param:lower()] | |||
if value and value ~= "" then | if value and value ~= "" then | ||
| Line 280: | Line 257: | ||
end | end | ||
--[[ Sets semantic properties | --[[ Sets semantic properties using the native mw.smw API. | ||
This is the primary function for setting semantic data. | |||
@param args - Template parameters | @param args - Template parameters | ||
@param mappings - Property mappings in formats: | @param mappings - Property mappings in formats: | ||
| Line 306: | Line 284: | ||
if type(mapping) == "string" then | if type(mapping) == "string" then | ||
-- Simple string mapping | -- Simple string mapping with case-insensitive lookup | ||
processSimpleMapping(properties, fullPropertyName, | -- Try exact match first, then lowercase | ||
local value = args[mapping] or args[mapping:lower()] | |||
processSimpleMapping(properties, fullPropertyName, value, transform[property], default[property]) | |||
elseif type(mapping) == "table" then | elseif type(mapping) == "table" then | ||
if mapping.is_subobject then | if mapping.is_subobject then | ||
| Line 364: | Line 344: | ||
end | end | ||
elseif mapping.param then | elseif mapping.param then | ||
-- Single mapping with object structure | -- Single mapping with object structure with case-insensitive lookup | ||
processSimpleMapping(properties, fullPropertyName, | local value = args[mapping.param] or args[mapping.param:lower()] | ||
processSimpleMapping(properties, fullPropertyName, value, transform[property], default[property]) | |||
elseif mapping.mappings then | elseif mapping.mappings then | ||
-- Complex mapping with multiple parameters | -- Complex mapping with multiple parameters | ||