Module:ZoteroAPI

De alcolois
Aller à la navigation Aller à la recherche

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=' .. itemKey .. '&include=data'
    
    -- 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',
                abstractNote = '$[*].data.abstractNote',
                firstName = '$[*].data.creators[0].firstName',
                lastName = '$[*].data.creators[0].lastName'
            }
        })
    end)
    
    if not success or not result then
        return nil
    end
    
    -- Créer un objet avec les données extraites
    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 type(values) == "table" and values[1] then
            data[field] = values[1]
        end
    end
    
    cachedData = data
    return cachedData
end

-- Fonction de débogage simple
function p.debugSimple(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=' .. itemKey .. '&include=data'
    
    -- Essayons une version simplifiée avec un seul champ
    local success, result = pcall(function()
        return mw.ext.externalData.getExternalData({
            url = url,
            format = 'json',
            use_jsonpath = true,
            data = {
                title = '$[*].data.caseName'
            }
        })
    end)
    
    if not success then
        return "Erreur: " .. tostring(result)
    end
    
    if not result then
        return "Aucun résultat"
    end
    
    -- Afficher la structure du résultat
    local output = {"Résultat:"}
    for k, v in pairs(result) do
        table.insert(output, k .. ": " .. type(v))
        if type(v) == "table" then
            for i, val in ipairs(v) do
                table.insert(output, "  " .. i .. ": " .. tostring(val))
            end
        else
            table.insert(output, "  " .. tostring(v))
        end
    end
    
    return table.concat(output, "\n")
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

-- 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=' .. itemKey .. '&include=data'
end

return p