Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
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 | 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 | |||
local data = load_cached_data(moduleName) | |||
return resolve_entry(data, id) | |||
end | end | ||
| Строка 146: | Строка 180: | ||
end | end | ||
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 | local entry = preEntry | ||
if not | 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 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 | local entry = load_entry_data(moduleName, id) or {} | ||
return flatten_entry(entry) | |||
end | |||
return flatten_entry(entry) | |||
end | |||
function p.flattenFieldSelective(frame) | function p.flattenFieldSelective(frame) | ||
| Строка 660: | Строка 694: | ||
return out | return out | ||
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 | |||
if not | |||
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 | local entry = load_entry_data(moduleName, id) or {} | ||
local parts = flatten_selected_parts(entry, paramNames) | local parts = flatten_selected_parts(entry, paramNames) | ||
if #parts == 0 then | if #parts == 0 then | ||