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

Aucun résumé des modifications
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


-- Fonction utilitaire pour récupérer et décoder les données Zotero
-- Fonction principale : récupère les données d'un item Zotero par sa clé
function p._fetchZoteroData(itemKey)
function p._fetchZoteroDataByKey(itemKey)
if not itemKey or itemKey == '' then
if not itemKey or itemKey == '' then
return nil
return nil
Ligne 9 : Ligne 9 :
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'


local data = mw.ext.externalData.getExternalData({
local success, data = pcall(mw.ext.externalData.getExternalData, {
url = url,
url = url,
format = 'json'
format = 'json'
})
})
if not success or not data then
return nil
end


local decoded
local decoded
Ligne 34 : Ligne 38 :
end
end


-- Fonction pour afficher le JSON formaté
-- 🔍 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)
function p.debugRawJson(frame)
local itemKey = frame.args[1]
local itemKey = frame.args[1]
if not itemKey or itemKey == '' then
local d = p._fetchZoteroDataByKey(itemKey)
return "❌ Aucun itemKey fourni."
if not d then return "Aucune donnée reçue" end
end
 
local d = p._fetchZoteroData(itemKey)
if not d then
return "Aucune donnée reçue ou erreur de décodage."
end


-- Fonction d’indentation
local function indentJson(json)
local function indentJson(json)
local indent = 0
local indent = 0
Ligne 79 : Ligne 96 :
local raw = mw.text.jsonEncode(d)
local raw = mw.text.jsonEncode(d)
return '<pre>' .. indentJson(raw) .. '</pre>'
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
end


return p
return p