Aller au contenu

Module:Site officiel

Cette page fait l’objet d’une mesure de semi-protection étendue.
Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 11 juin 2015 à 20:43 et modifiée en dernier par Zebulon84 (discuter | contributions) (Utilisation de Langue.indicationMultilingue à la place de Commun.indicationDeLangue2 ; ajout de l'alias "lang", souvent utilisé.). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[voir] [modifier] [historique] [purger]

Ce module est utilisé par le modèle {{Site officiel}}.

local Lien = { }

local Outils = require( 'Module:Outils' )
local Wikidata = require( 'Module:Wikidata' ) 
local Langue = require( 'Module:Langue' )

local function caterreur(param)
	if mw.title.getCurrentTitle().namespace == 0 then
		return '[[Catégorie:Page du modèle Site officiel comportant une erreur|' .. string.upper(param or '?') .. ']]'
	else
		return ''
	end
end

function Lien.siteOfficiel( frame )
	local oldargs = Outils.extractArgs( frame )
	local args = {}
	for i, j in pairs(oldargs) do
		if j ~= '' then args[i] = j end
	end

	local titre = args['titre'] or args['title'] or 'Site officiel'
	local langue = args['langue'] or args['lang']
	local url = args['url'] or args[2] or args[1]
	local erreurs = '' -- les messages d'erreurs sont ajoutés ici

	if (not langue) and (args[2]) then
		langue = args[1] 
	end
	
	-- si pas d'URL, essayer Wikidata
	if not url and args.wikidata ~= 'non' then
		local links = Wikidata.getClaims{property = 'P856'}
		if links and #links > 1 then -- s'il y a plusieurs liens, essayer de faire un truc un peu potable
			return "Sites officiels : " .. Wikidata.formatAndCat{claims = links, property = 'P856', displayformat = 'weblink', showlang = true}
		end
		if links then
			return  Wikidata.formatAndCat{claims = links, property = 'P856', displayformat = 'weblink', showntext = titre, showlang = true}
		end
	end
	
	-- si toujours pas, ajouter les catégories d'erreur et sortir
	if not url then
		erreurs = erreurs .. caterreur('url')
		return erreurs
	end

	-- nettoyage de l'URL
	url = string.gsub ( string.gsub ( url, '%[', '%%5B' ), '%]', '%%5D' )
	if not ( string.match( url, '^http' ) or string.match( url, '^//' ) ) then
			url = 'http://' .. url
	end
	
	-- formatage de la langue
	local indicLangue = ''  -- langue telle qu'affichée dans le rendu final
	if langue then
		indicLangue = Langue.indicationMultilingue{ langue }
	end

	return indicLangue .. ' [' .. url .. ' ' .. titre .. ']' .. erreurs
end

return Lien