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

Нет описания правки
мНет описания правки
 
(не показано 27 промежуточных версий этого же участника)
Строка 4: Строка 4:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs


local function get_module_name(pagePath)
function p.loadCachedData(pagePath)
return JsonPaths.get(pagePath)
local titleName = JsonPaths.get(pagePath)
end
if not titleName or titleName == "" then
 
local function load_cached_data(moduleName)
local ok, loaded = pcall(mw.loadData, moduleName)
if not ok or not loaded then
return nil
return nil
end
end
return loaded
end


local function parse_indexed_part(part)
local ok, data = pcall(mw.loadData, titleName)
local key, idx = string.match(part, "^(.-)%[(%d+)%]$")
if not ok or type(data) ~= "table" then
if key then
return nil
return key, tonumber(idx)
end
end


local num = tonumber(part)
return data
if num then
return nil, num
end
 
return part, nil
end
end


Строка 37: Строка 25:
local parsed = {}
local parsed = {}
for part in string.gmatch(path, "([^%.]+)") do
for part in string.gmatch(path, "([^%.]+)") do
parsed[#parsed + 1] = { parse_indexed_part(part) }
local key, idxStr = string.match(part, "^(.-)%[(%d+)%]$")
if key then
parsed[#parsed + 1] = { key, tonumber(idxStr) }
else
local num = tonumber(part)
if num then
parsed[#parsed + 1] = { nil, num }
else
parsed[#parsed + 1] = { part, nil }
end
end
end
end


Строка 43: Строка 41:
end
end


local function get_by_parsed_path(tbl, parsedPath)
local function navigate_path(tbl, path)
if not tbl or not parsedPath then
if not tbl then return nil end
return nil
if not path or path == "" then return tbl end
end


local cur = tbl
local cur = tbl
for i = 1, #parsedPath do
local start = 1
local token = parsedPath[i]
local len = #path
local key = token[1]
 
local idx = token[2]
while start <= len do
local dotPos = string.find(path, ".", start, true)
local part
if dotPos then
part = string.sub(path, start, dotPos - 1)
start = dotPos + 1
else
part = string.sub(path, start)
start = len + 1
end
 
if type(cur) ~= "table" then return nil end


if key and key ~= "" then
local bracketPos = string.find(part, "[", 1, true)
if type(cur) ~= "table" then
if bracketPos then
return nil
local key = string.sub(part, 1, bracketPos - 1)
local idx = tonumber(string.sub(part, bracketPos + 1, #part - 1))
 
if key ~= "" then
local val = cur[key]
if val == nil then val = cur["!type:" .. key] end
cur = val
end
end


local nextCur = cur[key]
if idx then
if nextCur == nil then
if type(cur) ~= "table" then return nil end
nextCur = cur["!type:" .. key]
cur = cur[idx]
end
end
cur = nextCur
else
end
local num = tonumber(part)
 
if num then
if idx then
cur = cur[num]
if type(cur) ~= "table" then
else
return nil
local val = cur[part]
if val == nil then val = cur["!type:" .. part] end
cur = val
end
end
cur = cur[idx]
end
end


if cur == nil then
if cur == nil then return nil end
return nil
end
end
end


return cur
return cur
end
local function get_by_path(tbl, path)
return get_by_parsed_path(tbl, parse_path(path))
end
end


local function format_value(v)
local function format_value(v)
local okJson, json = pcall(mw.text.jsonEncode, v)
if okJson and json == "null" then
return "null"
end
if v == nil then
if v == nil then
return ""
return ""
Строка 99: Строка 103:
return tostring(v)
return tostring(v)
elseif t == "table" then
elseif t == "table" then
local ok, json2 = pcall(mw.text.jsonEncode, v)
local ok, json = pcall(mw.text.jsonEncode, v)
if ok and json2 then
if ok and json then
return json2
return json
end
end
return ""
return ""
Строка 107: Строка 111:
return tostring(v)
return tostring(v)
end
end
end
local function to_nowiki(v)
return "<nowiki>" .. v .. "</nowiki>"
end
end


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


local function resolve_entry(data, id)
function p.ucfirst(s)
if not s or s == "" then return s end
return string.upper(s:sub(1, 1)) .. (s:sub(2) or "")
end
 
function p.normalizeComponentName(name)
if type(name) ~= "string" or name == "" then
return nil
end
name = mw.text.trim(name)
if name:sub(1, 5) == "type:" then
name = name:sub(6)
elseif name:sub(1, 6) == "!type:" then
name = name:sub(7)
end
if name == "" then
return nil
end
return name
end
 
local function split_data_page_path(pagePath)
local kind, name = string.match(pagePath or "", "^(component)/([^/]+)%.json$")
if not kind then
return nil, nil
end
return kind, p.ucfirst(name)
end
 
local function get_component_from_entity(entity, componentName)
if type(entity) ~= "table" or type(entity.components) ~= "table" then
return nil
end
 
return entity.components["type:" .. componentName]
or entity.components[componentName]
or entity.components["!type:" .. componentName]
end
 
local function load_specific_entry(pagePath, id, allowDefault)
local kind, name = split_data_page_path(pagePath)
if not kind or not id or id == "" then
return nil
end
 
if kind ~= "component" then
return nil
end
 
local base
local defaultData = p.loadCachedData(kind .. "/" .. name .. "/defaultFields.json")
if type(defaultData) == "table" then
base = deep_copy(defaultData)
end
 
local entityPage = "prototype/Entity/" .. id .. ".json"
local entity = p.loadCachedData(entityPage)
local specific = get_component_from_entity(entity, name)
 
if type(specific) ~= "table" then
if allowDefault == false then
return nil
end
return base
end
 
if base then
deep_merge(base, specific)
return base
end
 
return specific
end
 
local function resolve_entry_from_data(data, id, allowDefault)
if type(data) ~= "table" then
if type(data) ~= "table" then
return nil
return nil
Строка 173: Строка 247:
return merged
return merged
end
end
return deep_copy(specific)
return specific
end
end
end
end
end
end


local base = data["default"]
if allowDefault == false then
if type(base) == "table" then
return deep_copy(base)
end
 
return nil
end
 
local function resolve_path_value(data, id, parsedPath)
if type(data) ~= "table" or not parsedPath or not id or id == "" then
return nil
return nil
end
local direct = data[id]
if direct ~= nil then
local value = get_by_parsed_path(direct, parsedPath)
if value ~= nil then
return value
end
end
local idsTable = data.id
if type(idsTable) == "table" then
local specific = idsTable[id]
if type(specific) == "table" then
local value = get_by_parsed_path(specific, parsedPath)
if value ~= nil then
return value
end
end
end
end


local base = data["default"]
local base = data["default"]
if type(base) == "table" then
if type(base) == "table" then
return get_by_parsed_path(base, parsedPath)
return base
end
end


Строка 218: Строка 264:
end
end


local function resolve_entry_path_value(data, id, parsedPath)
local function load_entry(id, pagePath, data, allowDefault)
if type(data) ~= "table" or not parsedPath or not id or id == "" then
local specificPageEntry = load_specific_entry(pagePath, id, allowDefault)
return nil
if specificPageEntry ~= nil then
return specificPageEntry
end
end


local entry = resolve_entry(data, id)
data = data or p.loadCachedData(pagePath)
if type(entry) ~= "table" then
if allowDefault == nil then
return nil
allowDefault = true
end
end
 
return resolve_entry_from_data(data, id, allowDefault)
return get_by_parsed_path(entry, parsedPath)
end
end


Строка 256: Строка 302:


local function contains_target(v, target)
local function contains_target(v, target)
if type(v) == "string" then
return v == target
end
if type(v) == "table" then
if type(v) == "table" then
if is_array(v) then
if is_array(v) then
Строка 285: Строка 334:
end
end
return true
return true
end
local function find_matching_ids(idsTable, keyPath, searchValue)
local parsedPath = parse_path(keyPath)
local target = tostring(searchValue)
local matches = {}
for idKey, entry in pairs(idsTable) do
if type(entry) == "table" then
local v = get_by_parsed_path(entry, parsedPath)
if v ~= nil and contains_target(v, target) then
matches[#matches + 1] = idKey
end
end
end
return matches
end
end


Строка 359: Строка 391:


local parts = {}
local parts = {}
local function append_table_json(key, value)
local ok, json = pcall(mw.text.jsonEncode, value)
if ok and json then
parts[#parts + 1] = key .. "=" .. to_nowiki(json)
end
end


local function walk(tbl, prefix)
local function walk(tbl, prefix)
Строка 383: Строка 408:
if type(v) == "table" then
if type(v) == "table" then
if next(v) ~= nil then
if next(v) ~= nil then
append_table_json(key, v)
local ok, json = pcall(mw.text.jsonEncode, v)
if ok and json then
parts[#parts + 1] = key .. "=<nowiki>" .. json .. "</nowiki>"
end
if is_array(v) then
if is_array(v) then
local first = v[1]
local first = v[1]
Строка 423: Строка 451:
local ok, json = pcall(mw.text.jsonEncode, value)
local ok, json = pcall(mw.text.jsonEncode, value)
if ok and json then
if ok and json then
parts[#parts + 1] = key .. "=" .. to_nowiki(json)
parts[#parts + 1] = key .. "=<nowiki>" .. json .. "</nowiki>"
end
end
return
return
Строка 443: Строка 471:
if type(key) == "string" and key ~= "" and not seen[key] then
if type(key) == "string" and key ~= "" and not seen[key] then
seen[key] = true
seen[key] = true
append_flattened_part(parts, key, get_by_path(entry, key))
append_flattened_part(parts, key, navigate_path(entry, key))
end
end
end
end
Строка 482: Строка 510:
end
end


local function build_tpl(id, pagePath, tplPath, data, tplArgs)
local function build_tpl(id, pagePath, tplPath, data, tplArgs, preEntry, strictExact)
if id == "" or pagePath == "" or tplPath == "" then
if id == "" or pagePath == "" or tplPath == "" then
return ""
return ""
end
end


local moduleName = get_module_name(pagePath)
local entry
data = data or load_cached_data(moduleName)
if strictExact then
if not data then
entry = load_entry(id, pagePath, data, false)
else
entry = preEntry or load_entry(id, pagePath, data)
end
 
if entry == nil then
return ""
return ""
end
end


local entry = resolve_entry(data, id)
local extra = flatten_entry(entry)
local extra = flatten_entry(entry)
local extraTplArgs = tplArgs or ""
local extraTplArgs = tplArgs or ""
Строка 516: Строка 548:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local fieldId = args[3] or ""
local fieldId = p.ucfirst(args[3] or "")


if searchId == "" or fieldId == "" then
if searchId == "" or fieldId == "" then
Строка 526: Строка 558:


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 = get_module_name(storeName)
local data = p.loadCachedData(storeName)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
Строка 563: Строка 594:
end
end


local moduleName = get_module_name(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
Строка 587: Строка 612:
end
end


local moduleName = get_module_name(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
Строка 612: Строка 631:
end
end


local moduleName = get_module_name(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 ""
Строка 627: Строка 640:
end
end


local value = get_by_path(entry, keyPath)
return format_value(navigate_path(entry, keyPath))
return format_value(value)
end
 
local function collect_by_parsed_path(tbl, parsedPath, pos, out)
if pos > #parsedPath then
out[#out + 1] = tbl
return
end
 
if type(tbl) ~= "table" then
return
end
 
local token = parsedPath[pos]
local key = token[1]
local idx = token[2]
 
if key == "*" then
for _, child in pairs(tbl) do
local nextCur = child
 
if idx then
if type(nextCur) ~= "table" then
nextCur = nil
else
nextCur = nextCur[idx]
end
end
 
collect_by_parsed_path(nextCur, parsedPath, pos + 1, out)
end
return
end
 
local nextCur
 
if key and key ~= "" then
nextCur = tbl[key]
if nextCur == nil then nextCur = tbl["!type:" .. key] end
else
nextCur = tbl
end
 
if idx then
if type(nextCur) ~= "table" then
return
end
nextCur = nextCur[idx]
end
 
collect_by_parsed_path(nextCur, parsedPath, pos + 1, out)
end
 
local function get_by_parsed_path_multi(tbl, parsedPath)
local out = {}
collect_by_parsed_path(tbl, parsedPath, 1, out)
return out
end
 
local function entry_matches_path(entry, parsedPath, searchValue, searchType)
local values = get_by_parsed_path_multi(entry, parsedPath)
local target = tostring(searchValue)
 
for _, v in ipairs(values) do
if searchType == "key" then
if type(v) == "table" and v[target] ~= nil then
return true
end
else
if contains_target(v, target) then
return true
end
end
end
 
return false
end
 
local function entry_has_any_nonempty_path(entry, parsedPath)
local values = get_by_parsed_path_multi(entry, parsedPath)
 
for _, v in ipairs(values) do
if is_nonempty_value(v) then
return true
end
end
 
return false
end
 
local function split_csv_list(text)
text = mw.text.unstripNoWiki(text or "")
local out = {}
 
for part in string.gmatch(text, "([^,]+)") do
part = mw.text.trim(part)
if part ~= "" then
out[#out + 1] = part
end
end
 
return out
end
end


Строка 645: Строка 758:
end
end


local moduleName = get_module_name(pagePath)
local data = p.loadCachedData(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return "[]"
return "[]"
Строка 656: Строка 768:
end
end


local idsTable = type(data.id) == "table" and data.id or data
local ids = collect_id_keys(data)
local ids = collect_id_keys(data)
if #ids == 0 then
if #ids == 0 then
Строка 662: Строка 773:
end
end


local matches
local matches = {}
if searchType == "key" then
local target = tostring(searchValue)
local target = tostring(searchValue)
 
matches = {}
for _, idKey in ipairs(ids) do
for _, idKey in ipairs(ids) do
local entry = resolve_entry_from_data(data, idKey)
local v = resolve_path_value(data, idKey, parsedPath)
if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then
if type(v) == "table" and v[target] ~= nil then
matches[#matches + 1] = idKey
matches[#matches + 1] = idKey
end
end
 
if #matches == 0 then
return ""
end
 
local ok, json = pcall(mw.text.jsonEncode, matches)
if ok and json then
return json
end
 
return ""
end
 
local function collect_ids_from_entry(entry)
local out = {}
local seen = {}
 
local function add(v)
if v == nil then
return
end
local s = tostring(v)
if s ~= "" and not seen[s] then
seen[s] = true
out[#out + 1] = s
end
end
 
if type(entry) == "table" then
if is_array(entry) then
for _, v in ipairs(entry) do
add(v)
end
end
end
else
local target = tostring(searchValue)
matches = {}
if idsTable == data then
matches = find_matching_ids(idsTable, keyPath, searchValue)
else
else
for _, idKey in ipairs(ids) do
for _, v in pairs(entry) do
local v = resolve_path_value(data, idKey, parsedPath)
if type(v) == "table" and is_array(v) then
if v ~= nil and contains_target(v, target) then
for _, item in ipairs(v) do
matches[#matches + 1] = idKey
add(item)
end
elseif type(v) ~= "table" then
add(v)
end
end
end
end
end
end
else
add(entry)
end
end


if #matches == 0 then
return out
end
 
function p.search(frame)
local args = getArgs(frame, { removeBlanks = false })
local mode = (args[1] or ""):lower()
local rawList = args[2] or ""
 
if mode ~= "component" and mode ~= "tag" then
return ""
end
 
local targets = split_csv_list(rawList)
if #targets == 0 then
return ""
end
 
local pagePath = (mode == "component") and "component.json" or "tag.json"
local data = p.loadCachedData(pagePath)
if not data then
return ""
return ""
end
end
local matches = {}
local seen = {}
local function add_id(id)
local s = tostring(id)
if s ~= "" and not seen[s] then
seen[s] = true
matches[#matches + 1] = s
end
end
for _, key in ipairs(targets) do
local entry = resolve_entry_from_data(data, key)
if entry ~= nil then
local ids = collect_ids_from_entry(entry)
for _, id in ipairs(ids) do
add_id(id)
end
end
end
table.sort(matches, function(a, b)
return tostring(a) < tostring(b)
end)


local ok, json = pcall(mw.text.jsonEncode, matches)
local ok, json = pcall(mw.text.jsonEncode, matches)
Строка 729: Строка 916:
end
end


local moduleName = get_module_name(pagePath)
local data = p.loadCachedData(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
Строка 745: Строка 931:
end
end


local matches
local matches = {}
local entryCache = {}
 
if searchType == "path" then
if searchType == "path" then
matches = {}
for _, idKey in ipairs(ids) do
for _, idKey in ipairs(ids) do
local v = resolve_entry_path_value(data, idKey, parsedPath)
local entry = resolve_entry_from_data(data, idKey)
if is_nonempty_value(v) then
entryCache[idKey] = entry
if type(entry) == "table" and entry_has_any_nonempty_path(entry, parsedPath) then
matches[#matches + 1] = idKey
matches[#matches + 1] = idKey
end
end
end
end
elseif searchType == "key" then
else
local target = tostring(searchValue)
local target = tostring(searchValue)
matches = {}
for _, idKey in ipairs(ids) do
for _, idKey in ipairs(ids) do
local v = resolve_path_value(data, idKey, parsedPath)
local entry = resolve_entry_from_data(data, idKey)
if type(v) == "table" and v[target] ~= nil then
entryCache[idKey] = entry
if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then
matches[#matches + 1] = idKey
matches[#matches + 1] = idKey
end
end
else
local idsTable = data.id
if type(idsTable) == "table" then
local directMatches = find_matching_ids(idsTable, keyPath, searchValue)
local defaultValue = nil
local includeDefault = false
local base = data["default"]
if type(base) == "table" then
defaultValue = get_by_parsed_path(base, parsedPath)
includeDefault = defaultValue ~= nil and contains_target(defaultValue, tostring(searchValue))
end
if includeDefault and #directMatches < #ids then
local seen = {}
for i = 1, #directMatches do
seen[directMatches[i]] = true
end
matches = {}
for _, idKey in ipairs(directMatches) do
matches[#matches + 1] = idKey
end
for _, idKey in ipairs(ids) do
if not seen[idKey] then
local specific = idsTable[idKey]
local specificValue = type(specific) == "table" and get_by_parsed_path(specific, parsedPath) or
nil
if specificValue == nil then
matches[#matches + 1] = idKey
end
end
end
else
matches = directMatches
end
else
local target = tostring(searchValue)
matches = {}
for _, idKey in ipairs(ids) do
local v = resolve_path_value(data, idKey, parsedPath)
if v ~= nil and contains_target(v, target) then
matches[#matches + 1] = idKey
end
end
end
end
end
Строка 816: Строка 959:
local out = {}
local out = {}
for _, idKey in ipairs(matches) do
for _, idKey in ipairs(matches) do
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs)
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entryCache[idKey], true)
if tpl ~= "" then
if tpl ~= "" then
out[#out + 1] = tpl
out[#out + 1] = tpl
Строка 842: Строка 985:
end
end


local moduleName = get_module_name(pagePath)
local data = frame.data
local data = frame.data
if not data then
local tplStr = build_tpl(id, pagePath, tplPath, data, tplArgs, nil, true)
data = load_cached_data(moduleName)
end
if not data then
return ""
end
 
local tplStr = build_tpl(id, pagePath, tplPath, data, tplArgs)
return preprocess_or_return(frame, tplStr)
return preprocess_or_return(frame, tplStr)
end
end
Строка 859: Строка 994:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local generatorId = args[3] or ""
local generatorId = p.ucfirst(args[3] or "")
local tplPath = mw.text.unstripNoWiki(args[4] or "")
local tplPath = mw.text.unstripNoWiki(args[4] or "")
local tplArgs = args[5] or args.tplArgs or args.templateArgs or ""
local tplArgs = args[5] or args.tplArgs or args.templateArgs or ""
Строка 880: Строка 1015:
end
end


local moduleName = get_module_name(pagePath)
local data = p.loadCachedData(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
Строка 888: Строка 1022:
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, true)
if tpl ~= "" then
out[#out + 1] = tpl
end
end
end
end
end
Строка 906: Строка 1043:
local searchId = args[1] or ""
local searchId = args[1] or ""
local kind = (args[2] or ""):lower()
local kind = (args[2] or ""):lower()
local generatorId = args[3] or ""
local generatorId = p.ucfirst(args[3] or "")


if searchId == "" or generatorId == "" then
if searchId == "" or generatorId == "" then
Строка 927: Строка 1064:


return ""
return ""
end
function p.loadEntityData(entityId)
if entityId == "" then
return nil
end
local pagePath = "prototype/Entity/" .. entityId .. ".json"
return p.loadCachedData(pagePath)
end
function p.entityHasComponent(entity, compName)
if type(entity) ~= "table" or type(entity.components) ~= "table" then
return false
end
return entity.components[compName] ~= nil
or entity.components["type:" .. compName] ~= nil
or entity.components["!type:" .. compName] ~= nil
end
function p.collectEntityComponents(entity)
if type(entity) ~= "table" or type(entity.components) ~= "table" then
return {}
end
local out = {}
local seen = {}
local comps = entity.components
if #comps > 0 then
for _, v in ipairs(comps) do
local name = p.normalizeComponentName(v)
if name and not seen[name] then
seen[name] = true
out[#out + 1] = name
end
end
else
for k in pairs(comps) do
local name = p.normalizeComponentName(k)
if name and not seen[name] then
seen[name] = true
out[#out + 1] = name
end
end
end
table.sort(out)
return out
end
end


Строка 938: Строка 1125:
end
end


local moduleName = get_module_name("component.json")
local entity = p.loadEntityData(entityId)
local data = load_cached_data(moduleName)
if not entity then
if not data then
return "false"
return "false"
end
end


if type(data) ~= "table" then
return p.entityHasComponent(entity, compName) and "true" or "false"
return "false"
end
 
function p.getComp(frame)
local args = getArgs(frame, { removeBlanks = false })
local entityId = args[1] or ""
 
if entityId == "" then
return ""
end
end


local entry = data[entityId]
local entity = p.loadEntityData(entityId)
if type(entry) ~= "table" then
if not entity then
return "false"
return ""
end
end


local target = tostring(compName)
local out = p.collectEntityComponents(entity)
for _, v in ipairs(entry) do
local ok, json = pcall(mw.text.jsonEncode, out)
if tostring(v) == target then
if ok and json then
return "true"
return json
end
end
end


return "false"
return ""
end
end


Строка 973: Строка 1165:
end
end


local moduleName = get_module_name(pagePath)
local data = p.loadCachedData(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 ""
Строка 1024: Строка 1215:
end
end


local moduleName = get_module_name(pagePath)
local data = p.loadCachedData(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 ""
Строка 1038: Строка 1228:


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, true)
if tpl ~= "" then
out[#out + 1] = tpl
end
end
end
end
end
Строка 1053: Строка 1246:
local ok, json = pcall(mw.text.jsonEncode, value)
local ok, json = pcall(mw.text.jsonEncode, value)
if ok and json then
if ok and json then
return to_nowiki(json)
return "<nowiki>" .. json .. "</nowiki>"
end
end
return nil
return nil
Строка 1132: Строка 1325:
local json = encode_nowiki_json(value)
local json = encode_nowiki_json(value)
if json then
if json then
parts[#parts + 1] = prefix .. "=" .. tostring(json)
parts[#parts + 1] = prefix .. "=" .. json
end
end
end
end
Строка 1152: Строка 1345:
local json = encode_nowiki_json(v)
local json = encode_nowiki_json(v)
if json then
if json then
parts[#parts + 1] = key .. "=" .. tostring(json)
parts[#parts + 1] = key .. "=" .. json
end
end
elseif options.nestedKeyMode == "raw" then
elseif options.nestedKeyMode == "raw" then
local json = encode_nowiki_json(v)
local json = encode_nowiki_json(v)
if json then
if json then
parts[#parts + 1] = key .. "=" .. tostring(json)
parts[#parts + 1] = key .. "=" .. json
end
end
end
end
Строка 1217: Строка 1410:
return count > 1
return count > 1
end
end
local firstParamOverride = mw.text.unstripNoWiki(args.first or args.firstParam or args.idKey or "")


local function makeCall(obj)
local function makeCall(obj)
Строка 1223: Строка 1418:
end
end


local idKey = choose_id_key(obj)
local idKey = firstParamOverride
if idKey == "" then
idKey = choose_id_key(obj)
end
if not idKey then
if not idKey then
return
return
Строка 1245: Строка 1443:
local json = encode_nowiki_json(v)
local json = encode_nowiki_json(v)
if json then
if json then
parts[#parts + 1] = "value=" .. tostring(json)
parts[#parts + 1] = "value=" .. json
end
end
append_table_fields(parts, v, rawTypeOptions, nil)
append_table_fields(parts, v, rawTypeOptions, nil)
Строка 1255: Строка 1453:
local json = encode_nowiki_json(v)
local json = encode_nowiki_json(v)
if json then
if json then
parts[#parts + 1] = "value=" .. tostring(json)
parts[#parts + 1] = "value=" .. json
end
end
end
end
Строка 1284: Строка 1482:
if is_array(data) then
if is_array(data) then
for _, item in ipairs(data) do
for _, item in ipairs(data) do
makeCall(item)
if type(item) == "table" then
makeCall(item)
elseif item ~= nil and item ~= "" then
makeCall({ [item] = {} })
end
end
end
elseif is_object_map(data) then
elseif is_object_map(data) then
Строка 1333: Строка 1535:
local pairPattern = mw.text.unstripNoWiki(args.pattern or "(.*)")
local pairPattern = mw.text.unstripNoWiki(args.pattern or "(.*)")
local pairReplace = mw.text.unstripNoWiki(args.replace or "\\1")
local pairReplace = mw.text.unstripNoWiki(args.replace or "\\1")
local maxItems = tonumber(args.max or args.limit or args.max_count or args.maxCount or "")
if maxItems ~= nil then
maxItems = math.floor(maxItems)
if maxItems < 0 then
maxItems = nil
end
end


local out = {}
local out = {}


if is_array(data) then
if is_array(data) then
local processed = 0
for _, v in ipairs(data) do
for _, v in ipairs(data) do
processed = processed + 1
if maxItems ~= nil and processed > maxItems then
break
end
local text = ""
local text = ""


Строка 1380: Строка 1597:
return tostring(a) < tostring(b)
return tostring(a) < tostring(b)
end)
end)
local processed = 0


for _, k in ipairs(keys) do
for _, k in ipairs(keys) do
processed = processed + 1
if maxItems ~= nil and processed > maxItems then
break
end
local v = data[k]
local v = data[k]
local vStr
local vStr
Строка 1436: Строка 1660:
return frame:preprocess(table.concat(out, " "))
return frame:preprocess(table.concat(out, " "))
end
end
end
function p.flattenFieldSelectiveDirect(id, dataPage, paramNames)
if id == "" or dataPage == "" or type(paramNames) ~= "table" or #paramNames == 0 then
return ""
end
local entry = load_entry(id, dataPage) or {}
local parts = flatten_selected_parts(entry, paramNames)
if #parts == 0 then
return ""
end
return table.concat(parts, "|")
end
end


return p
return p