Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| (не показаны 4 промежуточные версии этого же участника) | |||
| Строка 646: | Строка 646: | ||
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 | |||
local function add_scalar_values(value, set) | |||
if type(value) == "table" then | |||
if is_array(value) then | |||
for _, item in ipairs(value) do | |||
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 | |||
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 | end | ||
| Строка 698: | Строка 749: | ||
return "" | return "" | ||
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 | |||
if mode == "component" then | |||
pagePath = "component.json" | |||
else | |||
pagePath = "component/tag.json" | |||
end | |||
local moduleName = JsonPaths.get(pagePath) | |||
local data = load_cached_data(moduleName) | |||
if not data or type(data) ~= "table" then | |||
return "[]" | |||
end | |||
local ids = collect_id_keys(data) | |||
if #ids == 0 then | |||
return "[]" | |||
end | |||
table.sort(ids, function(a, b) | |||
return tostring(a) < tostring(b) | |||
end) | |||
local function entry_has_all_targets_component(entry, wanted) | |||
if type(entry) ~= "table" then | |||
return false | |||
end | |||
local set = {} | |||
for _, v in ipairs(entry) do | |||
set[tostring(v)] = true | |||
end | |||
for i = 1, #wanted do | |||
if not set[wanted[i]] then | |||
return false | |||
end | |||
end | |||
return true | |||
end | |||
local function entry_has_all_targets_tag(entry, wanted) | |||
if type(entry) ~= "table" then | |||
return false | |||
end | |||
local tags = entry.tags or entry.tag | |||
if type(tags) ~= "table" then | |||
return false | |||
end | |||
local set = {} | |||
if is_array(tags) then | |||
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 | |||
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 | end | ||
| Строка 918: | Строка 1077: | ||
return "false" | return "false" | ||
end | |||
function p.getComp(frame) | |||
local args = getArgs(frame, { removeBlanks = false }) | |||
local entityId = args[1] or "" | |||
if entityId == "" then | |||
return "[]" | |||
end | |||
local moduleName = JsonPaths.get("component.json") | |||
local data = load_cached_data(moduleName) | |||
if not data or type(data) ~= "table" then | |||
return "[]" | |||
end | |||
local entry = data[entityId] | |||
if type(entry) ~= "table" then | |||
return "[]" | |||
end | |||
local out = {} | |||
for _, v in ipairs(entry) do | |||
if v ~= nil and v ~= "" then | |||
out[#out + 1] = v | |||
end | |||
end | |||
local ok, json = pcall(mw.text.jsonEncode, out) | |||
if ok and json then | |||
return json | |||
end | |||
return "[]" | |||
end | end | ||
| Строка 1299: | Строка 1492: | ||
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 = "" | ||
| Строка 1346: | Строка 1554: | ||
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 | ||
| Строка 1402: | Строка 1617: | ||
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 moduleName = JsonPaths.get(dataPage) | |||
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) | |||
if #parts == 0 then | |||
return "" | |||
end | |||
return table.concat(parts, "|") | |||
end | end | ||
return p | return p | ||