Module:Listeur

Útá Wikipedia.

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

local p = {}
local wikidata = require 'Module:Wikidata' 
local makeTable = require 'Module:Fabricant de tables'
local artworkbox = require "Module:Cartel d'art".artworkbox

-- i18n ===========================================================
local inI18n = { -- i18N des inputs
	['art'] = 'artworks',
	['catalogue'] = 'catalogue',
	['exposition'] = 'exhibition',
}

local outI18n = {
	wdqlink = 'Mettre à jour',
	invalidlisttype = 'Type de liste inconnu',
}

local function translate(msg)
	return outI18n[msg] or msg
end

local function getMeaning(msg)
	return inI18n[msg] or msg
end

-- =====================================================================
-- Commonly used data


local function getimage(item)
	local image = wikidata.formatStatements({entity = item, property ='P18', numval=1})
	return '[[File:' .. (image or 'Defaut 2.svg') .. '|200px]]'

end

local function catnum(item, fakearg, catalogue)
	return wikidata.formatStatements{entity = item, property = 'P528', qualifier = 'P972', qualifiervalue = catalogue}
end

local function exhibitioncatalogue(item, exhibition, catalogue)
	local val
	if catalogue then
		val = catnum(item, exhibition, catalogue)
	end
	if not val then -- si pas de n° de catalogue, essayer le numéro donné en qualificaif dans "historique des expos"
		val = wikidata.formatStatements({entity = item, property = 'P608', showonlyqualifier = {'P528'}, targetvalue = exhibition, qualifiervalue = catalogue})
	end
	return val
end

-- LISTS 
local listtypes = {
	artworks = {
		headers = { [2] = 'Image', [3] = 'Description'},
		columns = {
			[2] = 'image',
			[3]= function(item) return artworkbox(item, {separator = 'new line'}) end,
		},
	},
	exhibition = {
		headers = { [1] = 'N° cat', [2] = 'Image', [3] = 'Description'},
		columns = {
			[1] = function(item, exhibition, catalogue) return exhibitioncatalogue(item, exhibition, catalogue) end,
			[2] = 'image',
			[3]= function(item) return artworkbox(item, {separator = 'new line'}) end,
		},
		sortkey = function(row) return tonumber(row[1]) end, -- N° de catalogue, à améliorer pour les 123 bis etc.
	},
	default = {
		columns = {
			[1] = 'image',
			[2] = function(item) return wikidata._getLabel(item) end
		}
	}
}

local function getvalue(entity, query, topic, catalogue)
	local querylibrary = { -- maps keyword to the function it represents
		image = getimage
	}
	-- if query is a string, look for the function it represents
	if type(query) == 'string' and querylibrary[query] then
		 query = querylibrary[query]
	end
	-- if query is is a function, expand it
	if type(query) == 'function' then
		return query(entity, topic, catalogue)
	end

	return query
end

local function wdqlink(query, topic)
	if not query then
		return ''
	end
	query = getvalue(entity, query, topic)
	query = string.gsub(query, 'Q', '')
	local link = mw.getCurrentFrame():preprocess('https://tools.wmflabs.org/listeria/index.php?action=update&lang=wikidata&page={{FULLPAGENAMEE}}')
	local text = translate('wdqlink')
	return '[' .. link .. ' ' .. text .. ']'
end


local function splitlist(list)
	if type(list) == 'table' then
		return list
	else
		return mw.text.split(list, ',')
	end
end

local function makerow(item, params, topic, catalogue)
	local entity = mw.wikibase.getEntityObject(item)
	-- get cell values
	local vals = {}
	for _, column in pairs(params.columns) do
		local val = getvalue(entity, column, topic, catalogue) or ''
		table.insert(vals, val)
	end

	return vals
end

function p.makelist(items, topic, listtype, query, catalogue) 
	local obj = makeTable:new()
	items = splitlist(items)
	local rows = {}

	listtype = getMeaning(listtype) or 'default'
	local listparams = listtypes[listtype]
	if not listparams then
		return translate('invalidlisttype') .. ': ' .. listtype
	end

	if listparams.headers then
		obj:addHeaders(listparams.headers)
	end
	
	for i, item in pairs(items) do
		item = 'Q' .. item
		local row = makerow(item, listparams, topic, catalogue)
		obj:addRow(row, tonumber(row[1]))
	end

	if not style then
		style = {width = '100%'}
	end


	-- add rows
	for i, row in pairs(rows) do
		obj:addRow(row)
	end
	
--mise à jour automatique en rade	local link = wdqlink(query, topic)
--	return link .. obj:show()
	return obj:show()
end

function p.listfromFrame(frame)
	local nargs = {}
	for i, j in pairs(frame.args) do
		if j ~= '' then nargs[i] = j end
	end
	return p.makelist(nargs.items, nargs.topic, nargs.type, nargs.query, nargs.catalogue) 
end
return p