Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Отмена версии 315678, сделанной Pok (обсуждение) Метки: отмена отменено |
Pok (обсуждение | вклад) Отмена версии 315700, сделанной Pok (обсуждение) Метка: отмена |
||
| Строка 3: | Строка 3: | ||
local JsonPaths = require('Module:JsonPaths') | local JsonPaths = require('Module:JsonPaths') | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local function load_cached_data(moduleName) | local function load_cached_data(moduleName) | ||
local ok, loaded = pcall(mw.loadData, moduleName) | local ok, loaded = pcall(mw.loadData, moduleName) | ||
if ok | if not ok or not loaded then | ||
return nil | |||
return | |||
end | end | ||
return loaded | |||
return | |||
end | end | ||
| Строка 180: | Строка 146: | ||
end | end | ||
resolve_entry | local function resolve_entry(data, id) | ||
if type(data) ~= "table" then | if type(data) ~= "table" then | ||
return nil | return nil | ||
| Строка 453: | Строка 419: | ||
local moduleName = JsonPaths.get(pagePath) | local moduleName = JsonPaths.get(pagePath) | ||
data = data or load_cached_data(moduleName) | |||
if not data then | |||
if not | |||
return "" | return "" | ||
end | end | ||
local entry = preEntry or resolve_entry(data, id) | |||
local extra = flatten_entry(entry) | local extra = flatten_entry(entry) | ||
local extraTplArgs = tplArgs or "" | local extraTplArgs = tplArgs or "" | ||
| Строка 534: | Строка 495: | ||
local moduleName = JsonPaths.get(pagePath) | local moduleName = JsonPaths.get(pagePath) | ||
local entry = | local data = load_cached_data(moduleName) | ||
return flatten_entry(entry) | if not data then | ||
end | return "" | ||
end | |||
local entry = resolve_entry(data, id) or {} | |||
return flatten_entry(entry) | |||
end | |||
function p.flattenFieldSelective(frame) | function p.flattenFieldSelective(frame) | ||
| Строка 694: | Строка 660: | ||
return out | return out | ||
end | |||
local function add_scalar_values(value, set) | |||
if type(value) == "table" then | |||
if is_array(value) then | |||
for _, item in ipairs(value) do | |||
add_scalar_values(item, set) | |||
end | |||
else | |||
for _, item in pairs(value) do | |||
add_scalar_values(item, set) | |||
end | |||
end | |||
elseif value ~= nil then | |||
set[tostring(value)] = true | |||
end | |||
end | |||
local function table_has_all_values(value, targets) | |||
if type(targets) ~= "table" or #targets == 0 then | |||
return false | |||
end | |||
if type(value) ~= "table" then | |||
return #targets == 1 and tostring(value) == targets[1] | |||
end | |||
local set = {} | |||
add_scalar_values(value, set) | |||
for i = 1, #targets do | |||
if not set[targets[i]] then | |||
return false | |||
end | |||
end | |||
return true | |||
end | end | ||
| Строка 958: | Строка 961: | ||
local moduleName = JsonPaths.get(pagePath) | local moduleName = JsonPaths.get(pagePath) | ||
local data = frame.data | local data = frame.data | ||
if not data then | |||
if data then | data = load_cached_data(moduleName) | ||
end | end | ||
if not | if not data then | ||
return "" | return "" | ||
end | end | ||
local tplStr = build_tpl(id, pagePath, tplPath, data, tplArgs | local tplStr = build_tpl(id, pagePath, tplPath, data, tplArgs) | ||
return preprocess_or_return(frame, tplStr) | return preprocess_or_return(frame, tplStr) | ||
end | end | ||
| Строка 1625: | Строка 1625: | ||
local moduleName = JsonPaths.get(dataPage) | local moduleName = JsonPaths.get(dataPage) | ||
local entry = | 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, paramNames) | local parts = flatten_selected_parts(entry, paramNames) | ||
if #parts == 0 then | if #parts == 0 then | ||