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

Нет описания правки
Метка: отменено
Отмена версии 315677, сделанной 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 resolve_entry
local moduleApiCache = {}
local function load_module_api(moduleName)
if moduleApiCache[moduleName] ~= nil then
return moduleApiCache[moduleName]
end
local ok, loaded = pcall(require, moduleName)
if ok and type(loaded) == "table" then
moduleApiCache[moduleName] = loaded
return loaded
end
moduleApiCache[moduleName] = false
return nil
end


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 and type(loaded) == "table" and type(loaded.all) ~= "function" then
if not ok or not loaded then
return loaded
return nil
end
 
local api = load_module_api(moduleName)
if api and type(api.all) == "function" then
return api.all()
end
end
 
return loaded
return nil
end
 
local function load_entry_data(moduleName, id)
local api = load_module_api(moduleName)
if api and type(api.get) == "function" then
return api.get(id)
end
 
local data = load_cached_data(moduleName)
return resolve_entry(data, id)
end
end


Строка 180: Строка 146:
end
end


resolve_entry = function(data, id)
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)
local entry = preEntry
data = data or load_cached_data(moduleName)
if not entry then
if not data then
if data then
entry = resolve_entry(data, id)
else
entry = load_entry_data(moduleName, id)
end
end
if not entry then
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 = load_entry_data(moduleName, id) or {}
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
local entry
if not data then
if data then
data = load_cached_data(moduleName)
entry = resolve_entry(data, id)
else
entry = load_entry_data(moduleName, id)
end
end
if not entry then
if not data then
return ""
return ""
end
end


local tplStr = build_tpl(id, pagePath, tplPath, data, tplArgs, entry)
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 = load_entry_data(moduleName, id) or {}
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