Aller au contenu

Module:Commons tab

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 6 mars 2025 à 17:08 et modifiée en dernier par GrandEscogriffe (discuter | contributions) (version avec les messages d'erreur au cas où). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.
(diff) ← Version précédente | Version actuelle (diff) | Version suivante → (diff)

 Documentation[créer] [purger]
local p = {}

function p.getData(frame)
	local args = frame.args
	
	local filename = args[1]
	local datatable = mw.ext.data.get(filename)
	
	local showerror = args.showerror and string.lower(args.showerror) ~= "false"
	
	local selectname, selectvalue, selectindex, resultindex, resultname, resultvalue
	selectname = args[2]
	selectvalue = args[3]
	resultname = args[4]
	
	for j,field in ipairs(datatable.schema.fields) do
		if field.name == selectname then
			if selectindex then
				if showerror then error('Table has several columns with the selection name') end
			else
				selectindex = j
				if resultindex and not showerror then break end
			end
		end
		if field.name == resultname then
			if resultindex then
				if showerror then error('Table has several columns with the result name') end
			else
				resultindex = j
				if selectindex and not showerror then break end
			end
		end
	end
	
	if showerror and not selectindex then error('Table has no column with the selection name') end
	if showerror and not resultindex then error('Table has no column with the result name') end
	
	if selectindex and resultindex then
		for i,row in ipairs(datatable.data) do
			if tostring(row[selectindex]) == selectvalue then
				if resultvalue then
					if showerror then error('Table has several rows with the selection value') end
				else
					resultvalue = tostring(row[resultindex])
					if not showerror then break end
				end
			end
		end
		if showerror and not resultvalue then error('Table has no row with the selection value') end
	end
	
	return resultvalue
end
 
return p