« Module:ZoteroItem » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 2 : | Ligne 2 : | ||
function p.getItem(frame) | 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 | end | ||
return p | return p | ||