« 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 = {} | ||
local cachedData = nil -- 🔒 Cache unique pour la durée d’exécution de la page | |||
-- Fonction | -- Fonction utilitaire : extrait le titre de recherche | ||
function p. | local function getQueryTitle() | ||
if | local fullTitle = mw.title.getCurrentTitle().text | ||
return | local base = fullTitle:match("^(.-),%s?%d") or fullTitle | ||
base = mw.ustring.gsub(base, "’", "'") | |||
local encoded = mw.uri.encode(base, nil):gsub("+", "%%20") | |||
return encoded | |||
end | |||
-- Fonction de récupération de données Zotero | |||
function p._fetchZoteroData() | |||
if cachedData then | |||
return cachedData | |||
end | end | ||
local url = 'https://api.zotero.org/groups/4893620/items | local query = getQueryTitle() | ||
local url = 'https://api.zotero.org/groups/4893620/items?q=' .. query .. '&qmode=titleCreatorYear&include=data&format=json' | |||
local success, data = pcall(mw.ext.externalData.getExternalData, { | local success, data = pcall(mw.ext.externalData.getExternalData, { | ||
| Ligne 18 : | Ligne 29 : | ||
end | end | ||
-- Correction ici : on s'assure que `data` est bien une table | |||
local decoded | local decoded | ||
if type(data) == "string" then | if type(data) == "string" then | ||
| Ligne 31 : | Ligne 43 : | ||
end | end | ||
if | if type(decoded[1]) == "table" then | ||
return | cachedData = decoded[1] | ||
return cachedData | |||
end | end | ||
return | return nil | ||
end | end | ||
-- | -- Fonctions de débogage | ||
function p.debugResult( | function p.debugResult() | ||
local d = p._fetchZoteroData() | |||
local d = p. | |||
if not d then return "Aucune donnée reçue" end | if not d then return "Aucune donnée reçue" end | ||
local out = {} | |||
local | 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") | return table.concat(out, "\n") | ||
end | end | ||
function p.debugRawJson() | |||
function p.debugRawJson( | local d = p._fetchZoteroData() | ||
if not d then | |||
local d = p. | return "Aucune donnée reçue" | ||
if not d then return "Aucune donnée reçue" end | end | ||
local function indentJson(json) | local function indentJson(json) | ||
| Ligne 71 : | Ligne 78 : | ||
for i = 1, #json do | for i = 1, #json do | ||
local c = json:sub(i, i) | local c = json:sub(i, i) | ||
if c == '"' and json:sub(i - 1, i - 1) ~= '\\' then | if c == '"' and json:sub(i - 1, i - 1) ~= '\\' then | ||
inString = not inString | inString = not inString | ||
end | end | ||
if not inString then | if not inString then | ||
if c == '{' or c == '[' then | if c == '{' or c == '[' then | ||
| Ligne 95 : | Ligne 104 : | ||
local raw = mw.text.jsonEncode(d) | local raw = mw.text.jsonEncode(d) | ||
return '<pre>' .. | local pretty = indentJson(raw) | ||
return '<pre>' .. pretty .. '</pre>' | |||
end | end | ||
-- Fonctions | -- Fonctions accessibles | ||
function p.caseName( | function p.caseName() | ||
local d = p. | local d = p._fetchZoteroData() | ||
return d and d | return d and d.caseName or '' | ||
end | end | ||
function p.dateDecided( | function p.dateDecided() | ||
local d = p. | local d = p._fetchZoteroData() | ||
return d and d | return d and d.dateDecided or '' | ||
end | end | ||
function p.docketNumber( | function p.docketNumber() | ||
local d = p. | local d = p._fetchZoteroData() | ||
return d and d | return d and d.docketNumber or '' | ||
end | end | ||
function p.history( | function p.history() | ||
local d = p. | local d = p._fetchZoteroData() | ||
return d and d | return d and d.history or '' | ||
end | end | ||
function p.url( | function p.url() | ||
local d = p. | local d = p._fetchZoteroData() | ||
return d and d | return d and d.url or '' | ||
end | end | ||
function p.court( | function p.court() | ||
local d = p. | local d = p._fetchZoteroData() | ||
return d and d | return d and d.court or '' | ||
end | end | ||
function p.auteurPrenom( | function p.auteurPrenom() | ||
local d = p. | local d = p._fetchZoteroData() | ||
return d and d.firstName or '' | |||
end | end | ||
function p.auteurNom( | function p.auteurNom() | ||
local d = p. | local d = p._fetchZoteroData() | ||
return d and d.lastName or '' | |||
end | end | ||
-- Affichage URL pour vérification | -- Affichage de l'URL utilisée (pour vérification) | ||
function p.debugUrl( | function p.debugUrl() | ||
local | local query = getQueryTitle() | ||
return 'https://api.zotero.org/groups/4893620/items | return 'https://api.zotero.org/groups/4893620/items?q=' .. query .. '&qmode=titleCreatorYear&include=data&format=json' | ||
end | end | ||
return p | return p | ||