|
|
| Строка 1: |
Строка 1: |
| local p = {} | | local p = {} |
| | |
| local BASE_USER = "IanComradeBot/" | | local BASE_USER = "IanComradeBot/" |
| local CURRENT_PROJECT
| |
|
| |
| local function try_call_expand(node)
| |
| if type(node) ~= "table" then return nil end
| |
| local f = node.expand
| |
| if type(f) ~= "function" then return nil end
| |
| local ok, res = pcall(function() return f(node) end)
| |
| if ok and res and res ~= "" then return res end
| |
| ok, res = pcall(function() return f() end)
| |
| if ok and res and res ~= "" then return res end
| |
| ok, res = pcall(f, node)
| |
| if ok and res and res ~= "" then return res end
| |
| return nil
| |
| end
| |
|
| |
| local function gather_text(x, out, seen)
| |
| out = out or {}
| |
| seen = seen or {}
| |
| if type(x) == "string" then
| |
| table.insert(out, x)
| |
| return out
| |
| elseif type(x) == "number" or type(x) == "boolean" then
| |
| table.insert(out, tostring(x))
| |
| return out
| |
| elseif type(x) == "table" then
| |
| if seen[x] then return out end
| |
| seen[x] = true
| |
| local s = try_call_expand(x)
| |
| if s and s ~= "" then
| |
| table.insert(out, s)
| |
| return out
| |
| end
| |
| if x.text and type(x.text) == "string" and x.text ~= "" then
| |
| table.insert(out, x.text)
| |
| end
| |
| local i = 1
| |
| while x[i] ~= nil do
| |
| gather_text(x[i], out, seen)
| |
| i = i + 1
| |
| end
| |
| for k, v in pairs(x) do
| |
| if type(k) ~= "number" then
| |
| if type(v) == "string" or type(v) == "number" or type(v) == "boolean" then
| |
| table.insert(out, tostring(v))
| |
| elseif type(v) == "table" then
| |
| gather_text(v, out, seen)
| |
| end
| |
| end
| |
| end
| |
| return out
| |
| end
| |
| return out
| |
| end
| |
|
| |
| local function extract_string(v)
| |
| if v == nil then return nil end
| |
| if type(v) == "string" then if v == "" then return nil end; return v end
| |
| if type(v) == "number" or type(v) == "boolean" then return tostring(v) end
| |
| if type(v) == "table" then
| |
| local parts = gather_text(v)
| |
| if #parts == 0 then return nil end
| |
| local s = table.concat(parts, "")
| |
| if s == "" then return nil end
| |
| return s
| |
| end
| |
| return nil
| |
| end
| |
|
| |
| local function short_serialize(obj, depth, seen)
| |
| depth = depth or 0
| |
| seen = seen or {}
| |
| if type(obj) ~= "table" then return tostring(obj) end
| |
| if seen[obj] then return "<cycle>" end
| |
| if depth > 2 then return "{...}" end
| |
| seen[obj] = true
| |
| local parts = {}
| |
| local i = 1
| |
| while obj[i] ~= nil do
| |
| table.insert(parts, short_serialize(obj[i], depth+1, seen))
| |
| i = i + 1
| |
| end
| |
| for k,v in pairs(obj) do
| |
| if type(k) ~= "number" then
| |
| table.insert(parts, tostring(k) .. "=" .. short_serialize(v, depth+1, seen))
| |
| end
| |
| end
| |
| return "{" .. table.concat(parts, ", ") .. "}"
| |
| end
| |
|
| |
| function p.set_path(frame)
| |
| local raw
| |
| if type(frame) == "table" and frame.args then
| |
| raw = frame.args[1] or nil
| |
| if not raw then
| |
| local ok, a = pcall(function() return frame:getArgument(1) end)
| |
| if ok then raw = a end
| |
| end
| |
| if not raw then
| |
| local ok2, a2 = pcall(function() return frame:getArgument("GetFieldPath") end)
| |
| if ok2 then raw = a2 end
| |
| end
| |
| elseif type(frame) == "string" then
| |
| raw = frame
| |
| end
| |
|
| |
| local val = extract_string(raw)
| |
| if val and val ~= "" then
| |
| CURRENT_PROJECT = val
| |
| return "OK:" .. tostring(CURRENT_PROJECT)
| |
| end
| |
|
| |
| local diag = "NOT_SET"
| |
| if type(frame) == "table" then
| |
| diag = diag .. "; raw_args=" .. short_serialize(frame.args or {})
| |
| local ok1, a1 = pcall(function() return frame:getArgument(1) end)
| |
| diag = diag .. "; getArgument(1)=" .. (ok1 and short_serialize(a1) or "(err)")
| |
| local ok2, a2 = pcall(function() return frame:getArgument("GetFieldPath") end)
| |
| diag = diag .. "; getArgument('GetFieldPath')=" .. (ok2 and short_serialize(a2) or "(err)")
| |
| else
| |
| diag = diag .. "; frame_type=" .. type(frame) .. "; frame_val=" .. tostring(frame)
| |
| end
| |
| return diag
| |
| end
| |
|
| |
| function p._debug_get_cached()
| |
| if not CURRENT_PROJECT then return "(nil)" end
| |
| return tostring(CURRENT_PROJECT)
| |
| end
| |
|
| |
| function p.inspect(frame)
| |
| local f = frame or mw.getCurrentFrame()
| |
| if not f then return "NO_FRAME" end
| |
| local out = {}
| |
| local depth = 0
| |
| while f and depth < 10 do
| |
| table.insert(out, ("FRAME depth=" .. depth))
| |
| table.insert(out, (" args = " .. short_serialize(f.args or {})))
| |
| local ok1, a1 = pcall(function() return f:getArgument(1) end)
| |
| table.insert(out, (" getArgument('1') = " .. (ok1 and short_serialize(a1) or "(err)")))
| |
| local okN, aN = pcall(function() return f:getArgument("GetFieldPath") end)
| |
| table.insert(out, (" getArgument('GetFieldPath') = " .. (okN and short_serialize(aN) or "(err)")))
| |
| table.insert(out, (" has:getParent = " .. tostring(type(f.getParent) == "function")))
| |
| table.insert(out, (" has:callParserFunction = " .. tostring(type(f.callParserFunction) == "function")))
| |
| f = (type(f.getParent) == "function") and f:getParent() or nil
| |
| depth = depth + 1
| |
| end
| |
| table.insert(out, ("CURRENT_PROJECT = " .. tostring(CURRENT_PROJECT)))
| |
| return table.concat(out, "\n")
| |
| end
| |
|
| |
|
| function p.get_module_name(frame, pagePath) | | local function get_module_name(pagePath) |
| local project = pagePath and extract_string(pagePath) or CURRENT_PROJECT
| | return "Module:" .. BASE_USER .. pagePath .. "/data" |
| if (not project or project == "") and frame then
| |
| local ok, res = pcall(function() return frame:callParserFunction{ name = "var", args = { "GetFieldPath" } } end)
| |
| if ok and res and res ~= "" then project = tostring(res) end
| |
| end
| |
| if not project or project == "" then project = "default" end
| |
| return "Module:" .. BASE_USER .. project .. "/data" | |
| end | | end |
|
| |
|