Module:Wikidane

From WikiZagłębie

Documentation for this module may be created at Module:Wikidane/doc

-- Moduł:Wikidane – wersja odporna na brak Wikibase Client
local moduleData
pcall(function() moduleData = mw.loadData("Moduł:Wikidane/data") end)
pcall(function() moduleData = moduleData or mw.loadData("Module:Wikidane/data") end)
moduleData = moduleData or {
    errorMissingPropertyId = "(property ID missing)",
    warnDump = "",
    itemWithoutLabelCat = "",
}

-- Pomocniczo: bezpieczna referencja do mw.wikibase (może być nil)
local wb = mw.wikibase

local function hasWB()
    return wb and type(wb.getEntity) == "function"
end

local function loadArg(frame, id)
    local result = frame.args[id]
    if not result or (#result == 0) then
        result = frame.getParent and frame:getParent().args[id] or nil
        if not result or (#result == 0) then
            return nil
        end
    end
    return result
end

local function getEntityData(frame)
    if not hasWB() then
        return nil
    end
    local qid = loadArg(frame, "id")
    local data = wb.getEntity(qid)
    if not data then
        return
    end

    local f = frame.args[1] and frame or frame:getParent()
    local i = 1
    if f.args[1] == "bestclaims" then
        local pid = f.args[2]
        if not pid then
            return f, {}, 2
        end

        -- UWAGA NA NAZWY: utrzymuj spójnie "Moduł:" ALBO "Module:".
        local sd_ok, sd = pcall(require, "Moduł:Wikidane/select")
        if not sd_ok then sd_ok, sd = pcall(require, "Module:Wikidane/select") end
        if not sd_ok then return nil end

        local _, _qid, prop = sd.selectProperty(pid, sd.prepareFilters(frame), qid, sd.prepareSnakFilter(frame))
        if not _qid then
            return nil
        end
        data = prop
        i = 3
    end
    return f, data, i
end

return {

    T = function(frame)
        if not hasWB() then return 0 end
        local qid = loadArg(frame, "id")
        local pid = loadArg(frame, 1)
        if not pid then
            return moduleData.errorMissingPropertyId
        end

        local ok, sd = pcall(require, "Moduł:Wikidane/select")
        if not ok then ok, sd = pcall(require, "Module:Wikidane/select") end
        if not ok then return 0 end

        local _pid, _qid, prop = sd.selectProperty(pid, sd.prepareFilters(frame), qid, false, loadArg(frame, "ranga"), sd.prepareSnakFilter(frame))
        if _qid then
            return #prop
        end
        return 0
    end,

    P = function(frame)
        if not hasWB() then return "" end
        local qid = loadArg(frame, "id")
        local pid = loadArg(frame, 1)
        if not pid then
            return moduleData.errorMissingPropertyId
        end

        local okS, sd = pcall(require, "Moduł:Wikidane/select")
        if not okS then okS, sd = pcall(require, "Module:Wikidane/select") end
        if not okS then return "" end

        local okF, fmt = pcall(require, "Moduł:Wikidane/format")
        if not okF then okF, fmt = pcall(require, "Module:Wikidane/format") end
        if not okF then return "" end

        local _pid, _qid, prop = sd.selectProperty(
            pid, sd.prepareFilters(frame), qid,
            tonumber(loadArg(frame, "ile")),
            loadArg(frame, "ranga"),
            sd.prepareSnakFilter(frame)
        )
        if _qid then
            return fmt.run(frame, _pid, prop)
        end
        return ""
    end,

    etykieta = function(frame)
        if hasWB() then
            local qid = loadArg(frame, "id")
            local entity = wb.getEntity(qid)
            if entity then
                local i, lang = 1, true
                while lang do
                    lang = frame.args[i]
                    local result = entity:getLabel(lang)
                    if result and (#result > 0) then
                        return result
                    end
                    i = i + 1
                end
            end
        end
        return mw.title.getCurrentTitle().text .. (moduleData.itemWithoutLabelCat or "")
    end,

    id = function(frame)
        if not hasWB() then return nil end
        local title = loadArg(frame, 1)
        if title then
            return wb.getEntityIdForTitle(mw.getContentLanguage():ucfirst(title), loadArg(frame, "idwiki") or wb.getGlobalSiteId())
        else
            return wb.getEntityIdForCurrentPage()
        end
    end,

    label = function(frame)
        if not hasWB() then return "" end
        local id = loadArg(frame, 1)
        if id and string.match(id, "^[PQ]%d+$") then
            return wb.getLabel(id) or ""
        end
        return ""
    end,

    page = function(frame)
        if not hasWB() then return nil end
        local id = loadArg(frame, 1)
        if id then
            return wb.getSitelink(id, loadArg(frame, "site"))
        end
        return nil
    end,

    V = function(frame)
        local f, data, i = getEntityData(frame)
        if not data then return nil end
        while true do
            local index = f.args[i]
            if not index then
                return tostring(data)
            end
            data = data[index] or data[tonumber(index)]
            if not data then return nil end
            i = i + 1
        end
    end,

    Dump = function(frame)
        local f, data, i = getEntityData(frame)
        if not data then return moduleData.warnDump end
        while true do
            local index = f.args[i]
            if not index then
                return "<pre>" .. mw.dumpObject(data) .. "</pre>" .. (moduleData.warnDump or "")
            end
            data = data[index] or data[tonumber(index)]
            if not data then return moduleData.warnDump end
            i = i + 1
        end
    end,
}