Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показано 7 промежуточных версий этого же участника) | |||
| Строка 44: | Строка 44: | ||
return tostring(v) | return tostring(v) | ||
elseif t == "table" then | elseif t == "table" then | ||
local | local ok, json = pcall(mw.text.jsonEncode, v) | ||
if ok and json then | |||
return json | |||
if | |||
return | |||
end | end | ||
return "" | |||
else | else | ||
return tostring(v) | return tostring(v) | ||
| Строка 147: | Строка 129: | ||
end | end | ||
function p. | function p.findInGenerator(frame) | ||
local args = frame.args or {} | local args = frame.args or {} | ||
local | local searchId = args[1] or "" | ||
local fieldId = args[ | local kind = (args[2] or ""):lower() | ||
local fieldId = args[3] or "" | |||
if | if searchId == "" or fieldId == "" then | ||
return "" | |||
end | |||
if kind ~= "prototype" and kind ~= "component" then | |||
return "" | return "" | ||
end | end | ||
local | local baseUser = "IanComradeBot/" | ||
local storeName | |||
if kind == "prototype" then | |||
storeName = "prototype_store.json" | |||
else | |||
storeName = "component_store.json" | |||
end | |||
local moduleName = "Module:" .. baseUser .. storeName .. "/data" | |||
local data = cache[moduleName] | local data = cache[moduleName] | ||
| Строка 168: | Строка 161: | ||
end | end | ||
local | local entry = data[searchId] | ||
if type( | if type(entry) ~= "table" then | ||
return "" | return "" | ||
end | end | ||
local value = | local value = entry[fieldId] | ||
if value == nil then | if value == nil then | ||
return "" | return "" | ||
| Строка 278: | Строка 271: | ||
local value = get_by_path(entry, keyPath) | local value = get_by_path(entry, keyPath) | ||
return format_value(value) | return format_value(value) | ||
end | |||
function p.getId(frame) | |||
local args = frame.args or {} | |||
local searchValue = args[1] or "" | |||
local pagePath = args[2] or "" | |||
local keyPath = args[3] or "" | |||
if searchValue == "" or pagePath == "" or keyPath == "" then | |||
return "" | |||
end | |||
local baseUser = "IanComradeBot/" | |||
local moduleName = "Module:" .. baseUser .. pagePath .. "/data" | |||
local data = cache[moduleName] | |||
if not data then | |||
local ok, loaded = pcall(mw.loadData, moduleName) | |||
if not ok or not loaded then return "" end | |||
data = loaded | |||
cache[moduleName] = data | |||
end | |||
local idsTable = data.id | |||
if type(idsTable) ~= "table" then | |||
return "" | |||
end | |||
local target = tostring(searchValue) | |||
for idKey, entry in pairs(idsTable) do | |||
if type(entry) == "table" then | |||
local v = get_by_path(entry, keyPath) | |||
if v ~= nil then | |||
if type(v) == "table" then | |||
if is_array(v) then | |||
for _, item in ipairs(v) do | |||
if tostring(item) == target then | |||
return idKey | |||
end | |||
end | |||
else | |||
for _, item in pairs(v) do | |||
if tostring(item) == target then | |||
return idKey | |||
end | |||
end | |||
end | |||
else | |||
if tostring(v) == target then | |||
return idKey | |||
end | |||
end | |||
end | |||
end | |||
end | |||
return "" | |||
end | |||
function p.getTplId(frame) | |||
local args = frame.args or {} | |||
local searchValue = args[1] or "" | |||
local pagePath = args[2] or "" | |||
local keyPath = args[3] or "" | |||
local tplPath = mw.text.unstripNoWiki(args[4] or "") | |||
if searchValue == "" or pagePath == "" or keyPath == "" or tplPath == "" then | |||
return "" | |||
end | |||
local baseUser = "IanComradeBot/" | |||
local moduleName = "Module:" .. baseUser .. pagePath .. "/data" | |||
local data = cache[moduleName] | |||
if not data then | |||
local ok, loaded = pcall(mw.loadData, moduleName) | |||
if not ok or not loaded then return "" end | |||
data = loaded | |||
cache[moduleName] = data | |||
end | |||
local idsTable = data.id | |||
if type(idsTable) ~= "table" then | |||
return "" | |||
end | |||
local target = tostring(searchValue) | |||
local matches = {} | |||
for idKey, entry in pairs(idsTable) do | |||
if type(entry) == "table" then | |||
local v = get_by_path(entry, keyPath) | |||
if v ~= nil then | |||
if type(v) == "table" then | |||
if is_array(v) then | |||
for _, item in ipairs(v) do | |||
if tostring(item) == target then | |||
matches[#matches + 1] = idKey | |||
break | |||
end | |||
end | |||
else | |||
for _, item in pairs(v) do | |||
if tostring(item) == target then | |||
matches[#matches + 1] = idKey | |||
break | |||
end | |||
end | |||
end | |||
else | |||
if tostring(v) == target then | |||
matches[#matches + 1] = idKey | |||
end | |||
end | |||
end | |||
end | |||
end | |||
if #matches == 0 then | |||
return "" | |||
end | |||
local out = {} | |||
for _, idKey in ipairs(matches) do | |||
local tpl = p.getTpl({ args = { idKey, pagePath, tplPath } }) | |||
if tpl ~= "" then | |||
out[#out + 1] = tpl | |||
end | |||
end | |||
if #out == 0 then | |||
return "" | |||
end | |||
local result = table.concat(out, "\n") | |||
if type(frame.preprocess) == "function" then | |||
return frame:preprocess(result) | |||
end | |||
return result | |||
end | end | ||
| Строка 304: | Строка 438: | ||
end | end | ||
function p. | function p.getTplGenerator(frame) | ||
local args = frame.args or {} | local args = frame.args or {} | ||
local searchId = args[1] or "" | local searchId = args[1] or "" | ||
local | local kind = (args[2] or ""):lower() | ||
local | local generatorId = args[3] or "" | ||
local | local tplPath = args[4] or "" | ||
if searchId == "" or | if searchId == "" or generatorId == "" or tplPath == "" then | ||
return "" | return "" | ||
end | end | ||
if kind ~= "prototype" and kind ~= "component" then | |||
return "" | |||
end | |||
local dir = (kind == "prototype") and "prototype/" or "component/" | |||
local pagePath = dir .. generatorId .. ".json" | |||
local idsJson = p. | local idsJson = p.findInGenerator({ args = { searchId, kind, generatorId } }) | ||
local ok, ids = pcall(mw.text.jsonDecode, idsJson or "") | local ok, ids = pcall(mw.text.jsonDecode, idsJson or "") | ||
if not ok or type(ids) ~= "table" or #ids == 0 then | if not ok or type(ids) ~= "table" or #ids == 0 then | ||