Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 329: | Строка 329: | ||
return "" | return "" | ||
end | |||
function p.getTplId(frame) | |||
local args = frame.args or {} | |||
local searchValue = args[1] or "" | |||
local pagePath = args[2] or "" | |||
local keyPath = args[3] or "" | |||
local tplPath = args[4] or "" | |||
if searchValue == "" or pagePath == "" or keyPath == "" or tplPath == "" 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) | |||
local matches = {} | |||
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 | |||
matches[#matches + 1] = idKey | |||
break | |||
end | |||
end | |||
else | |||
for _, item in pairs(v) do | |||
if tostring(item) == target then | |||
matches[#matches + 1] = idKey | |||
break | |||
end | |||
end | |||
end | |||
else | |||
if tostring(v) == target then | |||
matches[#matches + 1] = idKey | |||
end | |||
end | |||
end | |||
end | |||
end | |||
if #matches == 0 then | |||
return "" | |||
end | |||
local out = {} | |||
for _, idKey in ipairs(matches) do | |||
local tpl = p.getTpl({ args = { idKey, pagePath, tplPath } }) | |||
if tpl ~= "" then | |||
out[#out + 1] = tpl | |||
end | |||
end | |||
if #out == 0 then | |||
return "" | |||
end | |||
local result = table.concat(out, "\n") | |||
if type(frame.preprocess) == "function" then | |||
return frame:preprocess(result) | |||
end | |||
return result | |||
end | end | ||