« Module:WikidataTable » : différence entre les versions

De Wikiromandie.org
Aller à la navigation Aller à la recherche
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... »
 
Aucun résumé des modifications
 
(6 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local p = {}
local p = {}
local http = mw.http
local json = mw.text.jsonDecode


function p.render(frame)
function p.render(frame)
     local sparql = frame.args.sparql
     -- Fetch arguments from frame or parent template frame
     local columns = frame.args.columns or "itemLabel"
     local getArgs = function(key)
    local title = frame.args.title
        return (frame.args and frame.args[key])
 
            or (frame:getParent() and frame:getParent().args and frame:getParent().args[key])
    if not sparql or sparql == "" then
            or ""
        return "⚠️ No SPARQL query provided."
     end
     end


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


    local response = http.get(url)
     if sparql == "" then
 
         return "⚠️ No SPARQL query provided."
     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
     end


     table.insert(html, '{| class="wikitable sortable"')
     -- Clean query string
 
    sparql = sparql:gsub('<nowiki>', ''):gsub('</nowiki>', '')
    -- Header
                  :gsub('\194\160', ' ') -- Remove non-breaking spaces
    table.insert(html, "|-")
                  :gsub('%s+', ' ')    -- Collapse spaces and newlines
    for _, var in ipairs(vars) do
        table.insert(html, "! " .. var)
     end


     -- Rows
     local function htmlEncode(s)
    for _, row in ipairs(rows) do
         return s:gsub('&', '&amp;')
         table.insert(html, "|-")
                :gsub('"', '&quot;')
        for _, var in ipairs(vars) do
                :gsub('<', '&lt;')
            local cell = ""
                 :gsub('>', '&gt;')
            if row[var] then
                 cell = row[var].value
            end
            table.insert(html, "| " .. cell)
        end
     end
     end


     table.insert(html, "|}")
     return string.format(
 
        '<div class="wikidata-sparql-table" data-sparql="%s" data-title="%s"></div>',
     return table.concat(html, "\n")
        htmlEncode(sparql),
        htmlEncode(title)
     )
end
end


return p
return p

Dernière version du 21 septembre 2026 à 12:41

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

local p = {}

function p.render(frame)
    -- Fetch arguments from frame or parent template frame
    local getArgs = function(key)
        return (frame.args and frame.args[key]) 
            or (frame:getParent() and frame:getParent().args and frame:getParent().args[key]) 
            or ""
    end

    local sparql = getArgs("sparql")
    local title  = getArgs("title")

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

    -- Clean query string
    sparql = sparql:gsub('<nowiki>', ''):gsub('</nowiki>', '')
                   :gsub('\194\160', ' ') -- Remove non-breaking spaces
                   :gsub('%s+', ' ')     -- Collapse spaces and newlines

    local function htmlEncode(s)
        return s:gsub('&', '&amp;')
                :gsub('"', '&quot;')
                :gsub('<', '&lt;')
                :gsub('>', '&gt;')
    end

    return string.format(
        '<div class="wikidata-sparql-table" data-sparql="%s" data-title="%s"></div>',
        htmlEncode(sparql),
        htmlEncode(title)
    )
end

return p