Модуль:Песочница/Pok: различия между версиями

Нет описания правки
Нет описания правки
Строка 3: Строка 3:
local CURRENT_PROJECT
local CURRENT_PROJECT


local function get_page_var(frame)
local function normalize_value(v)
if mw.ext and mw.ext.VariablesLua and type(mw.ext.VariablesLua.var) == "function" then
    if type(v) == "string" then
local v = mw.ext.VariablesLua.var("GetFieldPath")
        return v
if v and v ~= "" then return v end
    elseif type(v) == "table" then
end
        -- если таблица, попробуем взять первый элемент либо первый named value
if frame then
        if v[1] and v[1] ~= "" then return v[1] end
local ok, v = pcall(function() return frame:callParserFunction{ name = "var", args = { "GetFieldPath" } } end)
        for _, val in pairs(v) do
if ok and v and v ~= "" then return v end
            if type(val) == "string" and val ~= "" then return val end
end
        end
return nil
        return nil
end
    elseif v ~= nil then
 
        return tostring(v)
local function read_ancestor_arg()
    end
local f = mw.getCurrentFrame()
    return nil
while f do
local v = f:getArgument("Название") or f:getArgument("project") or f:getArgument("path")
if v and v ~= "" then return v end
f = f:getParent()
end
return nil
end
 
local function resolve_project(frame, pagePath)
if pagePath and pagePath ~= "" then return pagePath end
if CURRENT_PROJECT and CURRENT_PROJECT ~= "" then return CURRENT_PROJECT end
local v = get_page_var(frame)
if v and v ~= "" then CURRENT_PROJECT = v; return v end
local ancestor = read_ancestor_arg()
if ancestor and ancestor ~= "" then CURRENT_PROJECT = ancestor; return ancestor end
return nil
end
end


function p.set_path(frame)
function p.set_path(frame)
local arg
    local arg
if frame and frame.args and frame.args[1] then
    if frame then
arg = frame.args[1]
        -- стандартный путь: позиционный первый аргумент
end
        if frame.args and frame.args[1] then arg = frame.args[1] end
if (not arg or arg == "") and frame then
        -- если не получилось — пробуем getArgument
arg = frame:getArgument("1") or frame:getArgument("path") or frame:getArgument("GetFieldPath")
        if (not arg or arg == "") then arg = frame:getArgument(1) or frame:getArgument("path") or frame:getArgument("GetFieldPath") end
end
    else
if arg and arg ~= "" then
        arg = nil
CURRENT_PROJECT = arg
    end
return ""
    local val = normalize_value(arg)
end
    if val and val ~= "" then
return ""
        CURRENT_PROJECT = val
        return ""  
    end
    return ""
end
end


function p._debug_get_cached(frame)
function p._debug_get_cached(frame)
local v = CURRENT_PROJECT or get_page_var(frame) or read_ancestor_arg() or "(nil)"
    local v = CURRENT_PROJECT
return tostring(v)
    if not v then
        -- попробуем получить через frame (на случай #vardefine/var)
        if frame and frame.callParserFunction then
            local ok, res = pcall(function() return frame:callParserFunction{ name = "var", args = { "GetFieldPath" } } end)
            if ok and res and res ~= "" then v = res end
        end
    end
    if type(v) == "table" then
        local parts = {}
        for k, val in pairs(v) do
            table.insert(parts, tostring(k) .. "=" .. tostring(val))
        end
        return "table: {" .. table.concat(parts, ", ") .. "}"
    end
    return tostring(v or "(nil)")
end
end


function p.get_module_name(frame, pagePath)
function p.get_module_name(frame, pagePath)
local fp, pp = frame, pagePath
    local fp, pp = frame, pagePath
if type(frame) == "string" and (not pagePath) then
    if type(frame) == "string" and (not pagePath) then
pp = frame
        pp = frame
fp = nil
        fp = nil
end
    end
local project = resolve_project(fp, pp) or "default"
    local project = pp and normalize_value(pp) or CURRENT_PROJECT
return "Module:" .. BASE_USER .. project .. "/data"
    if (not project or project == "") and fp then
        -- попытка через #var
        local ok, res = pcall(function() return fp:callParserFunction{ name = "var", args = { "GetFieldPath" } } end)
        if ok and res and res ~= "" then project = res end
    end
    if not project or project == "" then project = "default" end
    return "Module:" .. BASE_USER .. project .. "/data"
end
end