Module:ZoteroAPI
La documentation pour ce module peut être créée à Module:ZoteroAPI/doc
local p = {}
-- Fonction principale : récupère les données d'un item Zotero par sa clé
function p._fetchZoteroDataByKey(itemKey)
if not itemKey or itemKey == '' then
return nil
end
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
local success, data = pcall(mw.ext.externalData.getExternalData, {
url = url,
format = 'json'
})
if not success or not data then
return nil
end
local decoded
if type(data) == "string" then
local ok, result = pcall(mw.text.jsonDecode, data)
if not ok or type(result) ~= "table" then
return nil
end
decoded = result
elseif type(data) == "table" then
decoded = data
else
return nil
end
if decoded.__json and type(decoded.__json) == "table" then
return decoded.__json
end
return decoded
end
-- 🔍 Fonction de débogage simple (résumé)
function p.debugResult(frame)
local itemKey = frame.args[1]
local d = p._fetchZoteroDataByKey(itemKey)
if not d then return "Aucune donnée reçue" end
local data = d.data or {}
local out = {
"✔ Clé : " .. (data.key or ''),
"✔ Titre : " .. (data.caseName or ''),
"✔ Tribunal : " .. (data.court or ''),
"✔ Date : " .. (data.dateDecided or ''),
"✔ URL : " .. (data.url or ''),
"✔ Auteur : " .. ((data.creators and data.creators[1] and data.creators[1].firstName or '') .. " " .. (data.creators and data.creators[1] and data.creators[1].lastName or ''))
}
return table.concat(out, "\n")
end
-- 🔍 Fonction de débogage complet (JSON indenté)
function p.debugRawJson(frame)
local itemKey = frame.args[1]
local d = p._fetchZoteroDataByKey(itemKey)
if not d then return "Aucune donnée reçue" end
local function indentJson(json)
local indent = 0
local formatted = {}
local inString = false
for i = 1, #json do
local c = json:sub(i, i)
if c == '"' and json:sub(i - 1, i - 1) ~= '\\' then
inString = not inString
end
if not inString then
if c == '{' or c == '[' then
table.insert(formatted, c .. '\n' .. string.rep(' ', indent + 1))
indent = indent + 1
elseif c == '}' or c == ']' then
indent = indent - 1
table.insert(formatted, '\n' .. string.rep(' ', indent) .. c)
elseif c == ',' then
table.insert(formatted, ',\n' .. string.rep(' ', indent))
else
table.insert(formatted, c)
end
else
table.insert(formatted, c)
end
end
return table.concat(formatted)
end
local raw = mw.text.jsonEncode(d)
return '<pre>' .. indentJson(raw) .. '</pre>'
end
-- Fonctions d'accès simples à des champs (données principales)
function p.caseName(frame)
local d = p._fetchZoteroDataByKey(frame.args[1])
return d and d.data and d.data.caseName or ''
end
function p.dateDecided(frame)
local d = p._fetchZoteroDataByKey(frame.args[1])
return d and d.data and d.data.dateDecided or ''
end
function p.docketNumber(frame)
local d = p._fetchZoteroDataByKey(frame.args[1])
return d and d.data and d.data.docketNumber or ''
end
function p.history(frame)
local d = p._fetchZoteroDataByKey(frame.args[1])
return d and d.data and d.data.history or ''
end
function p.url(frame)
local d = p._fetchZoteroDataByKey(frame.args[1])
return d and d.data and d.data.url or ''
end
function p.court(frame)
local d = p._fetchZoteroDataByKey(frame.args[1])
return d and d.data and d.data.court or ''
end
function p.auteurPrenom(frame)
local d = p._fetchZoteroDataByKey(frame.args[1])
local c = d and d.data and d.data.creators and d.data.creators[1]
return c and c.firstName or ''
end
function p.auteurNom(frame)
local d = p._fetchZoteroDataByKey(frame.args[1])
local c = d and d.data and d.data.creators and d.data.creators[1]
return c and c.lastName or ''
end
-- Affichage URL pour vérification
function p.debugUrl(frame)
local itemKey = frame.args[1]
return 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
end
return p