Aller au contenu

Poképédia subit actuellement quelques soucis techniques !
L'équipe est au courant des problèmes et fait le nécessaire pour les arranger le plus rapidement possible. Merci de votre compréhension !

Module:Carte JCC/wb

De Poképédia

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

local p = {}

local PROP_IMAGE = 'P55'
local PROP_EXTENSION = 'P140'
local PROP_SOUS_EXTENSION = 'P141'
local PROP_NUMERO_CARTE = 'P143'
local PROP_NUMERO_CARTE_SECOURS = 'P65'
local PROP_NUMERO_MAX_SET = 'P144'
local PROP_NUMERO_MAX_SET_SECOURS = 'P68'
local PROP_STADE = 'P97'

local IMAGE_PAR_DEFAUT = "Verso d'une carte JCC.png"

local function trim(value)
	if value == nil then
		return ''
	end

	return mw.text.trim(tostring(value))
end

local function non_vide(value)
	return trim(value) ~= ''
end

local function get_args(frame)
	local args = {}

	local function merge(source)
		if not source then
			return
		end

		for key, value in pairs(source) do
			if value ~= nil then
				args[key] = value
			end
		end
	end

	merge(frame:getParent() and frame:getParent().args)
	merge(frame.args)

	return args
end

local function get_statements(entity, property_id)
	if not entity then
		return {}
	end

	local ok, statements = pcall(function()
		return entity:getBestStatements(property_id)
	end)

	if not ok or type(statements) ~= 'table' then
		return {}
	end

	return statements
end

local function get_datavalue(statement)
	if not statement or not statement.mainsnak or not statement.mainsnak.datavalue then
		return nil
	end

	return statement.mainsnak.datavalue.value
end

local function get_first_string(entity, property_id)
	for _, statement in ipairs(get_statements(entity, property_id)) do
		local value = get_datavalue(statement)
		if type(value) == 'string' then
			return value
		end
	end

	return nil
end

local function get_first_item_id(entity, property_id)
	for _, statement in ipairs(get_statements(entity, property_id)) do
		local value = get_datavalue(statement)
		if type(value) == 'table' and value.id then
			return value.id
		end
	end

	return nil
end

local function get_first_item_title_or_label(entity, property_id)
	local item_id = get_first_item_id(entity, property_id)
	if not item_id then
		return ''
	end

	return trim(mw.wikibase.getSitelink(item_id) or mw.wikibase.getLabel(item_id) or '')
end

local function get_redirect_target_title(title_text)
	local title = mw.title.new(title_text)
	if not title or not title.redirectTarget then
		return nil
	end

	return title.redirectTarget.prefixedText or title.redirectTarget.text
end

local function resolve_entity(args)
	local explicit_item = trim(args.item)
	local requested_title = trim(args[1])

	if requested_title == '' then
		requested_title = trim(args.page)
	end

	local item_id = explicit_item
	if item_id == '' and requested_title ~= '' then
		item_id = mw.wikibase.getEntityIdForTitle(requested_title) or ''

		if item_id == '' then
			local redirect_target = get_redirect_target_title(requested_title)
			if redirect_target then
				item_id = mw.wikibase.getEntityIdForTitle(redirect_target) or ''
			end
		end
	end

	local entity = nil
	if item_id ~= '' then
		entity = mw.wikibase.getEntity(item_id)
	end

	local page_title = requested_title
	if page_title == '' and item_id ~= '' then
		page_title = trim(mw.wikibase.getSitelink(item_id) or '')
	end

	return {
		entity = entity,
		item_id = item_id,
		page_title = page_title,
	}
end

local function is_horizontal(args, entity)
	if trim(args.horizontal) == 'oui' then
		return true
	end

	return trim(get_first_string(entity, PROP_STADE)) == 'TURBO'
end

local function is_jumbo(args)
	return trim(args.jumbo) == 'oui'
end

local function has_zoom(args)
	local zoom = trim(args.zoom)
	return zoom == '' or zoom == 'oui'
end

local function normalize_filename(name)
	local trimmed = trim(name)

	trimmed = mw.ustring.gsub(trimmed, '^[Ff]ichier%s*:%s*', '')
	trimmed = mw.ustring.gsub(trimmed, '^[Ff]ile%s*:%s*', '')

	return trimmed
end

local function get_image_name(args, entity)
	local explicit_image = normalize_filename(args.image)
	if explicit_image ~= '' then
		return explicit_image
	end

	return normalize_filename(get_first_string(entity, PROP_IMAGE) or IMAGE_PAR_DEFAUT)
end

local function get_link_target(page_title, image_name)
	if page_title ~= '' then
		return page_title
	end

	return 'Fichier:' .. image_name
end

local function get_alt_text(page_title, image_name)
	if page_title ~= '' then
		return page_title
	end

	return image_name
end

local function get_extension_text(entity)
	return get_first_item_title_or_label(entity, PROP_EXTENSION)
end

local function get_subextension_text(entity)
	return trim(get_first_string(entity, PROP_SOUS_EXTENSION) or '')
end

local function get_card_number(args, entity)
	local explicit_number = trim(args['numéro'])
	if explicit_number ~= '' then
		return explicit_number
	end

	return trim(
		get_first_string(entity, PROP_NUMERO_CARTE)
		or get_first_string(entity, PROP_NUMERO_CARTE_SECOURS)
		or ''
	)
end

local function get_set_max_number(entity)
	return trim(
		get_first_string(entity, PROP_NUMERO_MAX_SET)
		or get_first_string(entity, PROP_NUMERO_MAX_SET_SECOURS)
		or ''
	)
end

local function get_container_class(horizontal, jumbo)
	local class_name = 'conteneur-carte'
	if horizontal then
		class_name = class_name .. '-horizontale'
	end
	if jumbo then
		class_name = class_name .. '-jumbo'
	end

	return class_name .. '-jcc'
end

local function get_card_class(args, jumbo)
	local class_name = ''
	if has_zoom(args) then
		class_name = 'survol-carte '
	end

	class_name = class_name .. 'carte'
	if jumbo then
		class_name = class_name .. '-jumbo'
	end

	return class_name .. '-jcc'
end

local function get_text_class(horizontal, jumbo)
	local class_name = 'texte-carte'
	if horizontal then
		class_name = class_name .. '-horizontale'
	end
	if jumbo then
		class_name = class_name .. '-jumbo'
	end

	return class_name .. '-jcc'
end

local function get_text_width_class(horizontal, jumbo)
	local class_name = 'forcer-largeur-texte'
	if horizontal then
		class_name = class_name .. '-horizontale'
	end
	if jumbo then
		class_name = class_name .. '-jumbo'
	end

	return class_name .. '-jcc'
end

local function get_image_class(horizontal, jumbo)
	local class_name = 'taille-carte'
	if horizontal then
		class_name = class_name .. '-horizontale'
	end
	if jumbo then
		class_name = class_name .. '-jumbo'
	else
		class_name = class_name .. '-jcc'
	end

	if jumbo then
		class_name = class_name .. '-jcc'
	end

	return class_name
end

local function get_image_size_option(horizontal, jumbo)
	if jumbo then
		return nil
	end

	if horizontal then
		return 'x245px'
	end

	return '245px'
end

local function build_image_wikitext(page_title, image_name, horizontal, jumbo)
	local options = {
		'link=' .. get_link_target(page_title, image_name),
		'alt=' .. get_alt_text(page_title, image_name),
		'class=' .. get_image_class(horizontal, jumbo),
	}

	local size_option = get_image_size_option(horizontal, jumbo)
	if size_option then
		options[#options + 1] = size_option
	end

	return string.format('[[Fichier:%s|%s]]', image_name, table.concat(options, '|'))
end

local function render_name(frame, page_title)
	if page_title == '' then
		return ''
	end

	return frame:expandTemplate{
		title = 'Lien carte',
		args = { [1] = page_title },
	}
end

local function render_extension(frame, entity)
	local extension_title = get_extension_text(entity)
	if extension_title == '' then
		return ''
	end

	local subextension = get_subextension_text(entity)
	local template_args = { [1] = extension_title }
	if subextension ~= '' then
		template_args[2] = subextension
	end

	return frame:expandTemplate{
		title = 'Extension courte',
		args = template_args,
	}
end

local function render_generated_text(frame, args, resolved, horizontal, jumbo)
	local name_markup = render_name(frame, resolved.page_title)
	local extension_markup = render_extension(frame, resolved.entity)
	local card_number = get_card_number(args, resolved.entity)
	local set_max_number = get_set_max_number(resolved.entity)
	local width_class = get_text_width_class(horizontal, jumbo)
	local pieces = {}

	if name_markup ~= '' then
		pieces[#pieces + 1] =
			'<span class="ligne-texte-carte-jcc"><span class="' .. width_class .. '"><b>'
			.. name_markup
			.. '</b></span></span>'
	end

	if extension_markup ~= '' then
		pieces[#pieces + 1] =
			'<br><span class="ligne-texte-carte-jcc" style="line-height: 100%; margin-top: 2px; margin-bottom: 2px"><span class="'
			.. width_class
			.. '"><small>'
			.. extension_markup
			.. '</small></span></span>'
	end

	if card_number ~= '' then
		local text = '<small>' .. card_number
		if set_max_number ~= '' then
			text = text .. '<small>/' .. set_max_number .. '</small>'
		end
		text = text .. ' </small>'

		pieces[#pieces + 1] = '<br><span class="ligne-texte-carte-jcc">' .. text .. '</span>'
	end

	return table.concat(pieces)
end

local function render_text_block(frame, args, resolved, horizontal, jumbo)
	local custom_text = args.texte
	local custom_text_trimmed = trim(custom_text)
	if custom_text_trimmed == 'non' then
		return ''
	end

	local text_class = get_text_class(horizontal, jumbo)
	local content = ''

	if non_vide(custom_text) then
		content = '<span class="ligne-texte-carte-jcc">' .. tostring(custom_text) .. '</span>'
	else
		content = render_generated_text(frame, args, resolved, horizontal, jumbo)
	end

	if content == '' then
		return ''
	end

	return '<div class="' .. text_class .. '">' .. content .. '</div>'
end

function p.main(frame)
	local args = get_args(frame)
	local resolved = resolve_entity(args)
	local horizontal = is_horizontal(args, resolved.entity)
	local jumbo = is_jumbo(args)
	local page_title = resolved.page_title
	local image_name = get_image_name(args, resolved.entity)
	local image_wikitext = build_image_wikitext(page_title, image_name, horizontal, jumbo)
	local card_class = get_card_class(args, jumbo)
	local container_class = get_container_class(horizontal, jumbo)
	local pieces = {
		'<div class="' .. container_class .. '"><div class="' .. card_class .. '">',
	}

	if non_vide(args.x) then
		pieces[#pieces + 1] = '<div class="note-carte-jcc"><b>× ' .. tostring(args.x) .. '</b></div> '
	end

	pieces[#pieces + 1] = image_wikitext
	pieces[#pieces + 1] = '</div>'
	pieces[#pieces + 1] = render_text_block(frame, args, resolved, horizontal, jumbo)
	pieces[#pieces + 1] = '</div>'

	return table.concat(pieces)
end

return p