« 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 20 : Ligne 20 :
     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 success, response = pcall(mw.ext.externalData.getExternalData, {
    -- Utiliser mw.uri.fetchContent au lieu de externalData
        url = url,
     local success, response = pcall(function()
        format = 'json'
        return mw.uri.fetchContent(url)
     })
     end)
      
      
     if not success or not response then
     if not success or not response then
Ligne 29 : Ligne 29 :
     end
     end
      
      
     -- Traitement des différents formats possibles de réponse
     -- Décoder la réponse JSON
    local data
     local success, data = pcall(mw.text.jsonDecode, response)
   
    if not success or not data then
    -- Si la réponse est une chaîne JSON, on la décode
     if type(response) == "string" then
        local ok, decoded = pcall(mw.text.jsonDecode, response)
        if not ok or type(decoded) ~= "table" then
            return nil
        end
        data = decoded
    elseif type(response) == "table" then
        -- Si c'est déjà une table, on l'utilise directement
        data = response
    else
         return nil
         return nil
     end
     end
      
      
     -- Vérifier la structure des données
     -- Extraire les données de la réponse
    -- Pour un itemKey spécifique, data peut être:
    -- 1. L'objet directement
    -- 2. Un objet avec une propriété "data" contenant les informations
   
     if data.data then
     if data.data then
        -- Si la structure est { data: {...} }
         cachedData = data.data
         cachedData = data.data
     else
     else
        -- Si la structure est l'objet directement
         cachedData = data
         cachedData = data
     end
     end
Ligne 61 : Ligne 44 :
     return cachedData
     return cachedData
end
end
-- Fonction pour déboguer la réponse brute
function p.debugRawResponse(frame)
function p.debugRawResponse(frame)
     local itemKey = frame and frame.args[1]
     local itemKey = frame and frame.args[1]
Ligne 70 : Ligne 55 :
     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 success, response = pcall(mw.ext.externalData.getExternalData, {
    -- Utiliser mw.uri.fetchContent
        url = url,
     local success, response = pcall(function()
        format = 'json'
        return mw.uri.fetchContent(url)
     })
     end)
      
      
     if not success then
     if not success then
Ligne 83 : Ligne 68 :
     end
     end
      
      
     -- Afficher le type et le contenu brut
     -- Limiter la taille de la réponse pour éviter de surcharger la page
     return "Type: " .. type(response) .. "\n\nContenu: " .. tostring(response)
    if #response > 1000 then
        response = response:sub(1, 1000) .. "... (tronqué)"
    end
   
     return "Type: " .. type(response) .. "\n\nContenu: " .. response
end
end
-- Fonctions de débogage
-- Fonctions de débogage
function p.debugResult(frame)
function p.debugResult(frame)

Version du 12 juin 2025 à 16:08

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'
    
    -- Utiliser mw.uri.fetchContent au lieu de externalData
    local success, response = pcall(function()
        return mw.uri.fetchContent(url)
    end)
    
    if not success or not response then
        return nil
    end
    
    -- Décoder la réponse JSON
    local success, data = pcall(mw.text.jsonDecode, response)
    if not success or not data then
        return nil
    end
    
    -- Extraire les données de la réponse
    if data.data then
        cachedData = data.data
    else
        cachedData = data
    end
    
    return cachedData
end

-- Fonction pour déboguer la réponse brute
function p.debugRawResponse(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'
    
    -- Utiliser mw.uri.fetchContent
    local success, response = pcall(function()
        return mw.uri.fetchContent(url)
    end)
    
    if not success then
        return "Erreur lors de la requête: " .. tostring(response)
    end
    
    if not response then
        return "Aucune réponse reçue"
    end
    
    -- Limiter la taille de la réponse pour éviter de surcharger la page
    if #response > 1000 then
        response = response:sub(1, 1000) .. "... (tronqué)"
    end
    
    return "Type: " .. type(response) .. "\n\nContenu: " .. response
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