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

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


local function load_cached_data(pagePath)
function p.loadCachedData(pagePath)
local titleName = JsonPaths.get(pagePath)
local titleName = JsonPaths.get(pagePath)
if not titleName or titleName == "" then
if not titleName or titleName == "" then
Строка 100: Строка 100:


local t = type(v)
local t = type(v)
if t == "string" then
if t == "string" or t == "number" or t == "boolean" then
return v
elseif t == "number" or t == "boolean" then
return tostring(v)
return tostring(v)
elseif t == "table" then
elseif t == "table" then
Строка 116: Строка 114:


local function is_array(tbl)
local function is_array(tbl)
local n = #tbl
local max = 0
if n == 0 then
return false
end
if tbl[n] == nil then
return false
end
local count = 0
local count = 0
for k in pairs(tbl) do
for k in pairs(tbl) do
if type(k) ~= "number" then
if type(k) ~= "number" then
return false
return false
end
if k > max then
max = k
end
end
count = count + 1
count = count + 1
end
end
return count == n
return count > 0 and max == count
end
 
local function is_array_of_primitives(tbl)
if type(tbl) ~= "table" then return false end
local n = #tbl
if n == 0 or tbl[n] == nil then return false end
local count = 0
for k, v in pairs(tbl) do
if type(k) ~= "number" then return false end
if type(v) == "table" then return false end
count = count + 1
end
return count == n
end
end


Строка 158: Строка 140:
end
end


local function merge_copy(base, specific)
local function deep_merge(dst, src)
local result = {}
for k, v in pairs(src) do
for k, v in pairs(base) do
if type(v) == "table" and type(dst[k]) == "table" then
local specificV = specific[k]
deep_merge(dst[k], v)
if specificV ~= nil then
if type(v) == "table" and type(specificV) == "table" then
result[k] = merge_copy(v, specificV)
elseif type(specificV) == "table" then
result[k] = deep_copy(specificV)
else
result[k] = specificV
end
elseif type(v) == "table" then
elseif type(v) == "table" then
result[k] = deep_copy(v)
dst[k] = deep_copy(v)
else
else
result[k] = v
dst[k] = v
end
end
end
end
for k, v in pairs(specific) do
if result[k] == nil then
if type(v) == "table" then
result[k] = deep_copy(v)
else
result[k] = v
end
end
end
return result
end
end


local function resolve_entry_from_data(data, id)
function p.ucfirst(s)
if type(data) ~= "table" then
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
return nil
end
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


if id and id ~= "" then
local function split_data_page_path(pagePath)
local direct = data[id]
local kind, name = string.match(pagePath or "", "^(component)/([^/]+)%.json$")
if direct ~= nil then
if not kind then
return direct
return nil, nil
end
 
local idsTable = data.id
if type(idsTable) == "table" then
local specific = idsTable[id]
if type(specific) == "table" then
local base = data["default"]
if type(base) == "table" then
return merge_copy(base, specific)
end
return specific
end
end
end
end
return kind, p.ucfirst(name)
end


local base = data["default"]
local function get_component_from_entity(entity, componentName)
if type(base) == "table" then
if type(entity) ~= "table" or type(entity.components) ~= "table" then
return base
return nil
end
end


return nil
return entity.components["type:" .. componentName]
or entity.components[componentName]
or entity.components["!type:" .. componentName]
end
end


local function load_entry(id, pagePath, data)
local function load_specific_entry(pagePath, id, allowDefault)
data = data or load_cached_data(pagePath)
local kind, name = split_data_page_path(pagePath)
return resolve_entry_from_data(data, id)
if not kind or not id or id == "" then
end
return nil
end
 
if kind ~= "component" then
return nil
end


local function collect_id_keys(data)
local base
if type(data) ~= "table" then
local defaultData = p.loadCachedData(kind .. "/" .. name .. "/defaultFields.json")
return {}
if type(defaultData) == "table" then
base = deep_copy(defaultData)
end
end


local idsTable = data.id
local entityPage = "prototype/Entity/" .. id .. ".json"
local ids = {}
local entity = p.loadCachedData(entityPage)
local specific = get_component_from_entity(entity, name)


if type(idsTable) == "table" then
if type(specific) ~= "table" then
for k in pairs(idsTable) do
if allowDefault == false then
ids[#ids + 1] = k
return nil
end
end
return ids
return base
end
end


for k in pairs(data) do
if base then
if k ~= "default" and k ~= "id" then
deep_merge(base, specific)
ids[#ids + 1] = k
return base
end
end
end


return ids
return specific
end
end


local function contains_target(v, target)
local function resolve_entry_from_data(data, id, allowDefault)
if type(v) == "string" then
if type(data) ~= "table" then
return v == target
return nil
end
end
if type(v) == "table" then
 
for _, item in pairs(v) do
if id and id ~= "" then
if tostring(item) == target then
local direct = data[id]
return true
if direct ~= nil then
return direct
end
 
local idsTable = data.id
if type(idsTable) == "table" then
local specific = idsTable[id]
if type(specific) == "table" then
local base = data["default"]
if type(base) == "table" then
local merged = deep_copy(base)
deep_merge(merged, specific)
return merged
end
return specific
end
end
end
end
return false
end
end


return tostring(v) == target
if allowDefault == false then
end
return nil
end


local function is_nonempty_value(v)
local base = data["default"]
if v == nil then
if type(base) == "table" then
return false
return base
end
end
if type(v) == "table" then
 
return next(v) ~= nil
return nil
end
 
local function load_entry(id, pagePath, data, allowDefault)
local specificPageEntry = load_specific_entry(pagePath, id, allowDefault)
if specificPageEntry ~= nil then
return specificPageEntry
end
end
return true
end


local function preprocess_or_return(frame, text)
data = data or p.loadCachedData(pagePath)
if type(frame) == "table" and type(frame.preprocess) == "function" then
if allowDefault == nil then
return frame:preprocess(text)
allowDefault = true
end
end
return text
return resolve_entry_from_data(data, id, allowDefault)
end
end


local function get_field_loose(entry, fieldId)
local function collect_id_keys(data)
local value = entry[fieldId]
if type(data) ~= "table" then
if value ~= nil then
return {}
return value
end
if fieldId == "" then
return nil
end
end


local first = string.sub(fieldId, 1, 1)
local idsTable = data.id
local tail = string.sub(fieldId, 2)
local ids = {}
 
if type(idsTable) == "table" then
for k in pairs(idsTable) do
ids[#ids + 1] = k
end
return ids
end


value = entry[string.lower(first) .. tail]
for k in pairs(data) do
if value ~= nil then
if k ~= "default" and k ~= "id" then
return value
ids[#ids + 1] = k
end
end
end


return entry[string.upper(first) .. tail]
return ids
end
end


local function apply_pattern(s, pattern, repl)
local function contains_target(v, target)
if not pattern or pattern == "" or not s then
if type(v) == "string" then
return s
return v == target
end
end
if type(v) == "table" then
if is_array(v) then
for _, item in ipairs(v) do
if tostring(item) == target then
return true
end
end
return false
end


local text = tostring(s)
for _, item in pairs(v) do
local replacement
if tostring(item) == target then
if repl and repl ~= "" then
return true
replacement = tostring(repl)
end
replacement = replacement:gsub("\\(%d)", "%%%1")
end
else
return false
replacement = "%1"
end
end


local patt = pattern
return tostring(v) == target
if not patt:find("%^") and not patt:find("%$") then
patt = "^" .. patt .. "$"
end
 
return (text:gsub(patt, replacement))
end
end


local function flatten_parts(entry)
local function is_nonempty_value(v)
if type(entry) ~= "table" then
if v == nil then
return {}
return false
end
if type(v) == "table" then
return next(v) ~= nil
end
end
return true
end


local parts = {}
local function preprocess_or_return(frame, text)
if type(frame) == "table" and type(frame.preprocess) == "function" then
return frame:preprocess(text)
end
return text
end


local function walk(tbl, prefix)
local function get_field_loose(entry, fieldId)
local entries = {}
local value = entry[fieldId]
for k in pairs(tbl) do
if value ~= nil then
entries[#entries + 1] = { k, tostring(k) }
return value
end
end
table.sort(entries, function(a, b) return a[2] < b[2] end)
if fieldId == "" then
 
return nil
for _, e in ipairs(entries) do
local k = e[1]
local kStr = e[2]
local v = tbl[k]
local key = (prefix == "" and kStr or prefix .. "." .. kStr)
 
if type(v) == "table" then
if next(v) ~= nil then
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
local first = v[1]
if type(first) == "table" then
walk(first, key)
end
else
walk(v, key)
end
end
else
parts[#parts + 1] = key .. "=" .. tostring(v)
end
end
end
end


walk(entry, "")
local first = string.sub(fieldId, 1, 1)
return parts
local tail = string.sub(fieldId, 2)
end


local function flatten_entry(entry)
value = entry[string.lower(first) .. tail]
local parts = flatten_parts(entry)
if value ~= nil then
if #parts == 0 then
return value
return ""
end
end
return table.concat(parts, "|")
 
return entry[string.upper(first) .. tail]
end
end


local function append_flattened_part(parts, key, value)
local function apply_pattern(s, pattern, repl)
if value == nil then
if not pattern or pattern == "" or not s then
return
return s
end
end


if type(value) == "table" then
local text = tostring(s)
if next(value) == nil then
local replacement
return
if repl and repl ~= "" then
end
replacement = tostring(repl)
replacement = replacement:gsub("\\(%d)", "%%%1")
else
replacement = "%1"
end


local ok, json = pcall(mw.text.jsonEncode, value)
local patt = pattern
if ok and json then
if not patt:find("%^") and not patt:find("%$") then
parts[#parts + 1] = key .. "=<nowiki>" .. json .. "</nowiki>"
patt = "^" .. patt .. "$"
end
return
end
end


parts[#parts + 1] = key .. "=" .. tostring(value)
return (text:gsub(patt, replacement))
end
end


local function flatten_selected_parts(entry, keys)
local function flatten_parts(entry)
if type(entry) ~= "table" or type(keys) ~= "table" then
if type(entry) ~= "table" then
return {}
return {}
end
end


local parts = {}
local parts = {}
local seen = {}


for i = 1, #keys do
local function walk(tbl, prefix)
local key = keys[i]
local keys = {}
if type(key) == "string" and key ~= "" and not seen[key] then
for k in pairs(tbl) do
seen[key] = true
keys[#keys + 1] = k
append_flattened_part(parts, key, navigate_path(entry, key))
end
table.sort(keys, function(a, b)
return tostring(a) < tostring(b)
end)
 
for _, k in ipairs(keys) do
local v = tbl[k]
local kStr = tostring(k)
local key = (prefix == "" and kStr or prefix .. "." .. kStr)
 
if type(v) == "table" then
if next(v) ~= nil then
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
local first = v[1]
if type(first) == "table" then
walk(first, key)
end
else
walk(v, key)
end
end
else
parts[#parts + 1] = key .. "=" .. tostring(v)
end
end
end
end
end


walk(entry, "")
return parts
return parts
end
end


local function resolve_template_path(tplPath)
local function flatten_entry(entry)
local templatePath = tplPath
local parts = flatten_parts(entry)
local project = JsonPaths.project()
if #parts == 0 then
if project ~= nil and project ~= "" then
return ""
templatePath = tplPath .. "/" .. project
templatePath = "{{#ifexist:Шаблон:" .. templatePath .. "|" .. templatePath .. "|" .. tplPath .. "}}"
end
end
 
return table.concat(parts, "|")
return templatePath
end
end


local function split_template_spec(tplPath, tplArgs)
local function append_flattened_part(parts, key, value)
tplPath = mw.text.unstripNoWiki(tplPath or "")
if value == nil then
tplArgs = mw.text.unstripNoWiki(tplArgs or "")
return
if tplArgs ~= "" and string.sub(tplArgs, 1, 1) == "|" then
tplArgs = string.sub(tplArgs, 2)
end
end


if tplArgs == "" then
if type(value) == "table" then
local pipePos = string.find(tplPath, "|", 1, true)
if next(value) == nil then
if pipePos then
return
tplArgs = mw.text.unstripNoWiki(string.sub(tplPath, pipePos + 1))
end
if tplArgs ~= "" and string.sub(tplArgs, 1, 1) == "|" then
 
tplArgs = string.sub(tplArgs, 2)
local ok, json = pcall(mw.text.jsonEncode, value)
end
if ok and json then
tplPath = string.sub(tplPath, 1, pipePos - 1)
parts[#parts + 1] = key .. "=<nowiki>" .. json .. "</nowiki>"
end
end
return
end
end


return tplPath, tplArgs
parts[#parts + 1] = key .. "=" .. tostring(value)
end
end


local function build_tpl(id, pagePath, tplPath, data, tplArgs, preEntry)
local function flatten_selected_parts(entry, keys)
if id == "" or pagePath == "" or tplPath == "" then
if type(entry) ~= "table" or type(keys) ~= "table" then
return ""
return {}
end
end


local entry = preEntry or load_entry(id, pagePath, data)
local parts = {}
if entry == nil then
local seen = {}
return ""
end


local extra = flatten_entry(entry)
for i = 1, #keys do
local extraTplArgs = tplArgs or ""
local key = keys[i]
if type(key) == "string" and key ~= "" and not seen[key] then
seen[key] = true
append_flattened_part(parts, key, navigate_path(entry, key))
end
end


local templatePath = resolve_template_path(tplPath)
return parts
end


local tplParts = { "{{Шаблон:" .. templatePath }
local function resolve_template_path(tplPath)
if extraTplArgs ~= "" then
local templatePath = tplPath
tplParts[#tplParts + 1] = extraTplArgs
local project = JsonPaths.project()
if project ~= nil and project ~= "" then
templatePath = tplPath .. "/" .. project
templatePath = "{{#ifexist:Шаблон:" .. templatePath .. "|" .. templatePath .. "|" .. tplPath .. "}}"
end
end
tplParts[#tplParts + 1] = "id=" .. tostring(id)
 
if extra ~= "" then
return templatePath
tplParts[#tplParts + 1] = extra
end
tplParts[#tplParts + 1] = "}}"
return table.concat(tplParts, "|")
end
end


function p.findInGenerator(frame)
local function split_template_spec(tplPath, tplArgs)
local args = frame.args or {}
tplPath = mw.text.unstripNoWiki(tplPath or "")
local searchId = args[1] or ""
tplArgs = mw.text.unstripNoWiki(tplArgs or "")
local kind = (args[2] or ""):lower()
if tplArgs ~= "" and string.sub(tplArgs, 1, 1) == "|" then
local fieldId = args[3] or ""
tplArgs = string.sub(tplArgs, 2)
end


if searchId == "" or fieldId == "" then
if tplArgs == "" then
return ""
local pipePos = string.find(tplPath, "|", 1, true)
if pipePos then
tplArgs = mw.text.unstripNoWiki(string.sub(tplPath, pipePos + 1))
if tplArgs ~= "" and string.sub(tplArgs, 1, 1) == "|" then
tplArgs = string.sub(tplArgs, 2)
end
tplPath = string.sub(tplPath, 1, pipePos - 1)
end
end
end
if kind ~= "prototype" and kind ~= "component" then
 
return tplPath, tplArgs
end
 
local function build_tpl(id, pagePath, tplPath, data, tplArgs, preEntry, strictExact)
if id == "" or pagePath == "" or tplPath == "" then
return ""
return ""
end
end


local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
local entry
local data = load_cached_data(storeName)
if strictExact then
if not data then
entry = load_entry(id, pagePath, data, false)
return ""
else
entry = preEntry or load_entry(id, pagePath, data)
end
end


local entry = data[searchId]
if entry == nil then
if type(entry) ~= "table" then
return ""
return ""
end
end


local value = get_field_loose(entry, fieldId)
local extra = flatten_entry(entry)
if value == nil then
local extraTplArgs = tplArgs or ""
return ""
 
local templatePath = resolve_template_path(tplPath)
 
local tplStr = "{{Шаблон:" .. templatePath
if extraTplArgs ~= "" then
tplStr = tplStr .. "|" .. extraTplArgs
end
end
 
tplStr = tplStr .. "|id=" .. tostring(id)
local out = {}
if extra ~= "" then
local t = type(value)
tplStr = tplStr .. "|" .. extra
if t == "table" then
for _, v in ipairs(value) do
out[#out + 1] = v
end
else
out[1] = value
end
end
tplStr = tplStr .. "}}"


return mw.text.jsonEncode(out)
return tplStr
end
end


function p.flattenField(frame)
function p.findInGenerator(frame)
local args = frame.args or {}
local args = frame.args or {}
local id = args[1] or ""
local searchId = args[1] or ""
local pagePath = args[2] or ""
local kind = (args[2] or ""):lower()
if id == "" or pagePath == "" then
local fieldId = p.ucfirst(args[3] or "")
 
if searchId == "" or fieldId == "" then
return ""
return ""
end
end
 
if kind ~= "prototype" and kind ~= "component" then
local entry = load_entry(id, pagePath) or {}
return flatten_entry(entry)
end
 
function p.flattenFieldSelective(frame)
local args = frame.args or {}
local id = args[1] or ""
local pagePath = args[2] or ""
local keysJson = mw.text.unstripNoWiki(args[3] or args.keys or "")
if id == "" or pagePath == "" or keysJson == "" then
return ""
return ""
end
end


local okKeys, keys = pcall(mw.text.jsonDecode, keysJson)
local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
if not okKeys or type(keys) ~= "table" or #keys == 0 then
local data = p.loadCachedData(storeName)
if not data then
return ""
return ""
end
end


local entry = load_entry(id, pagePath) or {}
local entry = data[searchId]
local parts = flatten_selected_parts(entry, keys)
if type(entry) ~= "table" then
if #parts == 0 then
return ""
return ""
end
end


return table.concat(parts, "|")
local value = get_field_loose(entry, fieldId)
if value == nil then
return ""
end
 
local out = {}
local t = type(value)
if t == "table" then
for _, v in ipairs(value) do
out[#out + 1] = v
end
else
out[1] = value
end
 
return mw.text.jsonEncode(out)
end
end


function p.get(frame)
function p.flattenField(frame)
local args = getArgs(frame, { removeBlanks = false })
local args = frame.args or {}
local id = args[1] or ""
local id = args[1] or ""
local pagePath = args[2] or ""
local pagePath = args[2] or ""
local keyPath = args[3] or ""
if id == "" or pagePath == "" then
 
if pagePath == "" then
return ""
return ""
end
end


local entry = load_entry(id, pagePath)
local entry = load_entry(id, pagePath) or {}
if entry == nil then
return flatten_entry(entry)
end
 
function p.flattenFieldSelective(frame)
local args = frame.args or {}
local id = args[1] or ""
local pagePath = args[2] or ""
local keysJson = mw.text.unstripNoWiki(args[3] or args.keys or "")
if id == "" or pagePath == "" or keysJson == "" then
return ""
end
 
local okKeys, keys = pcall(mw.text.jsonDecode, keysJson)
if not okKeys or type(keys) ~= "table" or #keys == 0 then
return ""
return ""
end
end


if keyPath == "" then
local entry = load_entry(id, pagePath) or {}
return format_value(entry)
local parts = flatten_selected_parts(entry, keys)
if #parts == 0 then
return ""
end
end


return format_value(navigate_path(entry, keyPath))
return table.concat(parts, "|")
end
end


local function collect_by_parsed_path(tbl, parsedPath, pos, out)
function p.get(frame)
local args = getArgs(frame, { removeBlanks = false })
local id = args[1] or ""
local pagePath = args[2] or ""
local keyPath = args[3] or ""
 
if pagePath == "" then
return ""
end
 
local entry = load_entry(id, pagePath)
if entry == nil then
return ""
end
 
if keyPath == "" then
return format_value(entry)
end
 
return format_value(navigate_path(entry, keyPath))
end
 
local function collect_by_parsed_path(tbl, parsedPath, pos, out)
if pos > #parsedPath then
if pos > #parsedPath then
out[#out + 1] = tbl
out[#out + 1] = tbl
Строка 675: Строка 744:
end
end


local function add_scalar_values(value, set)
function p.searchId(frame)
if type(value) == "table" then
local args = getArgs(frame, { removeBlanks = false })
for _, item in pairs(value) do
add_scalar_values(item, set)
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
 
function p.searchId(frame)
local args = getArgs(frame, { removeBlanks = false })
local searchValue = args[1] or ""
local searchValue = args[1] or ""
local pagePath = args[2] or ""
local pagePath = args[2] or ""
Строка 720: Строка 758:
end
end


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


local function entry_has_all_targets_component(entry, wanted)
local function collect_ids_from_entry(entry)
if type(entry) ~= "table" then
local out = {}
return false
local seen = {}
end


local set = {}
local function add(v)
for _, v in pairs(entry) do
if v == nil then
set[tostring(v)] = true
return
end
local s = tostring(v)
if s ~= "" and not seen[s] then
seen[s] = true
out[#out + 1] = s
end
end
end


for i = 1, #wanted do
if type(entry) == "table" then
if not set[wanted[i]] then
if is_array(entry) then
return false
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
end
else
add(entry)
end
end


return true
return out
end
end


local function entry_has_all_targets_tag(entry, wanted)
function p.search(frame)
if type(entry) ~= "table" then
local args = getArgs(frame, { removeBlanks = false })
return false
local mode = (args[1] or ""):lower()
end
local rawList = args[2] or ""


local tags = entry.tags or entry.tag
if mode ~= "component" and mode ~= "tag" then
if type(tags) ~= "table" then
return ""
return false
end
end


local set = {}
local targets = split_csv_list(rawList)
for _, v in pairs(tags) do
if #targets == 0 then
set[tostring(v)] = true
return ""
end
end


for i = 1, #wanted do
local pagePath = (mode == "component") and "component.json" or "tag.json"
if not set[wanted[i]] then
local data = p.loadCachedData(pagePath)
return false
if not data then
end
return ""
end
end


return true
local matches = {}
end
local seen = {}


function p.search(frame)
local function add_id(id)
local args = getArgs(frame, { removeBlanks = false })
local s = tostring(id)
local mode = (args[1] or ""):lower()
if s ~= "" and not seen[s] then
local rawList = args[2] or ""
seen[s] = true
 
matches[#matches + 1] = s
if mode ~= "component" and mode ~= "tag" then
end
return "[]"
end
end


local targets = split_csv_list(rawList)
for _, key in ipairs(targets) do
if #targets == 0 then
local entry = resolve_entry_from_data(data, key)
return "[]"
if entry ~= nil then
local ids = collect_ids_from_entry(entry)
for _, id in ipairs(ids) do
add_id(id)
end
end
end
end


local pagePath
table.sort(matches, function(a, b)
if mode == "component" then
return tostring(a) < tostring(b)
pagePath = "component.json"
end)
else
pagePath = "component/tag.json"
end


local data = load_cached_data(pagePath)
local ok, json = pcall(mw.text.jsonEncode, matches)
if not data or type(data) ~= "table" then
if ok and json then
return "[]"
return json
end
end


local ids = collect_id_keys(data)
return ""
if #ids == 0 then
return "[]"
end
 
local sortedIds = {}
for i = 1, #ids do
sortedIds[i] = { ids[i], tostring(ids[i]) }
end
table.sort(sortedIds, function(a, b) return a[2] < b[2] end)
 
local matches = {}
 
for _, e in ipairs(sortedIds) do
local idKey = e[1]
local entry = resolve_entry_from_data(data, idKey)
if mode == "component" then
if entry_has_all_targets_component(entry, targets) then
matches[#matches + 1] = idKey
end
else
if entry_has_all_targets_tag(entry, targets) then
matches[#matches + 1] = idKey
end
end
end
 
local ok, json = pcall(mw.text.jsonEncode, matches)
if ok and json then
return json
end
 
return "[]"
end
end


Строка 891: Строка 916:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 934: Строка 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, entryCache[idKey])
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
Строка 961: Строка 986:


local data = frame.data
local data = frame.data
local tplStr = build_tpl(id, pagePath, tplPath, data, tplArgs)
local tplStr = build_tpl(id, pagePath, tplPath, data, tplArgs, nil, true)
return preprocess_or_return(frame, tplStr)
return preprocess_or_return(frame, tplStr)
end
end
Строка 969: Строка 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 ""
Строка 990: Строка 1015:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 999: Строка 1024:
local entry = resolve_entry_from_data(data, id)
local entry = resolve_entry_from_data(data, id)
if entry ~= nil then
if entry ~= nil then
local tpl = build_tpl(id, pagePath, tplPath, data, tplArgs, entry)
local tpl = build_tpl(id, pagePath, tplPath, data, tplArgs, entry, true)
if tpl ~= "" then
if tpl ~= "" then
out[#out + 1] = tpl
out[#out + 1] = tpl
Строка 1018: Строка 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
Строка 1041: Строка 1066:
end
end


function p.hasComp(frame)
function p.loadEntityData(entityId)
local args = getArgs(frame, { removeBlanks = false })
if entityId == "" then
local entityId = args[1] or ""
return nil
local compName = args[2] or ""
end
 
local pagePath = "prototype/Entity/" .. entityId .. ".json"
return p.loadCachedData(pagePath)
end


if entityId == "" or compName == "" then
function p.entityHasComponent(entity, compName)
return "false"
if type(entity) ~= "table" or type(entity.components) ~= "table" then
return false
end
end


local data = load_cached_data("component.json")
return entity.components[compName] ~= nil
if not data then
or entity.components["type:" .. compName] ~= nil
return "false"
or entity.components["!type:" .. compName] ~= nil
end
end


if type(data) ~= "table" then
function p.collectEntityComponents(entity)
return "false"
if type(entity) ~= "table" or type(entity.components) ~= "table" then
return {}
end
end


local entry = data[entityId]
local out = {}
if type(entry) ~= "table" then
local seen = {}
return "false"
local comps = entity.components
end


for _, v in ipairs(entry) do
if #comps > 0 then
if tostring(v) == compName then
for _, v in ipairs(comps) do
return "true"
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
end
end


return "false"
table.sort(out)
return out
end
end


function p.getComp(frame)
function p.hasComp(frame)
local args = getArgs(frame, { removeBlanks = false })
local args = getArgs(frame, { removeBlanks = false })
local entityId = args[1] or ""
local entityId = args[1] or ""
local compName = args[2] or ""


if entityId == "" then
if entityId == "" or compName == "" then
return "[]"
return "false"
end
end


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


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


local out = {}
local entity = p.loadEntityData(entityId)
for _, v in ipairs(entry) do
if not entity then
if v ~= nil and v ~= "" then
return ""
out[#out + 1] = v
end
end
end


local out = p.collectEntityComponents(entity)
local ok, json = pcall(mw.text.jsonEncode, out)
local ok, json = pcall(mw.text.jsonEncode, out)
if ok and json then
if ok and json then
Строка 1103: Строка 1152:
end
end


return "[]"
return ""
end
end


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


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 1166: Строка 1215:
end
end


local data = load_cached_data(pagePath)
local data = p.loadCachedData(pagePath)
if not data then
if not data then
return ""
return ""
Строка 1181: Строка 1230:
local entry = resolve_entry_from_data(data, idKey)
local entry = resolve_entry_from_data(data, idKey)
if entry ~= nil then
if entry ~= nil then
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entry)
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entry, true)
if tpl ~= "" then
if tpl ~= "" then
out[#out + 1] = tpl
out[#out + 1] = tpl
Строка 1202: Строка 1251:
end
end


local function collect_sorted_entries(tbl, stringOnly)
local function collect_sorted_keys(tbl, stringOnly)
if stringOnly then
local keys = {}
local keys = {}
for k in pairs(tbl) do
for k in pairs(tbl) do
if not stringOnly or type(k) == "string" then
if type(k) == "string" then
keys[#keys + 1] = k
keys[#keys + 1] = k
end
end
end
table.sort(keys)
local entries = {}
for i = 1, #keys do
entries[i] = { keys[i], keys[i] }
end
return entries
end
end
local entries = {}
for k in pairs(tbl) do
entries[#entries + 1] = { k, tostring(k) }
end
table.sort(entries, function(a, b) return a[2] < b[2] end)
return entries
end


local function collect_sorted_keys(tbl, stringOnly)
table.sort(keys, function(a, b)
local entries = collect_sorted_entries(tbl, stringOnly)
return tostring(a) < tostring(b)
local keys = {}
end)
for i = 1, #entries do
 
keys[i] = entries[i][1]
end
return keys
return keys
end
end


local function choose_id_key(obj)
local function choose_id_key(obj)
local entries = {}
local keys = {}
for k in pairs(obj) do
for k in pairs(obj) do
if type(k) == "string" then
if type(k) == "string" then
entries[#entries + 1] = { k, type(obj[k]) ~= "table" }
keys[#keys + 1] = k
end
end
end
end


if #entries == 0 then
if #keys == 0 then
return nil
return nil
end
end


table.sort(entries, function(a, b)
table.sort(keys, function(a, b)
if a[2] ~= b[2] then
local av = obj[a]
return not a[2]
local bv = obj[b]
 
local aPrimitive = type(av) ~= "table"
local bPrimitive = type(bv) ~= "table"
 
if aPrimitive ~= bPrimitive then
return not aPrimitive
end
end
return a[1] < b[1]
 
return tostring(a) < tostring(b)
end)
end)


return entries[1][1]
return keys[1]
end
end


local function is_wrapper_block_key(key)
local function is_wrapper_block_key(key)
return type(key) == "string" and not key:match("^[%a_][%w_]*$")
return type(key) == "string" and not key:match("^[%a_][%w_]*$")
end
local function is_array_of_primitives(tbl)
if type(tbl) ~= "table" or not is_array(tbl) then
return false
end
for _, v in ipairs(tbl) do
if type(v) == "table" then
return false
end
end
return true
end
end


Строка 1276: Строка 1329:
end
end


local entries = collect_sorted_entries(value, false)
local keys = collect_sorted_keys(value, false)


for _, e in ipairs(entries) do
for _, k in ipairs(keys) do
local k = e[1]
local kStr = e[2]
if not (options.nestedKeyMode == "raw" and type(k) == "number") then
if not (options.nestedKeyMode == "raw" and type(k) == "number") then
local v = value[k]
local v = value[k]
local key
local key
if prefix then
if prefix then
key = prefix .. "." .. kStr
key = prefix .. "." .. tostring(k)
else
else
key = kStr
key = tostring(k)
end
end


Строка 1319: Строка 1370:
end
end
end
end
end
local function is_object_map(tbl)
local count = 0
for k, v in pairs(tbl) do
if type(k) ~= "string" or type(v) ~= "table" then
return false
end
count = count + 1
end
return count > 1
end
end


Строка 1359: Строка 1399:
skipPrimitiveRoot = true,
skipPrimitiveRoot = true,
}
}
local function is_object_map(tbl)
local count = 0
for k, v in pairs(tbl) do
if type(k) ~= "string" or type(v) ~= "table" then
return false
end
count = count + 1
end
return count > 1
end


local firstParamOverride = mw.text.unstripNoWiki(args.first or args.firstParam or args.idKey or "")
local firstParamOverride = mw.text.unstripNoWiki(args.first or args.firstParam or args.idKey or "")
Строка 1381: Строка 1432:
end
end


parts[#parts + 1] = idKey
parts[#parts + 1] = tostring(idKey)
local entries = collect_sorted_entries(obj, true)
local keys = collect_sorted_keys(obj, true)


for _, e in ipairs(entries) do
for _, k in ipairs(keys) do
local k = e[1]
local v = obj[k]
local v = obj[k]


Строка 1439: Строка 1489:
end
end
elseif is_object_map(data) then
elseif is_object_map(data) then
local dataEntries = collect_sorted_entries(data, true)
local keys = collect_sorted_keys(data, true)
for _, e in ipairs(dataEntries) do
for _, k in ipairs(keys) do
makeCall({ [e[1]] = data[e[1]] })
makeCall({ [k] = data[k] })
end
end
else
else
Строка 1536: Строка 1586:
end
end


out[#out + 1] = line
table.insert(out, line)
end
end
end
end
else
else
local dataEntries = {}
local keys = {}
for k in pairs(data) do
for k in pairs(data) do
dataEntries[#dataEntries + 1] = { k, tostring(k) }
keys[#keys + 1] = k
end
end
table.sort(dataEntries, function(a, b) return a[2] < b[2] end)
table.sort(keys, function(a, b)
return tostring(a) < tostring(b)
end)


local processed = 0
local processed = 0


for _, de in ipairs(dataEntries) do
for _, k in ipairs(keys) do
processed = processed + 1
processed = processed + 1
if maxItems ~= nil and processed > maxItems then
if maxItems ~= nil and processed > maxItems then
Строка 1554: Строка 1606:
end
end


local k = de[1]
local kStr = de[2]
local v = data[k]
local v = data[k]
local vStr
local vStr
Строка 1570: Строка 1620:
end
end


local baseKey = apply_pattern(kStr, keyPattern, "\\1")
local baseKey = apply_pattern(tostring(k), keyPattern, "\\1")


local MARK_KEY = "\31KEY\31"
local MARK_KEY = "\31KEY\31"
Строка 1579: Строка 1629:
local MARK_VAL = "\31VAL\31"
local MARK_VAL = "\31VAL\31"
local kRepl = (keyReplace or "\\1"):gsub("\\2", MARK_VAL)
local kRepl = (keyReplace or "\\1"):gsub("\\2", MARK_VAL)
local keyStr0 = apply_pattern(kStr, keyPattern, kRepl)
local keyStr0 = apply_pattern(tostring(k), keyPattern, kRepl)
local keyStr = tostring(keyStr0):gsub(MARK_VAL, vStr0)
local keyStr = tostring(keyStr0):gsub(MARK_VAL, vStr0)


Строка 1598: Строка 1648:
end
end


out[#out + 1] = line
table.insert(out, line)
end
end
end
end