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

Нет описания правки
Нет описания правки
Строка 271: Строка 271:
     local value = get_by_path(entry, keyPath)
     local value = get_by_path(entry, keyPath)
     return format_value(value)
     return format_value(value)
end
function p.getId(frame)
    local args = frame.args or {}
    local searchValue = args[1] or ""
    local pagePath = args[2] or ""
    local keyPath = args[3] or ""
    if searchValue == "" or pagePath == "" or keyPath == "" then
        return ""
    end
    local baseUser = "IanComradeBot/"
    local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
    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
    if type(idsTable) ~= "table" then
        return ""
    end
    local target = tostring(searchValue)
    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 tostring(item) == target then
                                return idKey
                            end
                        end
                    else
                        for _, item in pairs(v) do
                            if tostring(item) == target then
                                return idKey
                            end
                        end
                    end
                else
                    if tostring(v) == target then
                        return idKey
                    end
                end
            end
        end
    end
    return ""
end
end