Модуль:getfield

Вернуться

Строка 793: Строка 793:


return ""
return ""
end
local function collect_ids_from_entry(entry)
local out = {}
local seen = {}
local function add(v)
if v == nil then
return
end
local s = tostring(v)
if s ~= "" and not seen[s] then
seen[s] = true
out[#out + 1] = s
end
end
if type(entry) == "table" then
if is_array(entry) then
for _, v in ipairs(entry) do
add(v)
end
else
for _, v in pairs(entry) do
if type(v) == "table" and is_array(v) then
for _, item in ipairs(v) do
add(item)
end
elseif type(v) ~= "table" then
add(v)
end
end
end
else
add(entry)
end
return out
end
end


Строка 809: Строка 847:
end
end


local pagePath
local pagePath = (mode == "component") and "component.json" or "tag.json"
if mode == "component" then
pagePath = "component.json"
else
pagePath = "tag.json"
end
 
local data = p.loadCachedData(pagePath)
local data = p.loadCachedData(pagePath)
if not data or type(data) ~= "table" then
if not data then
return ""
return ""
end
end


local ids = collect_id_keys(data)
local matches = {}
if #ids == 0 then
local seen = {}
return ""
end
 
table.sort(ids, function(a, b)
return tostring(a) < tostring(b)
end)


local function entry_has_all_targets(entry, wanted)
local function add_id(id)
if type(entry) ~= "table" then
local s = tostring(id)
return false
if s ~= "" and not seen[s] then
end
seen[s] = true
 
matches[#matches + 1] = s
local set = {}
for _, v in ipairs(entry) do
set[tostring(v)] = true
end
end
end


for i = 1, #wanted do
for _, key in ipairs(targets) do
if not set[wanted[i]] then
local entry = resolve_entry_from_data(data, key)
return false
if entry ~= nil then
local ids = collect_ids_from_entry(entry)
for _, id in ipairs(ids) do
add_id(id)
end
end
end
end
return true
end
end


local matches = {}
table.sort(matches, function(a, b)
 
return tostring(a) < tostring(b)
for _, idKey in ipairs(ids) do
end)
local entry = resolve_entry_from_data(data, idKey)
if entry_has_all_targets(entry, targets) then
matches[#matches + 1] = idKey
end
end


local ok, json = pcall(mw.text.jsonEncode, matches)
local ok, json = pcall(mw.text.jsonEncode, matches)