« 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 1 : Ligne 1 :
local p = {}
local p = {}
local cachedData = nil  -- 🔒 Cache unique pour la durée d’exécution de la page


-- Fonction principale : récupère les données d'un item Zotero par sa clé
-- Fonction utilitaire : extrait le titre de recherche
function p._fetchZoteroDataByKey(itemKey)
local function getQueryTitle()
if not itemKey or itemKey == '' then
local fullTitle = mw.title.getCurrentTitle().text
return nil
local base = fullTitle:match("^(.-),%s?%d") or fullTitle
base = mw.ustring.gsub(base, "’", "'")
local encoded = mw.uri.encode(base, nil):gsub("+", "%%20")
return encoded
end
 
-- Fonction de récupération de données Zotero
function p._fetchZoteroData()
if cachedData then
return cachedData
end
end


local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
local query = getQueryTitle()
local url = 'https://api.zotero.org/groups/4893620/items?q=' .. query .. '&qmode=titleCreatorYear&include=data&format=json'


local success, data = pcall(mw.ext.externalData.getExternalData, {
local success, data = pcall(mw.ext.externalData.getExternalData, {
Ligne 18 : Ligne 29 :
end
end


-- Correction ici : on s'assure que `data` est bien une table
local decoded
local decoded
if type(data) == "string" then
if type(data) == "string" then
Ligne 31 : Ligne 43 :
end
end


if decoded.__json and type(decoded.__json) == "table" then
if type(decoded[1]) == "table" then
return decoded.__json
cachedData = decoded[1]
return cachedData
end
end


return decoded
return nil
end
end


-- 🔍 Fonction de débogage simple (résumé)
-- Fonctions de débogage
function p.debugResult(frame)
function p.debugResult()
local itemKey = frame.args[1]
local d = p._fetchZoteroData()
local d = p._fetchZoteroDataByKey(itemKey)
if not d then return "Aucune donnée reçue" end
if not d then return "Aucune donnée reçue" end
 
local out = {}
local data = d.data or {}
table.insert(out, "✔ Clé : " .. (d.key or ''))
 
table.insert(out, "✔ Titre : " .. (d.caseName or ''))
local out = {
table.insert(out, "✔ Tribunal : " .. (d.court or ''))
"✔ Clé : " .. (data.key or ''),
table.insert(out, "✔ Date : " .. (d.dateDecided or ''))
"✔ Titre : " .. (data.caseName or ''),
table.insert(out, "✔ URL : " .. (d.url or ''))
"✔ Tribunal : " .. (data.court or ''),
table.insert(out, "✔ Auteur : " .. (d.firstName or '') .. " " .. (d.lastName or ''))
"✔ Date : " .. (data.dateDecided or ''),
"✔ URL : " .. (data.url or ''),
"✔ Auteur : " .. ((data.creators and data.creators[1] and data.creators[1].firstName or '') .. " " .. (data.creators and data.creators[1] and data.creators[1].lastName or ''))
}
 
return table.concat(out, "\n")
return table.concat(out, "\n")
end
end


-- 🔍 Fonction de débogage complet (JSON indenté)
function p.debugRawJson()
function p.debugRawJson(frame)
local d = p._fetchZoteroData()
local itemKey = frame.args[1]
if not d then
local d = p._fetchZoteroDataByKey(itemKey)
return "Aucune donnée reçue"
if not d then return "Aucune donnée reçue" end
end


local function indentJson(json)
local function indentJson(json)
Ligne 71 : Ligne 78 :
for i = 1, #json do
for i = 1, #json do
local c = json:sub(i, i)
local c = json:sub(i, i)
if c == '"' and json:sub(i - 1, i - 1) ~= '\\' then
if c == '"' and json:sub(i - 1, i - 1) ~= '\\' then
inString = not inString
inString = not inString
end
end
if not inString then
if not inString then
if c == '{' or c == '[' then
if c == '{' or c == '[' then
Ligne 95 : Ligne 104 :


local raw = mw.text.jsonEncode(d)
local raw = mw.text.jsonEncode(d)
return '<pre>' .. indentJson(raw) .. '</pre>'
local pretty = indentJson(raw)
return '<pre>' .. pretty .. '</pre>'
end
end


-- Fonctions d'accès simples à des champs (données principales)
-- Fonctions accessibles
function p.caseName(frame)
function p.caseName()
local d = p._fetchZoteroDataByKey(frame.args[1])
local d = p._fetchZoteroData()
return d and d.data and d.data.caseName or ''
return d and d.caseName or ''
end
end


function p.dateDecided(frame)
function p.dateDecided()
local d = p._fetchZoteroDataByKey(frame.args[1])
local d = p._fetchZoteroData()
return d and d.data and d.data.dateDecided or ''
return d and d.dateDecided or ''
end
end


function p.docketNumber(frame)
function p.docketNumber()
local d = p._fetchZoteroDataByKey(frame.args[1])
local d = p._fetchZoteroData()
return d and d.data and d.data.docketNumber or ''
return d and d.docketNumber or ''
end
end


function p.history(frame)
function p.history()
local d = p._fetchZoteroDataByKey(frame.args[1])
local d = p._fetchZoteroData()
return d and d.data and d.data.history or ''
return d and d.history or ''
end
end


function p.url(frame)
function p.url()
local d = p._fetchZoteroDataByKey(frame.args[1])
local d = p._fetchZoteroData()
return d and d.data and d.data.url or ''
return d and d.url or ''
end
end


function p.court(frame)
function p.court()
local d = p._fetchZoteroDataByKey(frame.args[1])
local d = p._fetchZoteroData()
return d and d.data and d.data.court or ''
return d and d.court or ''
end
end


function p.auteurPrenom(frame)
function p.auteurPrenom()
local d = p._fetchZoteroDataByKey(frame.args[1])
local d = p._fetchZoteroData()
local c = d and d.data and d.data.creators and d.data.creators[1]
return d and d.firstName or ''
return c and c.firstName or ''
end
end


function p.auteurNom(frame)
function p.auteurNom()
local d = p._fetchZoteroDataByKey(frame.args[1])
local d = p._fetchZoteroData()
local c = d and d.data and d.data.creators and d.data.creators[1]
return d and d.lastName or ''
return c and c.lastName or ''
end
end


-- Affichage URL pour vérification
-- Affichage de l'URL utilisée (pour vérification)
function p.debugUrl(frame)
function p.debugUrl()
local itemKey = frame.args[1]
local query = getQueryTitle()
return 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
return 'https://api.zotero.org/groups/4893620/items?q=' .. query .. '&qmode=titleCreatorYear&include=data&format=json'
end
end


return p
return p

Version du 11 juin 2025 à 16:12

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 utilitaire : extrait le titre de recherche
local function getQueryTitle()
	local fullTitle = mw.title.getCurrentTitle().text
	local base = fullTitle:match("^(.-),%s?%d") or fullTitle
	base = mw.ustring.gsub(base, "’", "'")
	local encoded = mw.uri.encode(base, nil):gsub("+", "%%20")
	return encoded
end

-- Fonction de récupération de données Zotero
function p._fetchZoteroData()
	if cachedData then
		return cachedData
	end

	local query = getQueryTitle()
	local url = 'https://api.zotero.org/groups/4893620/items?q=' .. query .. '&qmode=titleCreatorYear&include=data&format=json'

	local success, data = pcall(mw.ext.externalData.getExternalData, {
		url = url,
		format = 'json'
	})

	if not success or not data then
		return nil
	end

	-- Correction ici : on s'assure que `data` est bien une table
	local decoded
	if type(data) == "string" then
		local ok, result = pcall(mw.text.jsonDecode, data)
		if not ok or type(result) ~= "table" then
			return nil
		end
		decoded = result
	elseif type(data) == "table" then
		decoded = data
	else
		return nil
	end

	if type(decoded[1]) == "table" then
		cachedData = decoded[1]
		return cachedData
	end

	return nil
end

-- Fonctions de débogage
function p.debugResult()
	local d = p._fetchZoteroData()
	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()
	local d = p._fetchZoteroData()
	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()
	local d = p._fetchZoteroData()
	return d and d.caseName or ''
end

function p.dateDecided()
	local d = p._fetchZoteroData()
	return d and d.dateDecided or ''
end

function p.docketNumber()
	local d = p._fetchZoteroData()
	return d and d.docketNumber or ''
end

function p.history()
	local d = p._fetchZoteroData()
	return d and d.history or ''
end

function p.url()
	local d = p._fetchZoteroData()
	return d and d.url or ''
end

function p.court()
	local d = p._fetchZoteroData()
	return d and d.court or ''
end

function p.auteurPrenom()
	local d = p._fetchZoteroData()
	return d and d.firstName or ''
end

function p.auteurNom()
	local d = p._fetchZoteroData()
	return d and d.lastName or ''
end

-- Affichage de l'URL utilisée (pour vérification)
function p.debugUrl()
	local query = getQueryTitle()
	return 'https://api.zotero.org/groups/4893620/items?q=' .. query .. '&qmode=titleCreatorYear&include=data&format=json'
end

return p