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

Нет описания правки
Нет описания правки
Строка 525: Строка 525:
function p.GeneratorId(frame)
function p.GeneratorId(frame)
     local args = frame.args or {}
     local args = frame.args or {}
     local generatorId = args[1] or ""
     local jsonStr = args[1] or ""
     local pagePath = args[2] or ""
     if jsonStr == "" then return "" end


     if generatorId == "" or pagePath == "" then
    local ok, data = pcall(mw.text.jsonDecode, jsonStr)
     if not ok or type(data) ~= "table" then
         return ""
         return ""
     end
     end


     local moduleName = get_module_name(pagePath .. ".json")
     local ids = {}
    local data = load_cached_data(moduleName)
     if type(data.id) == "table" then
     if not data or type(data) ~= "table" then
         for k in pairs(data.id) do
         return ""
            ids[#ids + 1] = k
        end
     end
     end


     local entry = data[generatorId]
     table.sort(ids)
    if entry == nil then
        return ""
    end


    if type(entry) ~= "table" then
     local okOut, outJson = pcall(mw.text.jsonEncode, ids)
        local okSingle, jsonSingle = pcall(mw.text.jsonEncode, { tostring(entry) })
     if okOut and outJson then
        if okSingle then
         return outJson
            return jsonSingle
        end
        return ""
    end
 
     local ok, json = pcall(mw.text.jsonEncode, entry)
     if ok then
         return json
     end
     end


     return ""
     return ""
end
end


function p.GeneratorTplId(frame)
function p.GeneratorTplId(frame)
     local args = frame.args or {}
     local args = frame.args or {}
     local generatorId = args[1] or ""
     local jsonStr = args[1] or ""
     local pagePath = args[2] or ""
     local tplPath = args[2] or ""
    local tplPath = args[3] or ""


     if generatorId == "" or pagePath == "" or tplPath == "" then
     if jsonStr == "" or tplPath == "" or pagePath == "" then
         return ""
         return ""
     end
     end


     local moduleName = get_module_name(pagePath .. ".json")
     local ok, data = pcall(mw.text.jsonDecode, jsonStr)
    local data = load_cached_data(moduleName)
     if not ok or type(data) ~= "table" then
     if not data or type(data) ~= "table" then
         return ""
         return ""
     end
     end


     local entry = data[generatorId]
     local idsTable = data.id
     if type(entry) ~= "table" then
     if type(idsTable) ~= "table" then
         return ""
         return ""
     end
     end


     local tplPagePath = pagePath .. "/" .. generatorId .. ".json"
     local out = {}


    local out = {}
     for idKey in pairs(idsTable) do
     for _, id in ipairs(entry) do
         local tpl = p.getTpl({ args = { idKey, jsonStr, tplPath } })
         local tpl = p.getTpl({ args = { id, tplPagePath, tplPath } })
         if tpl ~= "" then
         if tpl ~= "" then
             out[#out + 1] = tpl
             out[#out + 1] = tpl
Строка 591: Строка 578:
     end
     end


     if #out == 0 then
     table.sort(out)
        return ""
    end


     local result = table.concat(out, " ")
     local result = table.concat(out, " ")