Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 396: | Строка 396: | ||
end | end | ||
return table.concat(parts, "|") | return table.concat(parts, "|") | ||
end | |||
local function append_flattened_part(parts, key, value) | |||
if value == nil then | |||
return | |||
end | |||
if type(value) == "table" then | |||
if next(value) == nil then | |||
return | |||
end | |||
local ok, json = pcall(mw.text.jsonEncode, value) | |||
if ok and json then | |||
parts[#parts + 1] = key .. "=" .. to_nowiki(json) | |||
end | |||
return | |||
end | |||
parts[#parts + 1] = key .. "=" .. tostring(value) | |||
end | |||
local function flatten_selected_parts(entry, keys) | |||
if type(entry) ~= "table" or type(keys) ~= "table" then | |||
return {} | |||
end | |||
local parts = {} | |||
local seen = {} | |||
for i = 1, #keys do | |||
local key = keys[i] | |||
if type(key) == "string" and key ~= "" and not seen[key] then | |||
seen[key] = true | |||
append_flattened_part(parts, key, get_by_path(entry, key)) | |||
end | |||
end | |||
return parts | |||
end | end | ||
| Строка 493: | Строка 532: | ||
local entry = resolve_entry(data, id) or {} | local entry = resolve_entry(data, id) or {} | ||
return flatten_entry(entry) | return flatten_entry(entry) | ||
end | |||
function p.flattenFieldSelective(frame) | |||
local args = frame.args or {} | |||
local id = args[1] or "" | |||
local pagePath = args[2] or "" | |||
local keysJson = mw.text.unstripNoWiki(args[3] or args.keys or "") | |||
if id == "" or pagePath == "" or keysJson == "" then | |||
return "" | |||
end | |||
local okKeys, keys = pcall(mw.text.jsonDecode, keysJson) | |||
if not okKeys or type(keys) ~= "table" or #keys == 0 then | |||
return "" | |||
end | |||
local moduleName = get_module_name(pagePath) | |||
local data = load_cached_data(moduleName) | |||
if not data then | |||
return "" | |||
end | |||
local entry = resolve_entry(data, id) or {} | |||
local parts = flatten_selected_parts(entry, keys) | |||
if #parts == 0 then | |||
return "" | |||
end | |||
return table.concat(parts, "|") | |||
end | end | ||