« Module:Site officiel » : différence entre les versions

Une page de Wikipédia, l'encyclopédie libre.
Contenu supprimé Contenu ajouté
Zolo (discuter | contributions)
texte moins bizarre lorsque les valeurs viennent de Wikidata, et qu'il y en a plusieurs
le Module:Wikidata applique maintenant automatiquement le "maxLang", voir 209814449 ; sur des dizaines de modules faisant usage de « showlang=true », ce module avait été le seul à activer le "maxLang"
 
(47 versions intermédiaires par 12 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
local Lien = { }
local Lien = { }


local Outils = require( 'Module:Outils' )
local Verif = require "Module:Correction syntaxique"
local Wikidata = require( 'Module:Wikidata' )
local defaultLang = 'fr'
local Commun = require( 'Module:Biblio/Commun' )


local function caterreur(param)
local function caterreur(param)
Ligne 14 : Ligne 13 :


function Lien.siteOfficiel( frame )
function Lien.siteOfficiel( frame )
local oldargs = Outils.extractArgs( frame )
local templateargs = frame:getParent().args
local args = {}
local args = {}
for i, j in pairs(oldargs) do
for k, v in pairs(templateargs) do
if j ~= '' then args[i] = j end
if type(k) == 'number' then
v = mw.text.trim(v)
end
if v ~= '' then
args[k] = v
end
end
end
local nocaturl = args.nocaturl


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

local erreursarguments = Verif.verif(args, {'titre', 'title', 1, 2, 'langue', 'lang', 'url', 'wikidata', 'entity', 'id', 'nocaturl'}, 'Modèle Site officiel avec un paramètre inconnu‎')
if erreursarguments then
erreurs = erreurs .. erreursarguments
end

local titredefaut = 'Site officiel'
local titre = args['titre'] or args['title'] or titredefaut
local langue = args['langue'] or args['lang']
local url = args['url'] or args[2] or args[1]


if (not langue) and (args[2]) then
if (not langue) and (args[2]) then
langue = args[1]
langue = args[1]
end
local id = args.id or args.entity or args.wikidata
if url and url:sub(1, 1) == 'Q' then
id = url
url = nil
end
end
-- si pas d'URL, essayer Wikidata
-- si pas d'URL, essayer Wikidata, et si ça marche, sortir de ce module
if not url and args.wikidata ~= 'non' then
if not url and (id ~= '-') then
local links = Wikidata.getClaims{property = 'P856'}
local Wikidata = require "Module:Wikidata"
local isinlang = defaultLang
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}
local links = Wikidata.getClaims{entity = id, property = 'P856', excludespecial = true, isinlang = isinlang}
if not links then
links = Wikidata.getClaims{entity = id, property = 'P856', excludespecial = true}
isinlang = null
end
end
local Weblink = require 'Module:Weblink'
if links then
local statementformat = function(statement, args)
return Wikidata.formatAndCat{claims = links, property = 'P856', displayformat = 'weblink', showntext = titre, showlang = true}
if statement.mainsnak.snaktype == 'value' then
local value = statement.mainsnak.datavalue.value
local title = Wikidata.getFormattedQualifiers(statement, {'P1476'}, {isinlang = 'fr'})
if not title then
title = Wikidata.getFormattedQualifiers(statement, {'P1476'})
end
if not title then
title = args.text
end
return Weblink.makelink(value, title)
else
return Wikidata.formatSnak( statement.mainsnak, args )
end
end
if links and (#links > 1) then
return '<cite class="ouvrage" id="site_officiel" style="font-style: normal;">' .. "Sites officiels : " .. Wikidata.formatAndCat{claims = links, property = 'P856', displayformat = 'weblink', showlang = true, excludespecial = true, isinlang = isinlang, statementformat = statementformat} .. '</cite>' .. erreurs
elseif links then
if not (titre == titredefaut) then
statementformat = nil
end
return '<cite class="ouvrage" id="site_officiel" style="font-style: normal;">' .. Wikidata.formatAndCat{claims = links, property = 'P856', displayformat = 'weblink', showlang = true, text = titre, isinlang = isinlang, statementformat = statementformat} .. '</cite>' .. erreurs
end
end
end
end
Ligne 42 : Ligne 83 :
-- si toujours pas, ajouter les catégories d'erreur et sortir
-- si toujours pas, ajouter les catégories d'erreur et sortir
if not url then
if not url then
erreurs = erreurs .. caterreur('url')
if not nocaturl then
erreurs = erreurs .. caterreur('url')
end
return erreurs
return erreurs
end
end


-- nettoyage de l'URL
-- nettoyage de l'URL
url = string.gsub ( string.gsub ( url, '%[', '%%5B' ), '%]', '%%5D' )
url = url:gsub( '%[', '%%5B' ):gsub( '%]', '%%5D' )
if not ( string.match( url, '^http' ) or string.match( url, '^//' ) ) then
if not ( url:match( '^http' ) or url:match( '^//' ) ) then
url = 'http://' .. url
url = 'http://' .. url
end
end
-- formatage de la langue
-- formatage de la langue
local indicLangue, codeLangue, erreurLangue = '', nil, nil -- langue telle qu'affichée dans le rendu final
local indicLangue = '' -- langue telle qu'affichée dans le rendu final
if langue then
if langue then
if langue ~= 'fr' then
indicLangue, codeLangue, erreurLangue = Commun.indicationDeLangue2 ( args, validArg, langue )
local Langue = require "Module:Langue"
if erreurLangue then
indicLangue = Langue.indicationMultilingue{ langue } .. ' '
erreurs = erreurs .. caterreur('langue')
if indicLangue:find('class="error"', nil, true) then
erreurs = erreurs .. caterreur('langue')
end
end
end
end
end


return indicLangue .. '&#32;[' .. url .. ' ' .. titre .. ']' .. erreurs
return '<cite class="ouvrage" id="site_officiel" style="font-style: normal;">' .. indicLangue .. '[' .. url .. ' ' .. titre .. ']' .. '</cite>' .. erreurs
end
end



Dernière version du 20 novembre 2023 à 06:21

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

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

local Lien = { }

local Verif = require "Module:Correction syntaxique"
local defaultLang = 'fr'

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 templateargs = frame:getParent().args
	local args = {}
	for k, v in pairs(templateargs) do
		if type(k) == 'number' then
			v = mw.text.trim(v)
		end
		if v ~= '' then
			args[k] = v
		end
	end
	local nocaturl = args.nocaturl

	local erreurs = '' -- les messages d'erreurs sont ajoutés ici

	local erreursarguments = Verif.verif(args, {'titre', 'title', 1, 2, 'langue', 'lang', 'url', 'wikidata', 'entity', 'id', 'nocaturl'}, 'Modèle Site officiel avec un paramètre inconnu‎')
	if erreursarguments then
		erreurs = erreurs .. erreursarguments
	end

	local titredefaut = 'Site officiel'
	local titre = args['titre'] or args['title'] or titredefaut
	local langue = args['langue'] or args['lang']
	local url = args['url'] or args[2] or args[1]

	if (not langue) and (args[2]) then
		langue = args[1]
	end
	local id = args.id or args.entity or args.wikidata
	if url and url:sub(1, 1) == 'Q' then
		id = url
		url = nil
	end
	
	-- si pas d'URL, essayer Wikidata, et si ça marche, sortir de ce module
	if not url and (id ~= '-') then
		local Wikidata = require "Module:Wikidata"
		local isinlang = defaultLang
		local links = Wikidata.getClaims{entity = id, property = 'P856', excludespecial = true, isinlang = isinlang}
		if not links then
			links = Wikidata.getClaims{entity = id, property = 'P856', excludespecial = true}
			isinlang = null
		end
		local Weblink = require 'Module:Weblink'
		local statementformat = function(statement, args)
			if statement.mainsnak.snaktype == 'value' then
				local value = statement.mainsnak.datavalue.value
				local title = Wikidata.getFormattedQualifiers(statement, {'P1476'}, {isinlang = 'fr'})
				if not title then
					title = Wikidata.getFormattedQualifiers(statement, {'P1476'})
				end
				if not title then
					title = args.text
				end
				return Weblink.makelink(value, title)
			else
				return Wikidata.formatSnak( statement.mainsnak, args )
			end
		end
		if links and (#links > 1) then
			return '<cite class="ouvrage" id="site_officiel" style="font-style: normal;">' .. "Sites officiels : " .. Wikidata.formatAndCat{claims = links, property = 'P856', displayformat = 'weblink', showlang = true, excludespecial = true, isinlang = isinlang, statementformat = statementformat} .. '</cite>' .. erreurs
		elseif links then
			if not (titre == titredefaut) then
				statementformat = nil
			end
			return '<cite class="ouvrage" id="site_officiel" style="font-style: normal;">' .. Wikidata.formatAndCat{claims = links, property = 'P856', displayformat = 'weblink', showlang = true, text = titre, isinlang = isinlang, statementformat = statementformat} .. '</cite>' .. erreurs
		end
	end
	
	-- si toujours pas, ajouter les catégories d'erreur et sortir
	if not url then
		if not nocaturl then
			erreurs = erreurs .. caterreur('url')
		end
		return erreurs
	end

	-- nettoyage de l'URL
	url = url:gsub( '%[', '%%5B' ):gsub( '%]', '%%5D' )
	if not ( url:match( '^http' ) or url:match( '^//' ) ) then
			url = 'http://' .. url
	end
	
	-- formatage de la langue
	local indicLangue = ''  -- langue telle qu'affichée dans le rendu final
	if langue then
		if langue ~= 'fr' then
			local Langue = require "Module:Langue"
			indicLangue = Langue.indicationMultilingue{ langue } .. ' '
			if indicLangue:find('class="error"', nil, true) then
				erreurs = erreurs .. caterreur('langue')
			end
		end
	end

	return '<cite class="ouvrage" id="site_officiel" style="font-style: normal;">' .. indicLangue .. '[' .. url .. ' ' .. titre .. ']' .. '</cite>' .. erreurs
end

return Lien