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

Aucun résumé des modifications
Aucun résumé des modifications
 
(12 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


function p.getItem(frame)
-- Fonction utilitaire pour récupérer les données JSON Zotero
    local key = frame.args[1]
local function fetchZoteroData(itemKey)
    if not key or key == "" then
if not itemKey or itemKey == "" then return nil end
        return "Erreur : itemKey requis"
local url = string.format(
    end
"https://api.zotero.org/groups/4893620/items/%s?format=json&include=data",
itemKey
)
local data, errors = mw.ext.externalData.getExternalData{
url = url,
format = "JSON",
data = { json = "__json" }
}
if errors or not data or not data.json then
return nil
end
return data.json
end
 
-- Fonction utilitaire de formatage
local function htmlDecode(str)
if not str then return nil end
return str
:gsub(":", ":")
:gsub("[", "[")
:gsub("]", "]")
:gsub(""", '"')
:gsub("&", "&")
end
 
-- Fonction générique pour retourner un champ simple
local function getField(frame, path)
local itemKey = frame.args[1]
local data = fetchZoteroData(itemKey)
if not data then return "" end
local current = data
for _, key in ipairs(path) do
if type(current) ~= "table" or current[key] == nil then
return ""
end
current = current[key]
end
return mw.text.nowiki(tostring(current))
end
 
-- Fonctions individuelles
 
function p.key(frame)
return getField(frame, { "key" })
end
 
function p.alternateLink(frame)
local data = fetchZoteroData(frame.args[1])
if data and data.links and data.links.alternate and data.links.alternate.href then
return htmlDecode(data.links.alternate.href)
end
return ""
end
 
function p.caseName(frame)
return getField(frame, { "data", "caseName" })
end
 
function p.abstractNote(frame)
return getField(frame, { "data", "abstractNote" })
end
 
function p.court(frame)
return getField(frame, { "data", "court" })
end
 
function p.dateDecided(frame)
return getField(frame, { "data", "dateDecided" })
end
 
function p.docketNumber(frame)
return getField(frame, { "data", "docketNumber" })
end
 
function p.reporter(frame)
return getField(frame, { "data", "reporter" })
end
 
function p.reporterVolume(frame)
return getField(frame, { "data", "reporterVolume" })
end
 
function p.firstPage(frame)
return getField(frame, { "data", "firstPage" })
end
 
function p.history(frame)
return getField(frame, { "data", "history" })
end


    local groupId = "4893620"
function p.language(frame)
    local url = string.format(
return getField(frame, { "data", "language" })
        "https://api.zotero.org/groups/%s/items/%s?format=json&include=data",
end
        groupId,
        key
    )


    local data, errors = mw.ext.externalData.getExternalData{
function p.shortTitle(frame)
        url = url,
return getField(frame, { "data", "shortTitle" })
        format = "JSON",
end
        data = { json = "__json" }
    }


    if errors then
function p.url(frame)
        return "Erreur ExternalData : " .. table.concat(errors, "; ")
local data = fetchZoteroData(frame.args[1])
    elseif not data or not data.json then
if data and data.data and data.data.url then
        return "Erreur : aucune donnée retournée"
return htmlDecode(data.data.url)
    end
end
return ""
end


    local d = data.json.data
function p.rights(frame)
    if not d then
return getField(frame, { "data", "rights" })
        return "Erreur : champ 'data' manquant dans le JSON"
end
    end
 
function p.extra(frame)
return getField(frame, { "data", "extra" })
end


    local escape = mw.text.nowiki -- alias utile
local function trimDate(dateStr)
if type(dateStr) == "string" then
return mw.text.nowiki(dateStr:sub(1, 10))
end
return ""
end


    local out = {}
function p.dateAdded(frame)
    table.insert(out, "<strong>Titre :</strong> " .. escape(d.title or d.caseName or "(sans titre)"))
local data = fetchZoteroData(frame.args[1])
if not data or not data.data or not data.data.dateAdded then return "" end
return trimDate(data.data.dateAdded)
end


    if d.creators then
function p.dateModified(frame)
        local authors = {}
local data = fetchZoteroData(frame.args[1])
        for _, c in ipairs(d.creators) do
if not data or not data.data or not data.data.dateModified then return "" end
            table.insert(authors, escape((c.firstName or "") .. " " .. (c.lastName or "")))
return trimDate(data.data.dateModified)
        end
end
        table.insert(out, "<br><strong>Auteurs :</strong> " .. table.concat(authors, ", "))
    end


    if d.dateDecided then
function p.accessDate(frame)
        table.insert(out, "<br><strong>Date :</strong> " .. escape(d.dateDecided))
local data = fetchZoteroData(frame.args[1])
    end
if not data or not data.data or not data.data.accessDate then return "" end
return trimDate(data.data.accessDate)
end


    if d.abstractNote then
-- Créateurs (formaté en liste de noms complets)
        table.insert(out, "<br><strong>Résumé :</strong> " .. escape(d.abstractNote))
function p.creators(frame)
    end
local itemKey = frame.args[1]
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.creators then return "" end
local authors = {}
for _, c in ipairs(data.data.creators) do
local name = c.name or ((c.firstName or "") .. " " .. (c.lastName or ""))
if name ~= "" then
table.insert(authors, mw.text.nowiki(name))
end
end
return table.concat(authors, ", ")
end


    if d.url then
-- Tags
        table.insert(out, '<br><strong>Lien :</strong> <a href="' .. escape(d.url) .. '" target="_blank">Voir</a>')
function p.tags(frame)
    end
local itemKey = frame.args[1]
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.tags then return "" end
local tags = {}
for _, t in ipairs(data.data.tags) do
if t.tag then
table.insert(tags, mw.text.nowiki(t.tag))
end
end
return table.concat(tags, ", ")
end


    return table.concat(out)
-- Collections
function p.collections(frame)
local itemKey = frame.args[1]
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.collections then return "" end
if #data.data.collections == 0 then return "" end
local out = {}
for _, id in ipairs(data.data.collections) do
table.insert(out, mw.text.nowiki(id))
end
return table.concat(out, ", ")
end
end


-- Relations
function p.relations(frame)
local itemKey = frame.args[1]
local groupName = "alcolois"
local groupId = "4893620"
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.relations then return "" end
local rel = data.data.relations
local urls = {}
for k, v in pairs(rel) do
if k == "dc:relation" then
if type(v) == "string" then
local id = v:match("/items/(%w+)$")
if id then
table.insert(urls, string.format(
"https://www.zotero.org/groups/%s/%s/items/%s",
groupId, groupName, id
))
end
elseif type(v) == "table" then
for _, url in ipairs(v) do
local id = tostring(url):match("/items/(%w+)$")
if id then
table.insert(urls, string.format(
"https://www.zotero.org/groups/%s/%s/items/%s",
groupId, groupName, id
))
end
end
end
end
end
return table.concat(urls, ", ")
end
function p.notes(frame)
local itemKey = frame.args[1]
if not itemKey or itemKey == "" then return "" end
local url = string.format(
"https://api.zotero.org/groups/4893620/items/%s/children?format=json&include=data",
mw.uri.encode(itemKey)
)
local data, errors = mw.ext.externalData.getExternalData{
url = url,
format = "JSON",
data = { notes = "__json" }
}
if errors or not data or not data.notes then
return ""
end
local output = {}
for _, item in ipairs(data.notes) do
if item.data and item.data.itemType == "note" then
local rawHtml = item.data.note or ""
local url = (item.links and item.links.alternate and item.links.alternate.href) or ""
-- Étape 1 : supprimer tout sauf les balises autorisées
-- Autorisé : <p>, </p>, <h1>, </h1>, <a href="...">, </a>
local cleaned = rawHtml:gsub('<(.-)>', function(tag)
-- Cas <a href="...">
if tag:match('^a%s+href=') then
return "<" .. tag .. ">"
end
-- Cas </a>, <p>, </p>, <h1>, </h1>
if tag == "/a" or tag == "p" or tag == "/p" or tag == "h1" or tag == "/h1" then
return "<" .. tag .. ">"
end
-- Sinon : supprimer
return ""
end)
if cleaned ~= "" then
table.insert(output, cleaned)
end
if url ~= "" then
table.insert(output, url)
end
end
end
return table.concat(output, "\n\n")
end
return p
return p