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

Отмена версии 315700, сделанной Pok (обсуждение)
Метка: отмена
мНет описания правки
Метка: ручная отмена
 
(не показаны 3 промежуточные версии этого же участника)
Строка 4: Строка 4:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs


local function load_cached_data(moduleName)
local function load_cached_data(pagePath)
local ok, loaded = pcall(mw.loadData, moduleName)
local titleName = JsonPaths.get(pagePath)
if not ok or not loaded then
if not titleName or titleName == "" then
return nil
return nil
end
end
return loaded
 
local ok, data = pcall(mw.loadData, titleName)
if not ok or type(data) ~= "table" then
return nil
end
 
return data
end
end


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


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


return nil
return nil
end
local function load_entry(id, pagePath, data)
data = data or load_cached_data(pagePath)
return resolve_entry_from_data(data, id)
end
end


Строка 418: Строка 429:
end
end


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


local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
local moduleName = JsonPaths.get(storeName)
local data = load_cached_data(storeName)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
Строка 494: Строка 502:
end
end


local moduleName = JsonPaths.get(pagePath)
local entry = load_entry(id, pagePath) or {}
local data = load_cached_data(moduleName)
if not data then
return ""
end
 
local entry = resolve_entry(data, id) or {}
return flatten_entry(entry)
return flatten_entry(entry)
end
end
Строка 518: Строка 520:
end
end


local moduleName = JsonPaths.get(pagePath)
local entry = load_entry(id, pagePath) 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, keys)
local parts = flatten_selected_parts(entry, keys)
if #parts == 0 then
if #parts == 0 then
Строка 543: Строка 539:
end
end


local moduleName = JsonPaths.get(pagePath)
local entry = load_entry(id, pagePath)
local data = load_cached_data(moduleName)
if not data then
return ""
end
 
local entry = resolve_entry(data, id)
if entry == nil then
if entry == nil then
return ""
return ""
Строка 713: Строка 703:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = load_cached_data(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return "[]"
return "[]"
Строка 733: Строка 722:


for _, idKey in ipairs(ids) do
for _, idKey in ipairs(ids) do
local entry = resolve_entry(data, idKey)
local entry = resolve_entry_from_data(data, idKey)
if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then
if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then
matches[#matches + 1] = idKey
matches[#matches + 1] = idKey
Строка 772: Строка 761:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = load_cached_data(pagePath)
local data = load_cached_data(moduleName)
if not data or type(data) ~= "table" then
if not data or type(data) ~= "table" then
return "[]"
return "[]"
Строка 839: Строка 827:


for _, idKey in ipairs(ids) do
for _, idKey in ipairs(ids) do
local entry = resolve_entry(data, idKey)
local entry = resolve_entry_from_data(data, idKey)
if mode == "component" then
if mode == "component" then
if entry_has_all_targets_component(entry, targets) then
if entry_has_all_targets_component(entry, targets) then
Строка 889: Строка 877:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = load_cached_data(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
Строка 910: Строка 897:
if searchType == "path" then
if searchType == "path" then
for _, idKey in ipairs(ids) do
for _, idKey in ipairs(ids) do
local entry = resolve_entry(data, idKey)
local entry = resolve_entry_from_data(data, idKey)
entryCache[idKey] = entry
entryCache[idKey] = entry
if type(entry) == "table" and entry_has_any_nonempty_path(entry, parsedPath) then
if type(entry) == "table" and entry_has_any_nonempty_path(entry, parsedPath) then
Строка 919: Строка 906:
local target = tostring(searchValue)
local target = tostring(searchValue)
for _, idKey in ipairs(ids) do
for _, idKey in ipairs(ids) do
local entry = resolve_entry(data, idKey)
local entry = resolve_entry_from_data(data, idKey)
entryCache[idKey] = entry
entryCache[idKey] = entry
if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then
if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then
Строка 959: Строка 946:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = frame.data
local data = frame.data
if not data then
data = load_cached_data(moduleName)
end
if not data then
return ""
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)
Строка 997: Строка 976:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = load_cached_data(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
Строка 1005: Строка 983:
local out = {}
local out = {}
for _, id in ipairs(ids) do
for _, id in ipairs(ids) do
local tpl = build_tpl(id, pagePath, tplPath, data, tplArgs)
local entry = resolve_entry_from_data(data, id)
if tpl ~= "" then
if entry ~= nil then
out[#out + 1] = tpl
local tpl = build_tpl(id, pagePath, tplPath, data, tplArgs, entry)
if tpl ~= "" then
out[#out + 1] = tpl
end
end
end
end
end
Строка 1055: Строка 1036:
end
end


local moduleName = JsonPaths.get("component.json")
local data = load_cached_data("component.json")
local data = load_cached_data(moduleName)
if not data then
if not data then
return "false"
return "false"
Строка 1087: Строка 1067:
end
end


local moduleName = JsonPaths.get("component.json")
local data = load_cached_data("component.json")
local data = load_cached_data(moduleName)
if not data or type(data) ~= "table" then
if not data or type(data) ~= "table" then
return "[]"
return "[]"
Строка 1123: Строка 1102:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = load_cached_data(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
end
end


local idsTable = data.id
local idsTable = data.id or data
if type(idsTable) ~= "table" then
if type(idsTable) ~= "table" then
return ""
return ""
Строка 1174: Строка 1152:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = load_cached_data(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
end
end


local idsTable = data.id
local idsTable = data.id or data
if type(idsTable) ~= "table" then
if type(idsTable) ~= "table" then
return ""
return ""
Строка 1188: Строка 1165:


for idKey in pairs(idsTable) do
for idKey in pairs(idsTable) do
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs)
local entry = resolve_entry_from_data(data, idKey)
if tpl ~= "" then
if entry ~= nil then
out[#out + 1] = tpl
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entry)
if tpl ~= "" then
out[#out + 1] = tpl
end
end
end
end
end
Строка 1624: Строка 1604:
end
end


local moduleName = JsonPaths.get(dataPage)
local entry = load_entry(id, dataPage) 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