Module:WikidataTable

De Wikiromandie.org
Version datée du 9 avril 2026 à 20:16 par AdrianoRutz (discussion | contributions) (Page créée avec « local p = {} local http = mw.http local json = mw.text.jsonDecode function p.render(frame) local sparql = frame.args.sparql local columns = frame.args.columns or "itemLabel" local title = frame.args.title if not sparql or sparql == "" then return "⚠️ No SPARQL query provided." end local url = "https://query.wikidata.org/sparql?format=json&query=" .. mw.uri.encode(sparql) local response = http.get(url) if... »)
(diff) ← Version précédente | Version actuelle (diff) | Version suivante → (diff)
Aller à la navigation Aller à la recherche

La documentation pour ce module peut être créée à Module:WikidataTable/doc

local p = {}

local http = mw.http
local json = mw.text.jsonDecode

function p.render(frame)
    local sparql = frame.args.sparql
    local columns = frame.args.columns or "itemLabel"
    local title = frame.args.title

    if not sparql or sparql == "" then
        return "⚠️ No SPARQL query provided."
    end

    local url = "https://query.wikidata.org/sparql?format=json&query=" 
        .. mw.uri.encode(sparql)

    local response = http.get(url)

    if response.status ~= 200 then
        return "⚠️ Error fetching Wikidata."
    end

    local data = json(response.body)
    local vars = data.head.vars
    local rows = data.results.bindings

    local html = {}

    -- Title
    if title and title ~= "" then
        table.insert(html, "<h3>" .. title .. "</h3>")
    end

    table.insert(html, '{| class="wikitable sortable"')

    -- Header
    table.insert(html, "|-")
    for _, var in ipairs(vars) do
        table.insert(html, "! " .. var)
    end

    -- Rows
    for _, row in ipairs(rows) do
        table.insert(html, "|-")
        for _, var in ipairs(vars) do
            local cell = ""
            if row[var] then
                cell = row[var].value
            end
            table.insert(html, "| " .. cell)
        end
    end

    table.insert(html, "|}")

    return table.concat(html, "\n")
end

return p