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

De alcolois
Aller à la navigation Aller à la recherche
Page créée avec « -- Module:ZoteroItem local p = {} -- Fonction principale : passez seulement itemKey function p.getItem(frame) local key = frame.args[1] if not key or key == "" then return "Erreur : veuillez fournir un itemKey (exemple : XNXIG5JQ)" end -- ID de votre groupe Zotero (modifiez-le si différent) local groupId = "4893620" -- Construction de l'URL complète vers l'API Zotero local url = mw.uri.encode( "https://api.zotero.org/groups/" .. grou... »
 
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
-- Module:ZoteroItem
local p = {}
local p = {}


-- Fonction principale : passez seulement itemKey
function p.getItem(frame)
function p.getItem(frame)
   local key = frame.args[1]
   local key = frame.args[1]
   if not key or key == "" then
   if not key or key == "" then
     return "Erreur : veuillez fournir un itemKey (exemple : XNXIG5JQ)"
     return "Erreur : aucun itemKey fourni"
   end
   end
 
   local groupId = frame.args.groupId or "4893620"
  -- ID de votre groupe Zotero (modifiez-le si différent)
   local url = string.format(
   local groupId = "4893620"
     "https://api.zotero.org/groups/%s/items/%s?format=json&include=data",
 
    mw.uri.encode(groupId),
  -- Construction de l'URL complète vers l'API Zotero
    mw.uri.encode(key)
   local url = mw.uri.encode(
     "https://api.zotero.org/groups/" .. groupId .. "/items/" .. key .. "?format=json&include=data"
   )
   )


  -- Appel HTTP via External Data
   local t, errs = mw.ext.externalData.getWebData{
   local data, errs = mw.ext.externalData.getWebData{
     url = url,
     url   = url,
     format = "JSON"
     format = "JSON"
   }
   }


   if errs then
   if errs then
     return "Erreur lors de la récupération Zotero : " .. table.concat(errs, "; ")
     return "Erreur HTTP ou parsing : " .. table.concat(errs, "; ")
  end
  if not t then
    return "Aucun résultat, possible timeout ou erreur silencieuse"
   end
   end
 
   if not t.__json then
  local json = data.__json
     return "Résultat inattendu : " .. mw.text.jsonEncode(t)
   if not json or not json.data then
     return "Aucun item avec la clé " .. mw.text.escape(key)
   end
   end


  -- Extrait les champs souhaités
   local d = t.__json.data
   local d = json.data
   if not d then
  local title    = mw.text.escape(d.title or d.caseName or "(sans titre)")
     return "Aucun champ 'data' dans la réponse"
  local authors  = {}
   if d.creators then
     for _, c in ipairs(d.creators) do
      table.insert(authors, mw.text.escape((c.firstName or "") .. " " .. (c.lastName or "")))
    end
   end
   end
  local dateDecided = mw.text.escape(d.dateDecided or "")
  local abstractNote = mw.text.escape(d.abstractNote or "")
  local urlField    = mw.text.escape(d.url or "")


   -- Production du HTML
   -- traitement normal...
  local out = {}
  table.insert(out, "<strong>Titre :</strong> " .. title)
  if #authors > 0 then
    table.insert(out, "<br><strong>Auteurs :</strong> " .. table.concat(authors, ", "))
  end
  if dateDecided ~= "" then
    table.insert(out, "<br><strong>Date décidée :</strong> " .. dateDecided)
  end
  if abstractNote ~= "" then
    table.insert(out, "<br><strong>Résumé :</strong> " .. abstractNote)
  end
  if urlField ~= "" then
    table.insert(out, '<br><strong>Lien :</strong> <a href="' .. urlField .. '" target="_blank">Voir sur CanLII</a>')
  end


  return table.concat(out)
end
end


return p
return p

Version du 12 juin 2025 à 17:11

La documentation pour ce module peut être créée à Module:ZoteroItem/doc

local p = {}

function p.getItem(frame)
  local key = frame.args[1]
  if not key or key == "" then
    return "Erreur : aucun itemKey fourni"
  end
  local groupId = frame.args.groupId or "4893620"
  local url = string.format(
    "https://api.zotero.org/groups/%s/items/%s?format=json&include=data",
    mw.uri.encode(groupId),
    mw.uri.encode(key)
  )

  local t, errs = mw.ext.externalData.getWebData{
    url = url,
    format = "JSON"
  }

  if errs then
    return "Erreur HTTP ou parsing : " .. table.concat(errs, "; ")
  end
  if not t then
    return "Aucun résultat, possible timeout ou erreur silencieuse"
  end
  if not t.__json then
    return "Résultat inattendu : " .. mw.text.jsonEncode(t)
  end

  local d = t.__json.data
  if not d then
    return "Aucun champ 'data' dans la réponse"
  end

  -- traitement normal...

end

return p