Модуль:Сущность/data: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 4: | Строка 4: | ||
local moduleDataCache = {} | local moduleDataCache = {} | ||
local templateContentCache = {} | |||
local templateMetaCache = {} | |||
local templateArgCache = {} | |||
local flattenExtraCache = {} | |||
local switchModeRegistry = {} | local switchModeRegistry = {} | ||
local switchModeOrder = {} | local switchModeOrder = {} | ||
| Строка 30: | Строка 34: | ||
local function load_template_content(path) | local function load_template_content(path) | ||
if templateContentCache[path] ~= nil then | |||
return templateContentCache[path] or nil | |||
end | |||
local title = mw.title.new("Template:" .. path) | local title = mw.title.new("Template:" .. path) | ||
if not title then return nil end | if not title then | ||
templateContentCache[path] = false | |||
return nil | |||
end | |||
local ok, content = pcall(function() return title:getContent() end) | local ok, content = pcall(function() return title:getContent() end) | ||
if not ok then return nil end | if not ok then | ||
templateContentCache[path] = false | |||
return nil | |||
end | |||
templateContentCache[path] = content or false | |||
return content | return content | ||
end | end | ||
| Строка 48: | Строка 64: | ||
tplStr = tplStr .. "}}" | tplStr = tplStr .. "}}" | ||
return tplStr | return tplStr | ||
end | |||
local function add_template_param(params, seen, raw) | |||
local param = trim(raw or "") | |||
if param == "" or param == "id" or param:match("^%d+$") then | |||
return | |||
end | |||
if not seen[param] then | |||
seen[param] = true | |||
params[#params + 1] = param | |||
end | |||
end | |||
local function collect_template_params(content) | |||
local params = {} | |||
local seen = {} | |||
if not content or content == "" then | |||
return params | |||
end | |||
for param in content:gmatch("{{{%s*([^|}]+)%s*|") do | |||
add_template_param(params, seen, param) | |||
end | |||
for param in content:gmatch("{{{%s*([^|}]+)%s*}}") do | |||
add_template_param(params, seen, param) | |||
end | |||
return params | |||
end | |||
local function get_template_params(tplPath, content) | |||
local cached = templateArgCache[tplPath] | |||
if cached ~= nil then | |||
return cached | |||
end | |||
local params = collect_template_params(content) | |||
templateArgCache[tplPath] = params | |||
return params | |||
end | end | ||
| Строка 333: | Строка 389: | ||
local function getTemplateMeta(frame, tplPath) | local function getTemplateMeta(frame, tplPath) | ||
if templateMetaCache[tplPath] ~= nil then | |||
return templateMetaCache[tplPath] or "" | |||
end | |||
local expanded = frame:expandTemplate { | local expanded = frame:expandTemplate { | ||
title = tplPath, | title = tplPath, | ||
| Строка 340: | Строка 400: | ||
local ok, data = pcall(mw.text.jsonDecode, expanded) | local ok, data = pcall(mw.text.jsonDecode, expanded) | ||
if not ok or type(data) ~= "table" then | if not ok or type(data) ~= "table" then | ||
templateMetaCache[tplPath] = false | |||
return "" | return "" | ||
end | end | ||
| Строка 359: | Строка 420: | ||
end | end | ||
templateMetaCache[tplPath] = data | |||
return data | return data | ||
end | end | ||
| Строка 451: | Строка 513: | ||
end | end | ||
return basePriority | return basePriority | ||
end | |||
local function get_selective_extra(dp, id, dataPage, paramNames) | |||
if not dp or type(dp.flattenFieldSelective) ~= "function" then | |||
return "" | |||
end | |||
if type(paramNames) ~= "table" or #paramNames == 0 then | |||
return "" | |||
end | |||
local okJson, keysJson = pcall(mw.text.jsonEncode, paramNames) | |||
if not okJson or not keysJson or keysJson == "" then | |||
return "" | |||
end | |||
local cacheKey = dataPage .. "\31" .. id .. "\31" .. keysJson | |||
if flattenExtraCache[cacheKey] ~= nil then | |||
return flattenExtraCache[cacheKey] | |||
end | |||
local extra = dp.flattenFieldSelective({ args = { id, dataPage, keysJson } }) or "" | |||
flattenExtraCache[cacheKey] = extra | |||
return extra | |||
end | end | ||
| Строка 530: | Строка 615: | ||
local componentDefs = load_module_data("component.json") | local componentDefs = load_module_data("component.json") | ||
local prototypeStoreDefs = load_module_data("prototype_store.json") | local prototypeStoreDefs = load_module_data("prototype_store.json") | ||
if not componentDefs or not prototypeStoreDefs then return "" end | |||
if not componentDefs or not prototypeStoreDefs | |||
local foundComponents, foundPrototypes = collect_entity_sets(id, componentDefs, prototypeStoreDefs, ignoreComponents, | local foundComponents, foundPrototypes = collect_entity_sets(id, componentDefs, prototypeStoreDefs, ignoreComponents, | ||
| Строка 562: | Строка 646: | ||
local extra = "" | local extra = "" | ||
if ok and dp | local paramNames = get_template_params(tplPath, content) | ||
if ok and dp then | |||
local dataPage = tplPath .. ".json" | local dataPage = tplPath .. ".json" | ||
extra = dp | extra = get_selective_extra(dp, id, dataPage, paramNames) | ||
end | end | ||
| Строка 583: | Строка 668: | ||
end | end | ||
local componentStoreDefs = load_module_data("component_store.json") | |||
if type(componentStoreDefs) == "table" then | if type(componentStoreDefs) == "table" then | ||
local compStore = componentStoreDefs[id] | local compStore = componentStoreDefs[id] | ||