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

De alcolois
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 utilitaire : extrait le titre de recherche
-- Fonction de récupération de données Zotero par itemKey
local function getQueryTitle()
function p._fetchZoteroData(frame)
local fullTitle = mw.title.getCurrentTitle().text
    -- Utiliser le cache si disponible
local base = fullTitle:match("^(.-),%s?%d") or fullTitle
    if cachedData then
base = mw.ustring.gsub(base, "’", "'")
        return cachedData
local encoded = mw.uri.encode(base, nil):gsub("+", "%%20")
    end
return encoded
   
end
    -- Obtenir l'itemKey depuis le paramètre
 
    local itemKey = frame and frame.args[1]
-- Fonction de récupération de données Zotero
   
function p._fetchZoteroData()
    -- Si aucun itemKey n'est fourni, retourner nil
if cachedData then
    if not itemKey or itemKey == "" then
return cachedData
        return nil
end
    end
 
   
local query = getQueryTitle()
    -- Construire l'URL avec l'itemKey spécifique
local url = 'https://api.zotero.org/groups/4893620/items?q=' .. query .. '&qmode=titleCreatorYear&include=data&format=json'
    local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
 
   
local success, data = pcall(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
    if not success or not data then
return nil
        return nil
end
    end
 
   
-- Correction ici : on s'assure que `data` est bien une table
    -- Correction: on s'assure que `data` est bien une table
local decoded
    local decoded
if type(data) == "string" then
    if type(data) == "string" then
local ok, result = pcall(mw.text.jsonDecode, data)
        local ok, result = pcall(mw.text.jsonDecode, data)
if not ok or type(result) ~= "table" then
        if not ok or type(result) ~= "table" then
return nil
            return nil
end
        end
decoded = result
        decoded = result
elseif type(data) == "table" then
    elseif type(data) == "table" then
decoded = data
        decoded = data
else
    else
return nil
        return nil
end
    end
 
   
if type(decoded[1]) == "table" then
    -- Pour l'itemKey, la réponse de l'API est directement l'objet (pas un tableau)
cachedData = decoded[1]
    cachedData = decoded.data
return cachedData
    return cachedData
end
 
return nil
end
end


-- Fonctions de débogage
-- Fonctions de débogage
function p.debugResult()
function p.debugResult(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
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 out = {}
table.insert(out, "✔ Clé : " .. (d.key or ''))
    table.insert(out, "✔ Clé : " .. (d.key or ''))
table.insert(out, "✔ Titre : " .. (d.caseName or ''))
    table.insert(out, "✔ Titre : " .. (d.caseName or ''))
table.insert(out, "✔ Tribunal : " .. (d.court or ''))
    table.insert(out, "✔ Tribunal : " .. (d.court or ''))
table.insert(out, "✔ Date : " .. (d.dateDecided or ''))
    table.insert(out, "✔ Date : " .. (d.dateDecided or ''))
table.insert(out, "✔ URL : " .. (d.url or ''))
    table.insert(out, "✔ URL : " .. (d.url or ''))
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


function p.debugRawJson()
function p.debugRawJson(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
if not d then
    if not d then
return "Aucune donnée reçue"
        return "Aucune donnée reçue"
end
    end
 
    local function indentJson(json)
local function indentJson(json)
        local indent = 0
local indent = 0
        local formatted = {}
local formatted = {}
        local inString = false
local inString = false
        for i = 1, #json do
 
            local c = json:sub(i, i)
for i = 1, #json do
            if c == '"' and json:sub(i - 1, i - 1) ~= '\\' then
local c = json:sub(i, i)
                inString = not inString
 
            end
if c == '"' and json:sub(i - 1, i - 1) ~= '\\' then
            if not inString then
inString = not inString
                if c == '{' or c == '[' then
end
                    table.insert(formatted, c .. '\n' .. string.rep('  ', indent + 1))
 
                    indent = indent + 1
if not inString then
                elseif c == '}' or c == ']' then
if c == '{' or c == '[' then
                    indent = indent - 1
table.insert(formatted, c .. '\n' .. string.rep('  ', indent + 1))
                    table.insert(formatted, '\n' .. string.rep('  ', indent) .. c)
indent = indent + 1
                elseif c == ',' then
elseif c == '}' or c == ']' then
                    table.insert(formatted, ',\n' .. string.rep('  ', indent))
indent = indent - 1
                else
table.insert(formatted, '\n' .. string.rep('  ', indent) .. c)
                    table.insert(formatted, c)
elseif c == ',' then
                end
table.insert(formatted, ',\n' .. string.rep('  ', indent))
            else
else
                table.insert(formatted, c)
table.insert(formatted, c)
            end
end
        end
else
        return table.concat(formatted)
table.insert(formatted, c)
    end
end
    local raw = mw.text.jsonEncode(d)
end
    local pretty = indentJson(raw)
 
    return '<pre>' .. pretty .. '</pre>'
return table.concat(formatted)
end
 
local raw = mw.text.jsonEncode(d)
local pretty = indentJson(raw)
return '<pre>' .. pretty .. '</pre>'
end
end


-- Fonctions accessibles
-- Fonctions accessibles
function p.caseName()
function p.caseName(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
return d and d.caseName or ''
    return d and d.caseName or ''
end
end


function p.dateDecided()
function p.dateDecided(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
return d and d.dateDecided or ''
    return d and d.dateDecided or ''
end
end


function p.docketNumber()
function p.docketNumber(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
return d and d.docketNumber or ''
    return d and d.docketNumber or ''
end
end


function p.history()
function p.history(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
return d and d.history or ''
    return d and d.history or ''
end
end


function p.url()
function p.url(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
return d and d.url or ''
    return d and d.url or ''
end
end


function p.court()
function p.court(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
return d and d.court or ''
    return d and d.court or ''
end
end


function p.auteurPrenom()
function p.auteurPrenom(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
return d and d.firstName or ''
    return d and d.firstName or ''
end
end


function p.auteurNom()
function p.auteurNom(frame)
local d = p._fetchZoteroData()
    local d = p._fetchZoteroData(frame)
return d and d.lastName or ''
    return d and d.lastName or ''
end
end


-- Affichage de l'URL utilisée (pour vérification)
-- Affichage de l'URL utilisée (pour vérification)
function p.debugUrl()
function p.debugUrl(frame)
local query = getQueryTitle()
    local itemKey = frame and frame.args[1] or ""
return 'https://api.zotero.org/groups/4893620/items?q=' .. query .. '&qmode=titleCreatorYear&include=data&format=json'
    return 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
end
end


return p
return p

Version du 12 juin 2025 à 15:49

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 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'
    
    local success, data = pcall(mw.ext.externalData.getExternalData, {
        url = url,
        format = 'json'
    })
    
    if not success or not data then
        return nil
    end
    
    -- Correction: on s'assure que `data` est bien une table
    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
    
    -- Pour l'itemKey, la réponse de l'API est directement l'objet (pas un tableau)
    cachedData = decoded.data
    return cachedData
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