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

Нет описания правки
мНет описания правки
 
(не показаны 24 промежуточные версии этого же участника)
Строка 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
 
local num = tonumber(part)
if num then
return nil, num
end
end


return part, nil
return data
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
 
local bracketPos = string.find(part, "[", 1, true)
if bracketPos then
local key = string.sub(part, 1, bracketPos - 1)
local idx = tonumber(string.sub(part, bracketPos + 1, #part - 1))


if key and key ~= "" then
if key ~= "" then
if type(cur) ~= "table" then
local val = cur[key]
return nil
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
Строка 342: Строка 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)
Строка 366: Строка 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]
Строка 406: Строка 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
Строка 426: Строка 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
Строка 465: Строка 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 ""
Строка 499: Строка 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
Строка 509: Строка 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 ""
Строка 546: Строка 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
Строка 570: Строка 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
Строка 595: Строка 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 ""
Строка 610: Строка 640:
end
end


local value = get_by_path(entry, keyPath)
return format_value(navigate_path(entry, keyPath))
return format_value(value)
end
end


Строка 649: Строка 678:
if key and key ~= "" then
if key and key ~= "" then
nextCur = tbl[key]
nextCur = tbl[key]
if nextCur == nil then
if nextCur == nil then nextCur = tbl["!type:" .. key] end
nextCur = tbl["!type:" .. key]
end
else
else
nextCur = tbl
nextCur = tbl
Строка 701: Строка 728:


return false
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


Строка 717: Строка 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 "[]"
Строка 737: Строка 777:


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
Строка 746: Строка 786:
return ""
return ""
end
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
else
for _, v in pairs(entry) do
if type(v) == "table" and is_array(v) then
for _, item in ipairs(v) do
add(item)
end
elseif type(v) ~= "table" then
add(v)
end
end
end
else
add(entry)
end
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 ""
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)
Строка 785: Строка 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 ""
Строка 802: Строка 932:


local matches = {}
local matches = {}
local entryCache = {}


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
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
matches[#matches + 1] = idKey
matches[#matches + 1] = idKey
Строка 813: Строка 945:
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
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
Строка 826: Строка 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
Строка 852: Строка 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
Строка 869: Строка 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 ""
Строка 890: Строка 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 ""
Строка 898: Строка 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
Строка 916: Строка 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
Строка 937: Строка 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


Строка 948: Строка 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


Строка 983: Строка 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 ""
Строка 1034: Строка 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 ""
Строка 1048: Строка 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
Строка 1063: Строка 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
Строка 1142: Строка 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
Строка 1162: Строка 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
Строка 1227: Строка 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)
Строка 1233: Строка 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
Строка 1255: Строка 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)
Строка 1265: Строка 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
Строка 1294: Строка 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
Строка 1343: Строка 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 = ""


Строка 1390: Строка 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
Строка 1446: Строка 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