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

Нет описания правки
мНет описания правки
 
(не показана 1 промежуточная версия этого же участника)
Строка 548: Строка 548:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local fieldId = args[3] or ""
local fieldId = p.ucfirst(args[3] or "")


if searchId == "" or fieldId == "" then
if searchId == "" or fieldId == "" then
Строка 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)
local function add_id(id)
return tostring(a) < tostring(b)
local s = tostring(id)
end)
if s ~= "" and not seen[s] then
 
seen[s] = true
local function entry_has_all_targets(entry, wanted)
matches[#matches + 1] = s
if type(entry) ~= "table" then
return false
end
end
end


local set = {}
for _, key in ipairs(targets) do
for _, v in ipairs(entry) do
local entry = resolve_entry_from_data(data, key)
set[tostring(v)] = true
if entry ~= nil then
end
local ids = collect_ids_from_entry(entry)
 
for _, id in ipairs(ids) do
for i = 1, #wanted do
add_id(id)
if not set[wanted[i]] then
return false
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)
Строка 974: Строка 994:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local generatorId = args[3] or ""
local generatorId = p.ucfirst(args[3] or "")
local tplPath = mw.text.unstripNoWiki(args[4] or "")
local tplPath = mw.text.unstripNoWiki(args[4] or "")
local tplArgs = args[5] or args.tplArgs or args.templateArgs or ""
local tplArgs = args[5] or args.tplArgs or args.templateArgs or ""
Строка 1023: Строка 1043:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local generatorId = args[3] or ""
local generatorId = p.ucfirst(args[3] or "")


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