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

Нет описания правки
Нет описания правки
 
(не показаны 3 промежуточные версии этого же участника)
Строка 143: Строка 143:
     end
     end
     return nil
     return nil
end
local function collect_id_keys(data)
    if type(data) ~= "table" then
        return {}
    end
    local idsTable = data.id
    local ids = {}
    if type(idsTable) == "table" then
        for k in pairs(idsTable) do
            ids[#ids + 1] = k
        end
        return ids
    end
    for k in pairs(data) do
        if k ~= "default" and k ~= "id" then
            ids[#ids + 1] = k
        end
    end
    return ids
end
end


Строка 165: Строка 188:


     return tostring(v) == target
     return tostring(v) == target
end
local function is_nonempty_value(v)
    if v == nil then return false end
    if type(v) == "table" then
        return next(v) ~= nil
    end
    return true
end
end


Строка 374: Строка 405:
     if not data then return "[]" end
     if not data then return "[]" end


     local idsTable = data.id
     local ids = collect_id_keys(data)
     if type(idsTable) ~= "table" then
     if #ids == 0 then
         return ""
         return ""
     end
     end
Строка 383: Строка 414:
         local target = tostring(searchValue)
         local target = tostring(searchValue)
         matches = {}
         matches = {}
         for idKey, entry in pairs(idsTable) do
         for _, idKey in ipairs(ids) do
            local entry = resolve_entry(data, idKey)
             if type(entry) == "table" then
             if type(entry) == "table" then
                 local v = get_by_path(entry, keyPath)
                 local v = get_by_path(entry, keyPath)
Строка 392: Строка 424:
         end
         end
     else
     else
         matches = find_matching_ids(idsTable, keyPath, searchValue)
        local target = tostring(searchValue)
         matches = {}
        for _, idKey in ipairs(ids) do
            local entry = resolve_entry(data, idKey)
            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
     end
     end


Строка 435: Строка 477:
     if not data then return "" end
     if not data then return "" end


     local idsTable = data.id
     local ids = collect_id_keys(data)
     if type(idsTable) ~= "table" then
     if #ids == 0 then
         return ""
         return ""
     end
     end
Строка 443: Строка 485:
     if searchType == "path" then
     if searchType == "path" then
         matches = {}
         matches = {}
         for idKey, entry in pairs(idsTable) do
         for _, idKey in ipairs(ids) do
            local entry = resolve_entry(data, idKey)
             if type(entry) == "table" then
             if type(entry) == "table" then
                 local v = get_by_path(entry, keyPath)
                 local v = get_by_path(entry, keyPath)
                 if v ~= nil then
                 if is_nonempty_value(v) then
                     matches[#matches + 1] = idKey
                     matches[#matches + 1] = idKey
                 end
                 end
Строка 454: Строка 497:
         local target = tostring(searchValue)
         local target = tostring(searchValue)
         matches = {}
         matches = {}
         for idKey, entry in pairs(idsTable) do
         for _, idKey in ipairs(ids) do
            local entry = resolve_entry(data, idKey)
             if type(entry) == "table" then
             if type(entry) == "table" then
                 local v = get_by_path(entry, keyPath)
                 local v = get_by_path(entry, keyPath)
Строка 463: Строка 507:
         end
         end
     else
     else
         matches = find_matching_ids(idsTable, keyPath, searchValue)
        local target = tostring(searchValue)
         matches = {}
        for _, idKey in ipairs(ids) do
            local entry = resolve_entry(data, idKey)
            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
     end
     end


Строка 490: Строка 544:
     local id = args[1] or ""
     local id = args[1] or ""
     local pagePath = args[2] or ""
     local pagePath = args[2] or ""
     local tplPath = args[3] or ""
     local tplPath = mw.text.unstripNoWiki(args[3] or "")


     if id == "" or pagePath == "" or tplPath == "" then
     if id == "" or pagePath == "" or tplPath == "" then
Строка 521: Строка 575:
     local kind = (args[2] or ""):lower()
     local kind = (args[2] or ""):lower()
     local generatorId = args[3] or ""
     local generatorId = args[3] or ""
     local tplPath = args[4] or ""
     local tplPath = mw.text.unstripNoWiki(args[4] or "")


     if searchId == "" or generatorId == "" or tplPath == "" then
     if searchId == "" or generatorId == "" or tplPath == "" then