Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 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" | ||
local data = p.loadCachedData(pagePath) | local data = p.loadCachedData(pagePath) | ||
if not data | if not data then | ||
return "" | return "" | ||
end | end | ||
local | local matches = {} | ||
local seen = {} | |||
local function | local function add_id(id) | ||
local s = tostring(id) | |||
if s ~= "" and not seen[s] then | |||
seen[s] = true | |||
matches[#matches + 1] = s | |||
end | end | ||
end | |||
for _, key in ipairs(targets) do | |||
local entry = resolve_entry_from_data(data, key) | |||
if entry ~= nil then | |||
local ids = collect_ids_from_entry(entry) | |||
for _, id in ipairs(ids) do | |||
add_id(id) | |||
end | end | ||
end | end | ||
end | end | ||
table.sort(matches, function(a, b) | |||
return tostring(a) < tostring(b) | |||
end) | |||
end | |||
local ok, json = pcall(mw.text.jsonEncode, matches) | local ok, json = pcall(mw.text.jsonEncode, matches) | ||