Module:ZoteroItem

De alcolois
Aller à la navigation Aller à la recherche

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

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)
    local key = frame.args[1]
    if not key or key == "" then
        return "Erreur : aucun itemKey fourni"
    end
    local groupId = frame.args.groupId or "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"
    }

    if errs then
        return "Erreur HTTP ou parsing : " .. table.concat(errs, "; ")
    elseif not t then
        return "Résultat vide, possible timeout ou erreur silencieuse"
    elseif type(t) == "string" then
        return "Type inattendu (string) retourné : contenu = " ..
               mw.text.escape(t)
    elseif not t.__json then
        return "Structure inattendue, sans __json : " .. mw.text.jsonEncode(t)
    end

    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
    return table.concat(out)
end

return p