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

De alcolois
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Balise : Révoqué
Ligne 142 : Ligne 142 :


-- Collections
-- 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
-- Relations
function p.relations(frame)
function p.relations(frame)
local itemKey = frame.args[1]
local itemKey = frame.args[1]
local groupName = "alcolois" -- adapter si besoin
local groupName = "alcolois"
local groupId = "4893620"
local groupId = "4893620"


Ligne 164 : Ligne 151 :


local rel = data.data.relations
local rel = data.data.relations
local out = {}
local urls = {}


for k, v in pairs(rel) do
for k, v in pairs(rel) do
if k == "dc:relation" and type(v) == "table" then
if k == "dc:relation" then
for _, zoteroUrl in ipairs(v) do
if type(v) == "string" then
local id = tostring(zoteroUrl):match("/items/(%w+)$")
local id = v:match("/items/(%w+)$")
if id then
if id then
local fullUrl = string.format(
table.insert(urls, string.format(
"https://www.zotero.org/groups/%s/%s/items/%s",
"https://www.zotero.org/groups/%s/%s/items/%s",
groupId,
groupId, groupName, id
groupName,
))
id
end
)
elseif type(v) == "table" then
table.insert(out, '<li><a href="' .. fullUrl .. '" target="_blank">' .. fullUrl .. '</a></li>')
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
elseif type(v) == "string" then
-- single relation
local id = v:match("/items/(%w+)$")
if id then
local fullUrl = string.format(
"https://www.zotero.org/groups/%s/%s/items/%s",
groupId,
groupName,
id
)
table.insert(out, '<li><a href="' .. fullUrl .. '" target="_blank">' .. fullUrl .. '</a></li>')
end
end
end
end
end
end


if #out > 0 then
if #urls == 0 then
return "dc:relation:<ul>" .. table.concat(out, "") .. "</ul>"
return ""
end
end


return ""
return table.concat(urls, ", ")
end
end


return p
return p

Version du 12 juin 2025 à 18:10

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

local p = {}

-- Fonction utilitaire pour récupérer les données JSON Zotero
local function fetchZoteroData(itemKey)
	if not itemKey or itemKey == "" then return nil end
	local url = string.format(
		"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 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)
	return getField(frame, { "links", "alternate", "href" })
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

function p.language(frame)
	return getField(frame, { "data", "language" })
end

function p.shortTitle(frame)
	return getField(frame, { "data", "shortTitle" })
end

function p.url(frame)
	return getField(frame, { "data", "url" })
end

function p.accessDate(frame)
	return getField(frame, { "data", "accessDate" })
end

function p.rights(frame)
	return getField(frame, { "data", "rights" })
end

function p.extra(frame)
	return getField(frame, { "data", "extra" })
end

function p.dateAdded(frame)
	return getField(frame, { "data", "dateAdded" })
end

function p.dateModified(frame)
	return getField(frame, { "data", "dateModified" })
end

-- Créateurs (formaté en liste de noms complets)
function p.creators(frame)
	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

-- Tags
function p.tags(frame)
	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

-- Collections
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

	if #urls == 0 then
		return ""
	end

	return table.concat(urls, ", ")
end

return p