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

Нет описания правки
Нет описания правки
Строка 3: Строка 3:
local cache = {}
local cache = {}
local entryCache = {}
local entryCache = {}
local BASE_USER = "IanComradeBot/"
local function get_module_name(pagePath)
    return "Module:" .. BASE_USER .. pagePath .. "/data"
end
local function load_cached_data(moduleName)
    local data = cache[moduleName]
    if data then
        return data
    end
    local ok, loaded = pcall(mw.loadData, moduleName)
    if not ok or not loaded then
        return nil
    end
    cache[moduleName] = loaded
    return loaded
end


local function parse_indexed_part(part)
local function parse_indexed_part(part)
Строка 133: Строка 153:
     end
     end
     return nil
     return nil
end
local function contains_target(v, target)
    if type(v) == "table" then
        if is_array(v) then
            for _, item in ipairs(v) do
                if tostring(item) == target then
                    return true
                end
            end
            return false
        end
        for _, item in pairs(v) do
            if tostring(item) == target then
                return true
            end
        end
        return false
    end
    return tostring(v) == target
end
local function find_matching_ids(idsTable, keyPath, searchValue)
    local target = tostring(searchValue)
    local matches = {}
    for idKey, entry in pairs(idsTable) do
        if type(entry) == "table" then
            local v = get_by_path(entry, keyPath)
            if v ~= nil and contains_target(v, target) then
                matches[#matches + 1] = idKey
            end
        end
    end
    return matches
end
local function preprocess_or_return(frame, text)
    if type(frame.preprocess) == "function" then
        return frame:preprocess(text)
    end
    return text
end
end


Строка 148: Строка 213:
     end
     end


     local baseUser = "IanComradeBot/"
     local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
    local storeName
     local moduleName = get_module_name(storeName)
    if kind == "prototype" then
     local data = load_cached_data(moduleName)
        storeName = "prototype_store.json"
    else
        storeName = "component_store.json"
    end
     local moduleName = "Module:" .. baseUser .. storeName .. "/data"
 
     local data = cache[moduleName]
     if not data then
     if not data then
         local ok, loaded = pcall(mw.loadData, moduleName)
         return ""
        if not ok or not loaded then
            return ""
        end
        data = loaded
        cache[moduleName] = data
     end
     end


Строка 196: Строка 249:
     if id == "" or pagePath == "" then return "" end
     if id == "" or pagePath == "" then return "" end


    local baseUser = "IanComradeBot/"
     local moduleName = get_module_name(pagePath)
     local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
     local data = load_cached_data(moduleName)
 
     if not data then return "" end
     local data = cache[moduleName]
     if not data then
        local ok, loaded = pcall(mw.loadData, moduleName)
        if not ok or not loaded then return "" end
        data = loaded
        cache[moduleName] = data
    end


     local entry = resolve_entry(data, id) or {}
     local entry = resolve_entry(data, id) or {}
Строка 219: Строка 265:
             local v = tbl[k]
             local v = tbl[k]
             local key = (prefix == "" and tostring(k) or prefix .. "." .. tostring(k))
             local key = (prefix == "" and tostring(k) or prefix .. "." .. tostring(k))
 
             if type(v) == "table" then
            local okJson, json = pcall(mw.text.jsonEncode, v)
                if next(v) == nil then
             if okJson and json == "null" then
                elseif is_array(v) then
                parts[#parts + 1] = key .. "=null"
                    local ok, json = pcall(mw.text.jsonEncode, v)
            else
                    if ok and json then
                if type(v) == "table" then
                         parts[#parts + 1] = key .. "=" .. to_nowiki(json)
                    if next(v) == nil then
                    elseif is_array(v) then
                        local ok, arrJson = pcall(mw.text.jsonEncode, v)
                        if ok and arrJson then
                            parts[#parts + 1] = key .. "=" .. to_nowiki(arrJson)
                        end
                    else
                         local ok, objJson = pcall(mw.text.jsonEncode, v)
                        if ok and objJson then
                            parts[#parts + 1] = key .. "=" .. to_nowiki(objJson)
                        end
                        walk(v, key)
                     end
                     end
                 else
                 else
                     local fv = format_value(v)
                     local ok, json = pcall(mw.text.jsonEncode, v)
                     if fv ~= "" then
                     if ok and json then
                         parts[#parts + 1] = key .. "=" .. fv
                         parts[#parts + 1] = key .. "=" .. to_nowiki(json)
                     end
                     end
                    walk(v, key)
                 end
                 end
            else
                parts[#parts + 1] = key .. "=" .. tostring(v)
             end
             end
         end
         end
Строка 261: Строка 298:
     if pagePath == "" then return "" end
     if pagePath == "" then return "" end


    local baseUser = "IanComradeBot/"
     local moduleName = get_module_name(pagePath)
     local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
     local data = load_cached_data(moduleName)
 
     if not data then return "" end
     local data = cache[moduleName]
     if not data then
        local ok, loaded = pcall(mw.loadData, moduleName)
        if not ok or not loaded then return "" end
        data = loaded
        cache[moduleName] = data
    end


     local entryKey = moduleName .. "|" .. (id ~= "" and id or "default")
     local entryKey = moduleName .. "|" .. (id ~= "" and id or "default")
Строка 298: Строка 328:
     end
     end


    local baseUser = "IanComradeBot/"
     local moduleName = get_module_name(pagePath)
     local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
     local data = load_cached_data(moduleName)
 
     if not data then return "[]" end
     local data = cache[moduleName]
     if not data then
        local ok, loaded = pcall(mw.loadData, moduleName)
        if not ok or not loaded then return "[]" end
        data = loaded
        cache[moduleName] = data
    end


     local idsTable = data.id
     local idsTable = data.id
Строка 314: Строка 337:
     end
     end


    local target = tostring(searchValue)
     local matches = find_matching_ids(idsTable, keyPath, searchValue)
     local matches = {}
 
    for idKey, entry in pairs(idsTable) do
        if type(entry) == "table" then
            local v = get_by_path(entry, keyPath)
            if v ~= nil then
                if type(v) == "table" then
                    if is_array(v) then
                        for _, item in ipairs(v) do
                            if format_value(item) == target then
                                matches[#matches + 1] = idKey
                                break
                            end
                        end
                    else
                        for _, item in pairs(v) do
                            if format_value(item) == target then
                                matches[#matches + 1] = idKey
                                break
                            end
                        end
                    end
                else
                    if format_value(v) == target then
                        matches[#matches + 1] = idKey
                    end
                end
            end
        end
    end


     if #matches == 0 then
     if #matches == 0 then
Строка 369: Строка 362:
     end
     end


    local baseUser = "IanComradeBot/"
     local moduleName = get_module_name(pagePath)
     local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
     local data = load_cached_data(moduleName)
 
     if not data then return "" end
     local data = cache[moduleName]
     if not data then
        local ok, loaded = pcall(mw.loadData, moduleName)
        if not ok or not loaded then return "" end
        data = loaded
        cache[moduleName] = data
    end


     local idsTable = data.id
     local idsTable = data.id
Строка 385: Строка 371:
     end
     end


    local target = tostring(searchValue)
     local matches = find_matching_ids(idsTable, keyPath, searchValue)
     local matches = {}
 
    for idKey, entry in pairs(idsTable) do
        if type(entry) == "table" then
            local v = get_by_path(entry, keyPath)
            if v ~= nil then
                if type(v) == "table" then
                    if is_array(v) then
                        for _, item in ipairs(v) do
                            if format_value(item) == target then
                                matches[#matches + 1] = idKey
                                break
                            end
                        end
                    else
                        for _, item in pairs(v) do
                            if format_value(item) == target then
                                matches[#matches + 1] = idKey
                                break
                            end
                        end
                    end
                else
                    if format_value(v) == target then
                        matches[#matches + 1] = idKey
                    end
                end
            end
        end
    end


     if #matches == 0 then
     if #matches == 0 then
Строка 434: Строка 390:


     local result = table.concat(out, " ")
     local result = table.concat(out, " ")
     if type(frame.preprocess) == "function" then
     return preprocess_or_return(frame, result)
        return frame:preprocess(result)
    end
 
    return result
end
end


Строка 458: Строка 410:
     tplStr = tplStr .. "}}"
     tplStr = tplStr .. "}}"


     if type(frame.preprocess) == "function" then
     return preprocess_or_return(frame, tplStr)
        return frame:preprocess(tplStr)
    end
 
    return tplStr
end
end


Строка 497: Строка 445:


     local result = table.concat(out, " ")
     local result = table.concat(out, " ")
     if type(frame.preprocess) == "function" then
     return preprocess_or_return(frame, result)
        return frame:preprocess(result)
    end
 
    return result
end
end


Строка 513: Строка 457:
     end
     end


     local moduleName = "Module:IanComradeBot/component.json/data"
     local moduleName = get_module_name("component.json")
 
     local data = load_cached_data(moduleName)
     local data = cache[moduleName]
     if not data then
     if not data then
         local ok, loaded = pcall(mw.loadData, moduleName)
         return "false"
        if not ok or not loaded then
            return "false"
        end
        data = loaded
        cache[moduleName] = data
     end
     end