Модуль:Сущность/data: различия между версиями

Нет описания правки
мНет описания правки
Строка 6: Строка 6:
local dp = dpOk and dpModule or nil
local dp = dpOk and dpModule or nil


local moduleDataCache = {}
local templateContentCache = {}
local templateMetaCache = {}
local templateArgCache = {}
local flattenExtraCache = {}
local switchModeRegistry = {}
local switchModeRegistry = {}
local switchModeOrder = {}
local switchModeOrder = {}
Строка 33: Строка 28:
local function load_module_data(page)
local function load_module_data(page)
     local moduleName = JsonPaths.get(page)
     local moduleName = JsonPaths.get(page)
    if moduleDataCache[moduleName] ~= nil then
        return moduleDataCache[moduleName]
    end
     local ok, data = pcall(mw.loadData, moduleName)
     local ok, data = pcall(mw.loadData, moduleName)
     if not ok then
     if not ok then
        moduleDataCache[moduleName] = nil
         return nil
         return nil
     end
     end
    moduleDataCache[moduleName] = data
     return data
     return data
end
end


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
     if not title then
        templateContentCache[path] = false
         return nil
         return nil
     end
     end
     local ok, content = pcall(title.getContent, title)
     local ok, content = pcall(title.getContent, title)
     if not ok then
     if not ok then
        templateContentCache[path] = false
         return nil
         return nil
     end
     end
    templateContentCache[path] = content or false
     return content
     return content
end
end
Строка 111: Строка 90:


local function get_template_params(tplPath, content)
local function get_template_params(tplPath, content)
     local cached = templateArgCache[tplPath]
     return collect_template_params(content)
    if cached ~= nil then
        return cached
    end
 
    local params = collect_template_params(content)
    templateArgCache[tplPath] = params
    return params
end
end


Строка 507: Строка 479:


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,
Строка 518: Строка 486:
     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
Строка 538: Строка 505:
     end
     end


    templateMetaCache[tplPath] = data
     return data
     return data
end
end
Строка 676: Строка 642:
     end
     end


     local cacheKey = dataPage .. "\31" .. id .. "\31" .. table.concat(paramNames, "\30")
     return dp.flattenFieldSelectiveDirect(id, dataPage, paramNames) or ""
    if flattenExtraCache[cacheKey] ~= nil then
        return flattenExtraCache[cacheKey]
    end
 
    local extra = dp.flattenFieldSelectiveDirect(id, dataPage, paramNames) or ""
    flattenExtraCache[cacheKey] = extra
    return extra
end
end


Строка 795: Строка 754:
         processEntity("prototype", protoName, false)
         processEntity("prototype", protoName, false)
     end
     end
    local compWhitelistSet = parse_csv_set(componentWhitelist)
    local compBlacklistSet = parse_csv_set(componentBlacklist)
    local protoWhitelistSet = parse_csv_set(prototypeWhitelist)
    local protoBlacklistSet = parse_csv_set(prototypeBlacklist)
    local compHasWhitelist = next(compWhitelistSet) ~= nil
    local protoHasWhitelist = next(protoWhitelistSet) ~= nil


     local componentStoreDefs = load_module_data("component_store.json")
     local componentStoreDefs = load_module_data("component_store.json")
Строка 801: Строка 768:
         if type(compStore) == "table" then
         if type(compStore) == "table" then
             for compName in pairs(compStore) do
             for compName in pairs(compStore) do
                 processEntity("component", compName, true)
                 local allowed = true
                if compBlacklistSet[compName] then
                    allowed = false
                elseif compHasWhitelist and not compWhitelistSet[compName] then
                    allowed = false
                end
                if allowed then
                    processEntity("component", compName, true)
                end
             end
             end
         end
         end
Строка 810: Строка 785:
         if type(protoStore) == "table" then
         if type(protoStore) == "table" then
             for protoName in pairs(protoStore) do
             for protoName in pairs(protoStore) do
                 processEntity("prototype", protoName, true)
                 local allowed = true
                if protoBlacklistSet[protoName] then
                    allowed = false
                elseif protoHasWhitelist and not protoWhitelistSet[protoName] then
                    allowed = false
                end
                if allowed then
                    processEntity("prototype", protoName, true)
                end
             end
             end
         end
         end