Aller au contenu

Module:Listeur

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 17 août 2015 à 20:13 et modifiée en dernier par Zolo (discuter | contributions) (Nouvelle page : local p = {} local wikidata = require 'Module:Wikidata' local makeTable = require 'Module:Fabricant de tables' local artworkbox = require "Module:Cartel d'art".artworkbox -- i18...). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

 Documentation[créer] [purger]
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
	['exposition'] = 'artworks',
	["oeuvres d'art"] = 'artworks',
	['catalogue'] = 'catalogue',
	['collection'] = 'collection',
}

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 'Missing image text.png') .. '|200px]]'

end

local function catnum(item, catalogue, artworks)
	local val = wikidata._formatStatements{entity = 'Q1314013', property = 'P528', qualifier = 'P972', qualifiervalue = catalogue}
	if (not val) and artworks then -- s'il s'agit d'un catalogue d'exposition , une autre possibilité est de chercher en qualificatif de l'exposition
		val = wikidata.showQualifier({entity = 'Q1314013', property = 'P608', qualifiers = {'P528'}, targetvalue = artworks, lang=lang}) 
	end
	return val
end

-- LISTS 
local listtypes = {
	artworks = {
		columns = {
			[1] = function(item, catalogue, artworks) return catnum(item, catalogue, artworks) end,
			[2] = 'image',
			[3]= function(item) return artworkbox(item, {separator = '<br />'}) end,
		},
		sortkey = 1, -- catalogue as the sortkey
	},
	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, catalogue, topic)
	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
	
	for i, item in pairs(items) do
		item = 'Q' .. item
		local row = makerow(item, listparams, topic, catalogue)
		obj:addRow(row)
	end
	obj:sortColumn(listparams.sortkey)
	local header =  {}
	for i, j in pairs(listparams.columns) do -- à améliorer pour les ooptions de tri, etc.
		local text
		if type(j) == 'table' then
			text = j.header
		end
		table.insert(header, text)
	end


	if not style then
		style = {width = '100%'}
	end
	obj:addHeaders(header)

	-- add rows
	for i, row in pairs(rows) do
		obj:addRow(row)
	end
	local link = wdqlink(query, topic)
	return link .. 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