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

De alcolois
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 4 : Ligne 4 :
     local key = frame.args[1]
     local key = frame.args[1]
     if not key or key == "" then
     if not key or key == "" then
         return "Erreur : aucun itemKey fourni"
         return "Erreur : itemKey requis"
     end
     end


Ligne 10 : Ligne 10 :
     local url = string.format(
     local url = string.format(
         "https://api.zotero.org/groups/%s/items/%s?format=json&include=data",
         "https://api.zotero.org/groups/%s/items/%s?format=json&include=data",
         mw.uri.encode(groupId),
         groupId,
         mw.uri.encode(key)
         key
     )
     )


     local t, errs = mw.ext.externalData.getWebData{
     local data, errors = mw.ext.externalData.getExternalData{
         url = url,
         url = url,
         format = "JSON"
         format = "JSON",
        data = { json = "__json" }
     }
     }


    -- 🔍 Diagnostic détaillé
     if errors then
     if errs then
         return "Erreur ExternalData : " .. table.concat(errors, "; ")
         return "DEBUG: erreurs rencontrées : " .. table.concat(errs, "; ")
     elseif not data or not data.json then
     elseif t == nil then
         return "Erreur : aucune donnée retournée"
        return "DEBUG: t est nil — la requête n’a rien retourné"
    elseif type(t) == "string" then
        return "DEBUG: t est une chaîne ! Contenu brut = " .. mw.text.escape(t)
    elseif not t.__json then
         return "DEBUG: t est une table, mais sans champ '__json'. Contenu brut = " .. mw.text.jsonEncode(t)
    else
        return "DEBUG: succès ! Contenu de __json = " .. mw.text.jsonEncode(t.__json)
     end
     end
    local d = data.json.data
    if not d then
        return "Erreur : champ 'data' manquant dans le JSON"
    end
    local out = {}
    table.insert(out, "<strong>Titre :</strong> " .. mw.text.escape(d.title or d.caseName or "(sans titre)"))
    if d.creators then
        local authors = {}
        for _, c in ipairs(d.creators) do
            table.insert(authors, mw.text.escape((c.firstName or "") .. " " .. (c.lastName or "")))
        end
        table.insert(out, "<br><strong>Auteurs :</strong> " .. table.concat(authors, ", "))
    end
    if d.dateDecided then
        table.insert(out, "<br><strong>Date :</strong> " .. mw.text.escape(d.dateDecided))
    end
    if d.abstractNote then
        table.insert(out, "<br><strong>Résumé :</strong> " .. mw.text.escape(d.abstractNote))
    end
    if d.url then
        table.insert(out, '<br><strong>Lien :</strong> <a href="' .. mw.text.escape(d.url) .. '" target="_blank">Voir</a>')
    end
    return table.concat(out)
end
end


return p
return p

Version du 12 juin 2025 à 17:30

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

local p = {}

function p.getItem(frame)
    local key = frame.args[1]
    if not key or key == "" then
        return "Erreur : itemKey requis"
    end

    local groupId = "4893620"
    local url = string.format(
        "https://api.zotero.org/groups/%s/items/%s?format=json&include=data",
        groupId,
        key
    )

    local data, errors = mw.ext.externalData.getExternalData{
        url = url,
        format = "JSON",
        data = { json = "__json" }
    }

    if errors then
        return "Erreur ExternalData : " .. table.concat(errors, "; ")
    elseif not data or not data.json then
        return "Erreur : aucune donnée retournée"
    end

    local d = data.json.data
    if not d then
        return "Erreur : champ 'data' manquant dans le JSON"
    end

    local out = {}
    table.insert(out, "<strong>Titre :</strong> " .. mw.text.escape(d.title or d.caseName or "(sans titre)"))

    if d.creators then
        local authors = {}
        for _, c in ipairs(d.creators) do
            table.insert(authors, mw.text.escape((c.firstName or "") .. " " .. (c.lastName or "")))
        end
        table.insert(out, "<br><strong>Auteurs :</strong> " .. table.concat(authors, ", "))
    end

    if d.dateDecided then
        table.insert(out, "<br><strong>Date :</strong> " .. mw.text.escape(d.dateDecided))
    end

    if d.abstractNote then
        table.insert(out, "<br><strong>Résumé :</strong> " .. mw.text.escape(d.abstractNote))
    end

    if d.url then
        table.insert(out, '<br><strong>Lien :</strong> <a href="' .. mw.text.escape(d.url) .. '" target="_blank">Voir</a>')
    end

    return table.concat(out)
end

return p