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

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


local function load_cached_data(moduleName)
function p.loadCachedData(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)
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
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
return nil
end
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
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


local idsTable = data.id
return entity.components["type:" .. componentName]
if type(idsTable) == "table" then
or entity.components[componentName]
local specific = idsTable[id]
or entity.components["!type:" .. componentName]
if type(specific) == "table" then
end
local base = data["default"]
 
if type(base) == "table" then
local function load_specific_entry(pagePath, id, allowDefault)
local merged = deep_copy(base)
local kind, name = split_data_page_path(pagePath)
deep_merge(merged, specific)
if not kind or not id or id == "" then
return merged
return nil
end
end
return specific
 
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
end
return base
end
end


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


return nil
return specific
end
end


local function collect_id_keys(data)
local function resolve_entry_from_data(data, id, allowDefault)
if type(data) ~= "table" then
if type(data) ~= "table" then
return {}
return nil
end
end


local idsTable = data.id
if id and id ~= "" then
local ids = {}
local direct = data[id]
if direct ~= nil then
return direct
end


if type(idsTable) == "table" then
local idsTable = data.id
for k in pairs(idsTable) do
if type(idsTable) == "table" then
ids[#ids + 1] = k
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
return ids
end
end


for k in pairs(data) do
if allowDefault == false then
if k ~= "default" and k ~= "id" then
return nil
ids[#ids + 1] = k
end
end
end


return ids
local base = data["default"]
if type(base) == "table" then
return base
end
 
return nil
end
end


local function contains_target(v, target)
local function load_entry(id, pagePath, data, allowDefault)
if type(v) == "string" then
local specificPageEntry = load_specific_entry(pagePath, id, allowDefault)
return v == target
if specificPageEntry ~= nil then
return specificPageEntry
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


for _, item in pairs(v) do
data = data or p.loadCachedData(pagePath)
if tostring(item) == target then
if allowDefault == nil then
return true
allowDefault = true
end
end
return false
end
end
 
return resolve_entry_from_data(data, id, allowDefault)
return tostring(v) == target
end
end


local function is_nonempty_value(v)
local function collect_id_keys(data)
if v == nil then
if type(data) ~= "table" then
return false
return {}
end
end
if type(v) == "table" then
 
return next(v) ~= nil
local idsTable = data.id
local ids = {}
 
if type(idsTable) == "table" then
for k in pairs(idsTable) do
ids[#ids + 1] = k
end
return ids
end
end
return true
end


local function preprocess_or_return(frame, text)
for k in pairs(data) do
if type(frame) == "table" and type(frame.preprocess) == "function" then
if k ~= "default" and k ~= "id" then
return frame:preprocess(text)
ids[#ids + 1] = k
end
end
end
return text
end


local function get_field_loose(entry, fieldId)
return ids
local value = entry[fieldId]
if value ~= nil then
return value
end
if fieldId == "" then
return nil
end
 
local first = string.sub(fieldId, 1, 1)
local tail = string.sub(fieldId, 2)
 
value = entry[string.lower(first) .. tail]
if value ~= nil then
return value
end
 
return entry[string.upper(first) .. tail]
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
 
local text = tostring(s)
local replacement
if repl and repl ~= "" then
replacement = tostring(repl)
replacement = replacement:gsub("\\(%d)", "%%%1")
else
replacement = "%1"
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 patt = pattern
for _, item in pairs(v) do
if not patt:find("%^") and not patt:find("%$") then
if tostring(item) == target then
patt = "^" .. patt .. "$"
return true
end
end
return false
end
end


return (text:gsub(patt, replacement))
return tostring(v) == target
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 keys = {}
local value = entry[fieldId]
for k in pairs(tbl) do
if value ~= nil then
keys[#keys + 1] = k
return value
end
end
table.sort(keys, function(a, b)
if fieldId == "" then
return tostring(a) < tostring(b)
return nil
end)
end
 
local first = string.sub(fieldId, 1, 1)
local tail = string.sub(fieldId, 2)


for _, k in ipairs(keys) do
value = entry[string.lower(first) .. tail]
local v = tbl[k]
if value ~= nil then
local kStr = tostring(k)
return value
local key = (prefix == "" and kStr or prefix .. "." .. kStr)
end


if type(v) == "table" then
return entry[string.upper(first) .. tail]
if next(v) ~= nil then
end
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


walk(entry, "")
local function apply_pattern(s, pattern, repl)
return parts
if not pattern or pattern == "" or not s then
end
return s
 
local function flatten_entry(entry)
local parts = flatten_parts(entry)
if #parts == 0 then
return ""
end
end
return table.concat(parts, "|")
end


local function append_flattened_part(parts, key, value)
local text = tostring(s)
if value == nil then
local replacement
return
if repl and repl ~= "" then
replacement = tostring(repl)
replacement = replacement:gsub("\\(%d)", "%%%1")
else
replacement = "%1"
end
end


if type(value) == "table" then
local patt = pattern
if next(value) == nil then
if not patt:find("%^") and not patt:find("%$") then
return
patt = "^" .. patt .. "$"
end
 
local ok, json = pcall(mw.text.jsonEncode, value)
if ok and json then
parts[#parts + 1] = key .. "=<nowiki>" .. json .. "</nowiki>"
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
end
end
table.sort(keys, function(a, b)
return tostring(a) < tostring(b)
end)


return parts
for _, k in ipairs(keys) do
end
local v = tbl[k]
local kStr = tostring(k)
local key = (prefix == "" and kStr or prefix .. "." .. kStr)


local function resolve_template_path(tplPath)
if type(v) == "table" then
local templatePath = tplPath
if next(v) ~= nil then
local project = JsonPaths.project()
local ok, json = pcall(mw.text.jsonEncode, v)
if project ~= nil and project ~= "" then
if ok and json then
templatePath = tplPath .. "/" .. project
parts[#parts + 1] = key .. "=<nowiki>" .. json .. "</nowiki>"
templatePath = "{{#ifexist:Шаблон:" .. templatePath .. "|" .. templatePath .. "|" .. tplPath .. "}}"
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


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


local function split_template_spec(tplPath, tplArgs)
local function flatten_entry(entry)
tplPath = mw.text.unstripNoWiki(tplPath or "")
local parts = flatten_parts(entry)
tplArgs = mw.text.unstripNoWiki(tplArgs or "")
if #parts == 0 then
if tplArgs ~= "" and string.sub(tplArgs, 1, 1) == "|" then
return ""
tplArgs = string.sub(tplArgs, 2)
end
end
 
return table.concat(parts, "|")
if tplArgs == "" then
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
 
return tplPath, tplArgs
end
end


local function build_tpl(id, pagePath, tplPath, data, tplArgs, preEntry)
local function append_flattened_part(parts, key, value)
if id == "" or pagePath == "" or tplPath == "" then
if value == nil then
return ""
return
end
end


local moduleName = JsonPaths.get(pagePath)
if type(value) == "table" then
data = data or load_cached_data(moduleName)
if next(value) == nil then
if not data then
return
return ""
end
end


local entry = preEntry or resolve_entry(data, id)
local ok, json = pcall(mw.text.jsonEncode, value)
local extra = flatten_entry(entry)
if ok and json then
local extraTplArgs = tplArgs or ""
parts[#parts + 1] = key .. "=<nowiki>" .. json .. "</nowiki>"
 
end
local templatePath = resolve_template_path(tplPath)
return
 
local tplStr = "{{Шаблон:" .. templatePath
if extraTplArgs ~= "" then
tplStr = tplStr .. "|" .. extraTplArgs
end
end
tplStr = tplStr .. "|id=" .. tostring(id)
if extra ~= "" then
tplStr = tplStr .. "|" .. extra
end
tplStr = tplStr .. "}}"


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


function p.findInGenerator(frame)
local function flatten_selected_parts(entry, keys)
local args = frame.args or {}
if type(entry) ~= "table" or type(keys) ~= "table" then
local searchId = args[1] or ""
return {}
local kind = (args[2] or ""):lower()
end
local fieldId = args[3] or ""


if searchId == "" or fieldId == "" then
local parts = {}
return ""
local seen = {}
end
if kind ~= "prototype" and kind ~= "component" then
return ""
end


local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
for i = 1, #keys do
local moduleName = JsonPaths.get(storeName)
local key = keys[i]
local data = load_cached_data(moduleName)
if type(key) == "string" and key ~= "" and not seen[key] then
if not data then
seen[key] = true
return ""
append_flattened_part(parts, key, navigate_path(entry, key))
end
end
end


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


local value = get_field_loose(entry, fieldId)
local function resolve_template_path(tplPath)
if value == nil then
local templatePath = tplPath
return ""
local project = JsonPaths.project()
if project ~= nil and project ~= "" then
templatePath = tplPath .. "/" .. project
templatePath = "{{#ifexist:Шаблон:" .. templatePath .. "|" .. templatePath .. "|" .. tplPath .. "}}"
end
end


local out = {}
return templatePath
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.flattenField(frame)
local function split_template_spec(tplPath, tplArgs)
local args = frame.args or {}
tplPath = mw.text.unstripNoWiki(tplPath or "")
local id = args[1] or ""
tplArgs = mw.text.unstripNoWiki(tplArgs or "")
local pagePath = args[2] or ""
if tplArgs ~= "" and string.sub(tplArgs, 1, 1) == "|" then
if id == "" or pagePath == "" then
tplArgs = string.sub(tplArgs, 2)
return ""
end
end


local moduleName = JsonPaths.get(pagePath)
if tplArgs == "" then
local data = load_cached_data(moduleName)
local pipePos = string.find(tplPath, "|", 1, true)
if not data then
if pipePos then
return ""
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


local entry = resolve_entry(data, id) or {}
return tplPath, tplArgs
return flatten_entry(entry)
end
end


function p.flattenFieldSelective(frame)
local function build_tpl(id, pagePath, tplPath, data, tplArgs, preEntry, strictExact)
local args = frame.args or {}
if id == "" or pagePath == "" or tplPath == "" then
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 entry
if not okKeys or type(keys) ~= "table" or #keys == 0 then
if strictExact then
return ""
entry = load_entry(id, pagePath, data, false)
else
entry = preEntry or load_entry(id, pagePath, data)
end
end


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


local entry = resolve_entry(data, id) or {}
local extra = flatten_entry(entry)
local parts = flatten_selected_parts(entry, keys)
local extraTplArgs = tplArgs or ""
if #parts == 0 then
 
return ""
local templatePath = resolve_template_path(tplPath)
 
local tplStr = "{{Шаблон:" .. templatePath
if extraTplArgs ~= "" then
tplStr = tplStr .. "|" .. extraTplArgs
end
tplStr = tplStr .. "|id=" .. tostring(id)
if extra ~= "" then
tplStr = tplStr .. "|" .. extra
end
end
tplStr = tplStr .. "}}"


return table.concat(parts, "|")
return tplStr
end
end


function p.get(frame)
function p.findInGenerator(frame)
local args = getArgs(frame, { removeBlanks = false })
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()
local keyPath = args[3] or ""
local fieldId = p.ucfirst(args[3] or "")


if pagePath == "" then
if searchId == "" or fieldId == "" then
return ""
end
if kind ~= "prototype" and kind ~= "component" then
return ""
return ""
end
end


local moduleName = JsonPaths.get(pagePath)
local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
local data = load_cached_data(moduleName)
local data = p.loadCachedData(storeName)
if not data then
if not data then
return ""
return ""
end
end


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


if keyPath == "" then
local value = get_field_loose(entry, fieldId)
return format_value(entry)
if value == nil then
return ""
end
end


return format_value(navigate_path(entry, keyPath))
local out = {}
end
local t = type(value)
 
if t == "table" then
local function collect_by_parsed_path(tbl, parsedPath, pos, out)
for _, v in ipairs(value) do
if pos > #parsedPath then
out[#out + 1] = v
out[#out + 1] = tbl
end
return
else
out[1] = value
end
end


if type(tbl) ~= "table" then
return mw.text.jsonEncode(out)
return
end
 
function p.flattenField(frame)
local args = frame.args or {}
local id = args[1] or ""
local pagePath = args[2] or ""
if id == "" or pagePath == "" then
return ""
end
end


local token = parsedPath[pos]
local entry = load_entry(id, pagePath) or {}
local key = token[1]
return flatten_entry(entry)
local idx = token[2]
end


if key == "*" then
function p.flattenFieldSelective(frame)
for _, child in pairs(tbl) do
local args = frame.args or {}
local nextCur = child
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


if idx then
local okKeys, keys = pcall(mw.text.jsonDecode, keysJson)
if type(nextCur) ~= "table" then
if not okKeys or type(keys) ~= "table" or #keys == 0 then
nextCur = nil
return ""
else
end
nextCur = nextCur[idx]
end
end


collect_by_parsed_path(nextCur, parsedPath, pos + 1, out)
local entry = load_entry(id, pagePath) or {}
end
local parts = flatten_selected_parts(entry, keys)
return
if #parts == 0 then
return ""
end
end


local nextCur
return table.concat(parts, "|")
end


if key and key ~= "" then
function p.get(frame)
nextCur = tbl[key]
local args = getArgs(frame, { removeBlanks = false })
if nextCur == nil then nextCur = tbl["!type:" .. key] end
local id = args[1] or ""
else
local pagePath = args[2] or ""
nextCur = tbl
local keyPath = args[3] or ""
 
if pagePath == "" then
return ""
end
 
local entry = load_entry(id, pagePath)
if entry == nil then
return ""
end
end


if idx then
if keyPath == "" then
if type(nextCur) ~= "table" then
return format_value(entry)
return
end
nextCur = nextCur[idx]
end
end


collect_by_parsed_path(nextCur, parsedPath, pos + 1, out)
return format_value(navigate_path(entry, keyPath))
end
end


local function get_by_parsed_path_multi(tbl, parsedPath)
local function collect_by_parsed_path(tbl, parsedPath, pos, out)
local out = {}
if pos > #parsedPath then
collect_by_parsed_path(tbl, parsedPath, 1, out)
out[#out + 1] = tbl
return out
return
end
end


local function entry_matches_path(entry, parsedPath, searchValue, searchType)
if type(tbl) ~= "table" then
local values = get_by_parsed_path_multi(entry, parsedPath)
return
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
end


return false
local token = parsedPath[pos]
end
local key = token[1]
local idx = token[2]
 
if key == "*" then
for _, child in pairs(tbl) do
local nextCur = child


local function entry_has_any_nonempty_path(entry, parsedPath)
if idx then
local values = get_by_parsed_path_multi(entry, parsedPath)
if type(nextCur) ~= "table" then
nextCur = nil
else
nextCur = nextCur[idx]
end
end


for _, v in ipairs(values) do
collect_by_parsed_path(nextCur, parsedPath, pos + 1, out)
if is_nonempty_value(v) then
return true
end
end
return
end
end


return false
local nextCur
end


local function split_csv_list(text)
if key and key ~= "" then
text = mw.text.unstripNoWiki(text or "")
nextCur = tbl[key]
local out = {}
if nextCur == nil then nextCur = tbl["!type:" .. key] end
else
nextCur = tbl
end


for part in string.gmatch(text, "([^,]+)") do
if idx then
part = mw.text.trim(part)
if type(nextCur) ~= "table" then
if part ~= "" then
return
out[#out + 1] = part
end
end
nextCur = nextCur[idx]
end
end


return out
collect_by_parsed_path(nextCur, parsedPath, pos + 1, out)
end
end


local function add_scalar_values(value, set)
local function get_by_parsed_path_multi(tbl, parsedPath)
if type(value) == "table" then
local out = {}
if is_array(value) then
collect_by_parsed_path(tbl, parsedPath, 1, out)
for _, item in ipairs(value) do
return out
add_scalar_values(item, set)
end
else
for _, item in pairs(value) do
add_scalar_values(item, set)
end
end
elseif value ~= nil then
set[tostring(value)] = true
end
end
end


local function table_has_all_values(value, targets)
local function entry_matches_path(entry, parsedPath, searchValue, searchType)
if type(targets) ~= "table" or #targets == 0 then
local values = get_by_parsed_path_multi(entry, parsedPath)
return false
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
end


if type(value) ~= "table" then
return false
return #targets == 1 and tostring(value) == targets[1]
end
end


local set = {}
local function entry_has_any_nonempty_path(entry, parsedPath)
add_scalar_values(value, set)
local values = get_by_parsed_path_multi(entry, parsedPath)


for i = 1, #targets do
for _, v in ipairs(values) do
if not set[targets[i]] then
if is_nonempty_value(v) then
return false
return true
end
end
end
end


return true
return false
end
end


function p.searchId(frame)
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
 
function p.searchId(frame)
local args = getArgs(frame, { removeBlanks = false })
local args = getArgs(frame, { removeBlanks = false })
local searchValue = args[1] or ""
local searchValue = args[1] or ""
Строка 713: Строка 758:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = p.loadCachedData(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return "[]"
return "[]"
Строка 733: Строка 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
Строка 751: Строка 795:
end
end


function p.search(frame)
local function collect_ids_from_entry(entry)
local args = getArgs(frame, { removeBlanks = false })
local out = {}
local mode = (args[1] or ""):lower()
local seen = {}
local rawList = args[2] or ""


if mode ~= "component" and mode ~= "tag" then
local function add(v)
return "[]"
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
end


local targets = split_csv_list(rawList)
if type(entry) == "table" then
if #targets == 0 then
if is_array(entry) then
return "[]"
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
end


local pagePath
return out
if mode == "component" then
end
pagePath = "component.json"
 
else
function p.search(frame)
pagePath = "component/tag.json"
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
end


local moduleName = JsonPaths.get(pagePath)
local targets = split_csv_list(rawList)
local data = load_cached_data(moduleName)
if #targets == 0 then
if not data or type(data) ~= "table" then
return ""
return "[]"
end
end


local ids = collect_id_keys(data)
local pagePath = (mode == "component") and "component.json" or "tag.json"
if #ids == 0 then
local data = p.loadCachedData(pagePath)
return "[]"
if not data then
return ""
end
end


table.sort(ids, function(a, b)
local matches = {}
return tostring(a) < tostring(b)
local seen = {}
end)


local function entry_has_all_targets_component(entry, wanted)
local function add_id(id)
if type(entry) ~= "table" then
local s = tostring(id)
return false
if s ~= "" and not seen[s] then
end
seen[s] = true
 
matches[#matches + 1] = s
local set = {}
for _, v in ipairs(entry) do
set[tostring(v)] = true
end
end
end


for i = 1, #wanted do
for _, key in ipairs(targets) do
if not set[wanted[i]] then
local entry = resolve_entry_from_data(data, key)
return false
if entry ~= nil then
local ids = collect_ids_from_entry(entry)
for _, id in ipairs(ids) do
add_id(id)
end
end
end
end
return true
end
end


local function entry_has_all_targets_tag(entry, wanted)
table.sort(matches, function(a, b)
if type(entry) ~= "table" then
return tostring(a) < tostring(b)
return false
end)
end


local tags = entry.tags or entry.tag
local ok, json = pcall(mw.text.jsonEncode, matches)
if type(tags) ~= "table" then
if ok and json then
return false
return json
end
end


local set = {}
return ""
if is_array(tags) then
end
for _, v in ipairs(tags) do
set[tostring(v)] = true
end
else
for _, v in pairs(tags) do
set[tostring(v)] = true
end
end


for i = 1, #wanted do
function p.searchIdTpl(frame)
if not set[wanted[i]] then
return false
end
end
 
return true
end
 
local matches = {}
 
for _, idKey in ipairs(ids) do
local entry = resolve_entry(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
 
function p.searchIdTpl(frame)
local args = getArgs(frame, { removeBlanks = false })
local args = getArgs(frame, { removeBlanks = false })
local searchValue = args[1] or ""
local searchValue = args[1] or ""
Строка 889: Строка 916:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = p.loadCachedData(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
Строка 910: Строка 936:
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: Строка 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
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
Строка 933: Строка 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
Строка 959: Строка 985:
end
end


local moduleName = JsonPaths.get(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)
return preprocess_or_return(frame, tplStr)
end
if not data then
return ""
end
 
local tplStr = build_tpl(id, pagePath, tplPath, data, tplArgs)
return preprocess_or_return(frame, tplStr)
end
end


Строка 976: Строка 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 ""
Строка 997: Строка 1015:
end
end


local moduleName = JsonPaths.get(pagePath)
local data = p.loadCachedData(pagePath)
local data = load_cached_data(moduleName)
if not data then
if not data then
return ""
return ""
Строка 1005: Строка 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
Строка 1023: Строка 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
Строка 1046: Строка 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


if entityId == "" or compName == "" then
local pagePath = "prototype/Entity/" .. entityId .. ".json"
return "false"
return p.loadCachedData(pagePath)
end
end


local moduleName = JsonPaths.get("component.json")
function p.entityHasComponent(entity, compName)
local data = load_cached_data(moduleName)
if type(entity) ~= "table" or type(entity.components) ~= "table" then
if not data then
return false
return "false"
end
end


if type(data) ~= "table" then
return entity.components[compName] ~= nil
return "false"
or entity.components["type:" .. compName] ~= nil
end
or entity.components["!type:" .. compName] ~= nil
end


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


for _, v in ipairs(entry) do
local out = {}
if tostring(v) == compName then
local seen = {}
return "true"
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
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 moduleName = JsonPaths.get("component.json")
local entity = p.loadEntityData(entityId)
local data = load_cached_data(moduleName)
if not entity then
if not data or type(data) ~= "table" then
return "false"
return "[]"
end
end


local entry = data[entityId]
return p.entityHasComponent(entity, compName) and "true" or "false"
if type(entry) ~= "table" then
end
return "[]"
end


local out = {}
function p.getComp(frame)
for _, v in ipairs(entry) do
local args = getArgs(frame, { removeBlanks = false })
if v ~= nil and v ~= "" then
local entityId = args[1] or ""
out[#out + 1] = v
 
end
if entityId == "" then
return ""
end
 
local entity = p.loadEntityData(entityId)
if not entity then
return ""
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
Строка 1110: Строка 1152:
end
end


return "[]"
return ""
end
end


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


local moduleName = JsonPaths.get(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 ""
Строка 1165: Строка 1206:
function p.getAllTpl(frame)
function p.getAllTpl(frame)
local args = getArgs(frame, { removeBlanks = false })
local args = getArgs(frame, { removeBlanks = false })
local pagePath = args[1] or ""
local pagePath = args[1] or ""
local tplPath = args[2] or ""
local tplPath = args[2] or ""
local tplArgs = args[3] or args.tplArgs or args.templateArgs or ""
local tplArgs = args[3] or args.tplArgs or args.templateArgs or ""
tplPath, tplArgs = split_template_spec(tplPath, tplArgs)
tplPath, tplArgs = split_template_spec(tplPath, tplArgs)
 
 
if pagePath == "" or tplPath == "" then
if pagePath == "" or tplPath == "" then
return ""
return ""
end
end
 
 
local moduleName = JsonPaths.get(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 or data
local idsTable = data.id
if type(idsTable) ~= "table" then
if type(idsTable) ~= "table" then
return ""
return ""
end
end
 
 
local out = {}
local out = {}
 
 
for idKey in pairs(idsTable) do
for idKey in pairs(idsTable) do
local entry = resolve_entry_from_data(data, idKey)
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs)
if entry ~= nil then
if tpl ~= "" then
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entry, true)
out[#out + 1] = tpl
if tpl ~= "" then
out[#out + 1] = tpl
end
end
end
end
end
Строка 1624: Строка 1667:
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