« Module:ZoteroAPI » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| (5 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 17 : | Ligne 17 : | ||
end | end | ||
-- Construire l'URL avec l'itemKey | -- Construire l'URL avec l'itemKey directement dans le chemin | ||
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey | local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey | ||
local success, | -- Récupérer les données avec getExternalData en utilisant JSONPath | ||
local success, result = pcall(function() | |||
return mw.ext.externalData.getExternalData({ | |||
url = url, | |||
format = 'json', | |||
use_jsonpath = true, | |||
data = { | |||
key = '$.key', | |||
caseName = '$.data.caseName', | |||
dateDecided = '$.data.dateDecided', | |||
court = '$.data.court', | |||
url = '$.data.url', | |||
history = '$.data.history', | |||
docketNumber = '$.data.docketNumber', | |||
firstName = '$.data.creators[0].firstName', | |||
lastName = '$.data.creators[0].lastName' | |||
} | |||
}) | |||
end) | |||
if not success or not | if not success or not result then | ||
return nil | return nil | ||
end | end | ||
-- | -- Créer un objet avec les données extraites | ||
local data | local data = {} | ||
-- | -- ExternalData retourne un tableau de valeurs pour chaque champ | ||
-- Nous prenons le premier élément de chaque tableau | |||
for field, values in pairs(result) do | |||
if | if type(values) == "table" and values[1] then | ||
data[field] = values[1] | |||
end | end | ||
end | end | ||
cachedData = data | |||
return cachedData | return cachedData | ||
end | end | ||
function p. | |||
-- Fonction de débogage simple | |||
function p.debugSimple(frame) | |||
local itemKey = frame and frame.args[1] | local itemKey = frame and frame.args[1] | ||
| Ligne 68 : | Ligne 67 : | ||
end | end | ||
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey | local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey | ||
-- Essayons une approche simplifiée | |||
local output = {"Test de récupération des données:"} | |||
local success, | -- Test 1: Récupérer juste le titre sans JSONPath | ||
local success, result1 = pcall(function() | |||
return mw.ext.externalData.getExternalData({ | |||
url = url, | |||
format = 'json' | |||
}) | |||
end) | |||
table.insert(output, "\n1. Test sans JSONPath:") | |||
if not success then | if not success then | ||
table.insert(output, " Erreur: " .. tostring(result1)) | |||
elseif not result1 then | |||
table.insert(output, " Aucun résultat") | |||
else | |||
table.insert(output, " Type de résultat: " .. type(result1)) | |||
-- Afficher quelques détails si c'est une table | |||
if type(result1) == "table" then | |||
for k, v in pairs(result1) do | |||
if type(v) ~= "table" then | |||
table.insert(output, " " .. k .. ": " .. tostring(v)) | |||
else | |||
table.insert(output, " " .. k .. ": [table]") | |||
end | |||
end | |||
else | |||
table.insert(output, " Valeur: " .. tostring(result1)) | |||
end | |||
end | end | ||
if not | -- Test 2: Récupérer juste le titre avec JSONPath simple | ||
local success, result2 = pcall(function() | |||
return mw.ext.externalData.getExternalData({ | |||
url = url, | |||
format = 'json', | |||
use_jsonpath = true, | |||
data = { | |||
title = '$.data.caseName' | |||
} | |||
}) | |||
end) | |||
table.insert(output, "\n2. Test avec JSONPath simple:") | |||
if not success then | |||
table.insert(output, " Erreur: " .. tostring(result2)) | |||
elseif not result2 then | |||
table.insert(output, " Aucun résultat") | |||
else | |||
table.insert(output, " Type de résultat: " .. type(result2)) | |||
-- Afficher quelques détails si c'est une table | |||
if type(result2) == "table" then | |||
for k, v in pairs(result2) do | |||
if type(v) ~= "table" then | |||
table.insert(output, " " .. k .. ": " .. tostring(v)) | |||
else | |||
table.insert(output, " " .. k .. ": [table]") | |||
if k == "title" and type(v) == "table" then | |||
for i, val in ipairs(v) do | |||
table.insert(output, " " .. i .. ": " .. tostring(val)) | |||
end | |||
end | |||
end | |||
end | |||
else | |||
table.insert(output, " Valeur: " .. tostring(result2)) | |||
end | |||
end | end | ||
-- | -- Test 3: Essayer avec #get_web_data via preprocess | ||
local webDataCall = '{{#get_web_data:url=' .. url .. '|format=json|use jsonpath=true|data=title=$.data.caseName}}' | |||
local result3 = frame:preprocess(webDataCall) | |||
table.insert(output, "\n3. Test avec #get_web_data:") | |||
table.insert(output, " Résultat: " .. result3) | |||
return table.concat(output, "\n") | |||
end | end | ||
-- Fonctions de débogage | -- Fonctions de débogage | ||
function p.debugResult(frame) | function p.debugResult(frame) | ||
| Ligne 98 : | Ligne 161 : | ||
table.insert(out, "✔ Auteur : " .. (d.firstName or '') .. " " .. (d.lastName or '')) | table.insert(out, "✔ Auteur : " .. (d.firstName or '') .. " " .. (d.lastName or '')) | ||
return table.concat(out, "\n") | return table.concat(out, "\n") | ||
end | end | ||
| Ligne 181 : | Ligne 207 : | ||
function p.debugUrl(frame) | function p.debugUrl(frame) | ||
local itemKey = frame and frame.args[1] or "" | local itemKey = frame and frame.args[1] or "" | ||
return 'https://api.zotero.org/groups/4893620/items/' .. itemKey | return 'https://api.zotero.org/groups/4893620/items/' .. itemKey | ||
end | end | ||
return p | return p | ||