« Module:ZoteroAPI » : différence entre les versions
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 cachedData = nil -- 🔒 Cache unique pour la durée d'exécution de la page | local cachedData = nil -- 🔒 Cache unique pour la durée d'exécution de la page | ||
-- Fonction d'aide pour appeler #get_web_data via preprocess | |||
function p._getWebDataViaParser(frame, url) | |||
local webDataCall = '{{#get_web_data:url=' .. url .. '|format=json}}' | |||
local result = frame:preprocess(webDataCall) | |||
return result | |||
end | |||
-- Fonction de récupération de données Zotero par itemKey | -- Fonction de récupération de données Zotero par itemKey | ||
| Ligne 20 : | Ligne 27 : | ||
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' | ||
-- Récupérer les données via le parser | |||
local jsonData = p._getWebDataViaParser(frame, url) | |||
-- Récupérer les données | |||
local | |||
-- Si pas de données, retourner nil | |||
if not jsonData or jsonData == "" then | |||
-- | |||
if not jsonData | |||
return nil | return nil | ||
end | end | ||
-- Décoder le JSON | -- Décoder le JSON | ||
local | local success, decoded = pcall(mw.text.jsonDecode, jsonData) | ||
if not success or not decoded then | if not success or not decoded then | ||
return nil | return nil | ||
end | end | ||
-- | -- Extraire les données de la réponse | ||
if decoded.data then | if decoded.data then | ||
cachedData = decoded.data | cachedData = decoded.data | ||
| Ligne 86 : | Ligne 52 : | ||
end | end | ||
-- Fonction de débogage de | -- Fonction de débogage pour tester la récupération de données | ||
function p. | function p.debugGetData(frame) | ||
local itemKey = frame and frame.args[1] | local itemKey = frame and frame.args[1] | ||
| Ligne 96 : | Ligne 62 : | ||
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' | ||
-- Tester | -- Tester la récupération directe via le parser | ||
local | local jsonData = p._getWebDataViaParser(frame, url) | ||
if not jsonData or jsonData == "" then | |||
return "Pas de données récupérées via #get_web_data" | |||
if | |||
end | end | ||
-- | -- Limiter la taille pour l'affichage | ||
if #jsonData > 1000 then | |||
jsonData = jsonData:sub(1, 1000) .. "... (tronqué)" | |||
end | end | ||
return | return "Données récupérées (longueur: " .. #jsonData .. "):\n\n" .. jsonData | ||
end | end | ||
Version du 12 juin 2025 à 16:19
La documentation pour ce module peut être créée à Module:ZoteroAPI/doc
local p = {}
local cachedData = nil -- 🔒 Cache unique pour la durée d'exécution de la page
-- Fonction d'aide pour appeler #get_web_data via preprocess
function p._getWebDataViaParser(frame, url)
local webDataCall = '{{#get_web_data:url=' .. url .. '|format=json}}'
local result = frame:preprocess(webDataCall)
return result
end
-- Fonction de récupération de données Zotero par itemKey
function p._fetchZoteroData(frame)
-- Utiliser le cache si disponible
if cachedData then
return cachedData
end
-- Obtenir l'itemKey depuis le paramètre
local itemKey = frame and frame.args[1]
-- Si aucun itemKey n'est fourni, retourner nil
if not itemKey or itemKey == "" then
return nil
end
-- Construire l'URL avec l'itemKey spécifique
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
-- Récupérer les données via le parser
local jsonData = p._getWebDataViaParser(frame, url)
-- Si pas de données, retourner nil
if not jsonData or jsonData == "" then
return nil
end
-- Décoder le JSON
local success, decoded = pcall(mw.text.jsonDecode, jsonData)
if not success or not decoded then
return nil
end
-- Extraire les données de la réponse
if decoded.data then
cachedData = decoded.data
else
cachedData = decoded
end
return cachedData
end
-- Fonction de débogage pour tester la récupération de données
function p.debugGetData(frame)
local itemKey = frame and frame.args[1]
if not itemKey or itemKey == "" then
return "Aucun itemKey fourni"
end
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
-- Tester la récupération directe via le parser
local jsonData = p._getWebDataViaParser(frame, url)
if not jsonData or jsonData == "" then
return "Pas de données récupérées via #get_web_data"
end
-- Limiter la taille pour l'affichage
if #jsonData > 1000 then
jsonData = jsonData:sub(1, 1000) .. "... (tronqué)"
end
return "Données récupérées (longueur: " .. #jsonData .. "):\n\n" .. jsonData
end
-- Fonctions de débogage
function p.debugResult(frame)
local d = p._fetchZoteroData(frame)
if not d then return "Aucune donnée reçue" end
local out = {}
table.insert(out, "✔ Clé : " .. (d.key or ''))
table.insert(out, "✔ Titre : " .. (d.caseName or ''))
table.insert(out, "✔ Tribunal : " .. (d.court or ''))
table.insert(out, "✔ Date : " .. (d.dateDecided or ''))
table.insert(out, "✔ URL : " .. (d.url or ''))
table.insert(out, "✔ Auteur : " .. (d.firstName or '') .. " " .. (d.lastName or ''))
return table.concat(out, "\n")
end
function p.debugRawJson(frame)
local d = p._fetchZoteroData(frame)
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)
local pretty = indentJson(raw)
return '<pre>' .. pretty .. '</pre>'
end
-- Fonctions accessibles
function p.caseName(frame)
local d = p._fetchZoteroData(frame)
return d and d.caseName or ''
end
function p.dateDecided(frame)
local d = p._fetchZoteroData(frame)
return d and d.dateDecided or ''
end
function p.docketNumber(frame)
local d = p._fetchZoteroData(frame)
return d and d.docketNumber or ''
end
function p.history(frame)
local d = p._fetchZoteroData(frame)
return d and d.history or ''
end
function p.url(frame)
local d = p._fetchZoteroData(frame)
return d and d.url or ''
end
function p.court(frame)
local d = p._fetchZoteroData(frame)
return d and d.court or ''
end
function p.auteurPrenom(frame)
local d = p._fetchZoteroData(frame)
return d and d.firstName or ''
end
function p.auteurNom(frame)
local d = p._fetchZoteroData(frame)
return d and d.lastName or ''
end
-- Affichage de l'URL utilisée (pour vérification)
function p.debugUrl(frame)
local itemKey = frame and frame.args[1] or ""
return 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
end
return p