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

Нет описания правки
Нет описания правки
Метка: отменено
Строка 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 not ok or not loaded then
if ok and type(loaded) == "table" and type(loaded.all) ~= "function" then
return nil
return loaded
end
 
local api = load_module_api(moduleName)
if api and type(api.all) == "function" then
return api.all()
end
 
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
end
return loaded
 
local data = load_cached_data(moduleName)
return resolve_entry(data, id)
end
end


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


local function resolve_entry(data, id)
resolve_entry = function(data, id)
if type(data) ~= "table" then
if type(data) ~= "table" then
return nil
return nil
Строка 419: Строка 453:


local moduleName = JsonPaths.get(pagePath)
local moduleName = JsonPaths.get(pagePath)
data = data or load_cached_data(moduleName)
local entry = preEntry
if not data then
if not entry 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 ""
Строка 495: Строка 534:


local moduleName = JsonPaths.get(pagePath)
local moduleName = JsonPaths.get(pagePath)
local data = load_cached_data(moduleName)
local entry = load_entry_data(moduleName, id) or {}
if not data then
return flatten_entry(entry)
return ""
end
end
 
local entry = resolve_entry(data, id) or {}
return flatten_entry(entry)
end


function p.flattenFieldSelective(frame)
function p.flattenFieldSelective(frame)
Строка 660: Строка 694:


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


Строка 942: Строка 939:
return ""
return ""
end
end
 
 
local result = table.concat(out, " ")
local result = table.concat(out, " ")
return preprocess_or_return(frame, result)
return preprocess_or_return(frame, result)
end
end
 
 
function p.getTpl(frame)
function p.getTpl(frame)
local args = getArgs(frame, { removeBlanks = false })
local args = getArgs(frame, { removeBlanks = false })
local id = args[1] or ""
local id = args[1] or ""
local pagePath = args[2] or ""
local pagePath = args[2] or ""
local tplPath = mw.text.unstripNoWiki(args[3] or "")
local tplPath = mw.text.unstripNoWiki(args[3] or "")
local tplArgs = args[4] or args.tplArgs or args.templateArgs or ""
local tplArgs = args[4] or args.tplArgs or args.templateArgs or ""
tplPath, tplArgs = split_template_spec(tplPath, tplArgs)
tplPath, tplArgs = split_template_spec(tplPath, tplArgs)
 
 
if id == "" or pagePath == "" or tplPath == "" then
if id == "" or pagePath == "" or tplPath == "" then
return ""
return ""
end
 
local moduleName = JsonPaths.get(pagePath)
local data = frame.data
local entry
if data then
entry = resolve_entry(data, id)
else
entry = load_entry_data(moduleName, id)
end
end
 
if not entry then
local moduleName = JsonPaths.get(pagePath)
local data = frame.data
if not data then
data = load_cached_data(moduleName)
end
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, entry)
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 data = load_cached_data(moduleName)
local entry = load_entry_data(moduleName, id) or {}
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