« 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 1 : Ligne 1 :
local p = {}
local p = {}
local t, errs = mw.ext.externalData.getWebData{ url = url, format = "JSON" }


if errs then
  return "DEBUG: errs = " .. table.concat(errs, "; ")
elseif t == nil then
  return "DEBUG: t is nil"
elseif type(t) == "string" then
  return "DEBUG: t is string! content = " .. mw.text.escape(t)
else
  return "DEBUG: t is table type=" .. type(t) .. ", fields = " .. mw.text.jsonEncode(t)
end
function p.getItem(frame)
function p.getItem(frame)
     local key = frame.args[1]
     local key = frame.args[1]
Ligne 16 : Ligne 6 :
         return "Erreur : aucun itemKey fourni"
         return "Erreur : aucun itemKey fourni"
     end
     end
     local groupId = frame.args.groupId or "4893620"
 
     local groupId = "4893620"
     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",
Ligne 28 : Ligne 19 :
     }
     }


    -- 🔍 Diagnostic détaillé
     if errs then
     if errs then
         return "Erreur HTTP ou parsing : " .. table.concat(errs, "; ")
         return "DEBUG: erreurs rencontrées : " .. table.concat(errs, "; ")
     elseif not t then
     elseif t == nil then
         return "Résultat vide, possible timeout ou erreur silencieuse"
         return "DEBUG: t est nil — la requête n’a rien retourné"
     elseif type(t) == "string" then
     elseif type(t) == "string" then
         return "Type inattendu (string) retourné : contenu = " ..
         return "DEBUG: t est une chaîne ! Contenu brut = " .. mw.text.escape(t)
              mw.text.escape(t)
     elseif not t.__json then
     elseif not t.__json then
         return "Structure inattendue, sans __json : " .. mw.text.jsonEncode(t)
         return "DEBUG: t est une table, mais sans champ '__json'. Contenu brut = " .. mw.text.jsonEncode(t)
    end
     else
 
         return "DEBUG: succès ! Contenu de __json = " .. mw.text.jsonEncode(t.__json)
    local d = t.__json.data
    if type(d) ~= "table" then
        return "Réponse reçue sans champ 'data'"
    end
 
    -- Extraction des champs utiles
    local title = mw.text.escape(d.title or d.caseName or "(sans titre)")
    local authors = {}
    if d.creators then
        for _, c in ipairs(d.creators) do
            table.insert(authors, mw.text.escape(
                (c.firstName or "") .. " " .. (c.lastName or "")
            ))
        end
    end
    local dateDecided = mw.text.escape(d.dateDecided or "")
     local abstractNote = mw.text.escape(d.abstractNote or "")
    local urlField = mw.text.escape(d.url or "")
 
    local out = {}
    table.insert(out, "<strong>Titre :</strong> " .. title)
    if #authors > 0 then
         table.insert(out, "<br><strong>Auteurs :</strong> " .. table.concat(authors, ", "))
    end
    if dateDecided ~= "" then
        table.insert(out, "<br><strong>Date décidée :</strong> " .. dateDecided)
    end
    if abstractNote ~= "" then
        table.insert(out, "<br><strong>Résumé :</strong> " .. abstractNote)
    end
    if urlField ~= "" then
        table.insert(out, '<br><strong>Lien :</strong> <a href="' .. urlField ..
            '" target="_blank">Voir la source</a>')
     end
     end
    return table.concat(out)
end
end


return p
return p

Version du 12 juin 2025 à 17:24

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 : aucun itemKey fourni"
    end

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

    local t, errs = mw.ext.externalData.getWebData{
        url = url,
        format = "JSON"
    }

    -- 🔍 Diagnostic détaillé
    if errs then
        return "DEBUG: erreurs rencontrées : " .. table.concat(errs, "; ")
    elseif t == nil then
        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

return p