Модуль:GetField: различия между версиями

мНет описания правки
Нет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}


local cache = {}
local entryCache = {}
local BASE_USER = "IanComradeBot/"
local BASE_USER = "IanComradeBot/"


Строка 10: Строка 8:


local function load_cached_data(moduleName)
local function load_cached_data(moduleName)
    local data = cache[moduleName]
    if data then
        return data
    end
     local ok, loaded = pcall(mw.loadData, moduleName)
     local ok, loaded = pcall(mw.loadData, moduleName)
     if not ok or not loaded then
     if not ok or not loaded then
         return nil
         return nil
     end
     end
    cache[moduleName] = loaded
     return loaded
     return loaded
end
end
Строка 210: Строка 201:


     return entry[string.upper(first) .. tail]
     return entry[string.upper(first) .. tail]
end
local function flatten_entry(entry)
    if type(entry) ~= "table" then return "" end
    local parts = {}
    local function append_table_json(key, value)
        local ok, json = pcall(mw.text.jsonEncode, value)
        if ok and json then
            parts[#parts + 1] = key .. "=" .. to_nowiki(json)
        end
    end
    local function walk(tbl, prefix)
        local keys = {}
        for k in pairs(tbl) do keys[#keys + 1] = k end
        table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
        for _, k in ipairs(keys) do
            local v = tbl[k]
            local kStr = tostring(k)
            local key = (prefix == "" and kStr or prefix .. "." .. kStr)
            if type(v) == "table" then
                if next(v) == nil then
                else
                    append_table_json(key, v)
                    if not is_array(v) then
                        walk(v, key)
                    end
                end
            else
                parts[#parts + 1] = key .. "=" .. tostring(v)
            end
        end
    end
    walk(entry, "")
    return table.concat(parts, "|")
end
end


Строка 266: Строка 295:


     local entry = resolve_entry(data, id) or {}
     local entry = resolve_entry(data, id) or {}
 
     return flatten_entry(entry)
     if type(entry) ~= "table" then return "" end
 
    local parts = {}
    local function append_table_json(key, value)
        local ok, json = pcall(mw.text.jsonEncode, value)
        if ok and json then
            parts[#parts + 1] = key .. "=" .. to_nowiki(json)
        end
    end
 
    local function walk(tbl, prefix)
        local keys = {}
        for k in pairs(tbl) do keys[#keys + 1] = k end
        table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
        for _, k in ipairs(keys) do
            local v = tbl[k]
            local kStr = tostring(k)
            local key = (prefix == "" and kStr or prefix .. "." .. kStr)
            if type(v) == "table" then
                if next(v) == nil then
                else
                    append_table_json(key, v)
                    if not is_array(v) then
                        walk(v, key)
                    end
                end
            else
                parts[#parts + 1] = key .. "=" .. tostring(v)
            end
        end
    end
 
    walk(entry, "")
 
    return table.concat(parts, "|")
end
end


Строка 316: Строка 310:
     if not data then return "" end
     if not data then return "" end


    local entryKey = moduleName .. "|" .. (id ~= "" and id or "default")
     local entry = resolve_entry(data, id)
     local entry = entryCache[entryKey]
    if not entry then
        entry = resolve_entry(data, id)
        entryCache[entryKey] = entry
    end
     if entry == nil then return "" end
     if entry == nil then return "" end


Строка 393: Строка 382:
     local out = {}
     local out = {}
     for _, idKey in ipairs(matches) do
     for _, idKey in ipairs(matches) do
         local tpl = p.getTpl({ args = { idKey, pagePath, tplPath } })
         local tpl = p.getTpl({ args = { idKey, pagePath, tplPath }, data = data })
         if tpl ~= "" then
         if tpl ~= "" then
             out[#out + 1] = tpl
             out[#out + 1] = tpl
Строка 417: Строка 406:
     end
     end


     local extra = p.flattenField({ args = { id, pagePath } }) or ""
     local moduleName = get_module_name(pagePath)
    local data = frame.data
    if not data then
        data = load_cached_data(moduleName)
    end
    if not data then
        return ""
    end
 
    local entry = resolve_entry(data, id)
    local extra = flatten_entry(entry)
     local tplStr = "{{" .. tostring(tplPath) .. "|id=" .. tostring(id)
     local tplStr = "{{" .. tostring(tplPath) .. "|id=" .. tostring(id)
     if extra ~= "" then
     if extra ~= "" then
Строка 447: Строка 446:
     local ok, ids = pcall(mw.text.jsonDecode, idsJson or "")
     local ok, ids = pcall(mw.text.jsonDecode, idsJson or "")
     if not ok or type(ids) ~= "table" or #ids == 0 then
     if not ok or type(ids) ~= "table" or #ids == 0 then
        return ""
    end
    local moduleName = get_module_name(pagePath)
    local data = load_cached_data(moduleName)
    if not data then
         return ""
         return ""
     end
     end
Строка 452: Строка 457:
     local out = {}
     local out = {}
     for _, id in ipairs(ids) do
     for _, id in ipairs(ids) do
         local tpl = p.getTpl({ args = { id, pagePath, tplPath } })
         local tpl = p.getTpl({ args = { id, pagePath, tplPath }, data = data })
         if tpl ~= "" then
         if tpl ~= "" then
             out[#out + 1] = tpl
             out[#out + 1] = tpl
Строка 580: Строка 585:


     for idKey in pairs(idsTable) do
     for idKey in pairs(idsTable) do
         local tpl = p.getTpl({ args = { idKey, pagePath, tplPath } })
         local tpl = p.getTpl({ args = { idKey, pagePath, tplPath }, data = data })
         if tpl ~= "" then
         if tpl ~= "" then
             out[#out + 1] = tpl
             out[#out + 1] = tpl