« Module:Lien interwiki » : différence entre les versions
Apparence
wp>Orlodrim Déplacement du <span class="ExistingLink"></span> autour du lien affiché plutôt que le mettre sur un contenu invisible après le lien. Ça avait été ajouté par Dr Brains. Je ne suis pas sûr de ce qu'il comptait en faire mais je n'ai pas trouvé de script mentionnant cette classe, donc rien à mettre à jour a priori. Par contre, ça va m'être utile pour en créer un. |
m 1 version importée |
(Aucune différence)
| |
Dernière version du 26 juin 2025 à 13:20
La documentation pour ce module peut être créée à Module:Lien interwiki/doc
-- luacheck: globals mw, no max line length
local mwTrim = mw.text.trim
local p = {}
function p.main( frame )
local function lien( interwiki, target, text )
if target:sub( 1, 1 ) == ':' then
target = target:sub( 2 )
end
if interwiki then
target = interwiki .. ':' .. target
end
if text then
return '[[:' .. target .. '|' .. text .. ']]'
else
return '[[:' .. target .. ']]'
end
end
local function indicateurAvecLien( lang, cible_trad )
local function makeSpan( libelle )
local title = 'Article ' .. libelle .. '\194\160: «\194\160' .. cible_trad .. '\194\160»'
return '<span class="indicateur-langue" title="' .. title:gsub( '"', '"' ) .. '">(' .. lang .. ')</span>'
end
if lang == 'wikidata' or lang == 'd' then
return lien( lang, cible_trad, makeSpan( 'sur Wikidata' ) )
else
-- noms pour les langues les plus utilisées, afin d'économiser l'appel au Module:Langue
local langs = {
de = 'allemand',
en = 'anglais',
es = 'espagnol',
it = 'italien',
ja = 'japonais',
pl = 'polonais',
pt = 'portugais',
ru = 'russe',
zh = 'chinois',
}
local nomLangue = langs[ lang ]
if not nomLangue then
nomLangue = require( 'Module:Langue' ).nomLangue( lang )
if nomLangue:find( 'class="error"', nil, true ) then
nomLangue = nil
end
end
if nomLangue then
return lien( lang, cible_trad, makeSpan( 'en ' .. nomLangue ) )
else
return lien( lang, cible_trad, makeSpan( 'dans une autre langue' ) )
.. '<span class="error">Erreur : langue non reconnue par le modèle Lien : « ' .. lang .. ' ».</span>'
.. '[[Catégorie:Article contenant un appel à traduction avec un code langue inconnu|+]]'
end
end
end
local validArgs = {
fr = true,
[1] = true,
lang = true,
langue = true,
trad = true,
texte = true,
lienfr = true,
nocat = true,
}
local parentArgs = frame:getParent().args
local hasInvalidArgs = false
local args = {}
for k, v in pairs( parentArgs ) do
-- détecte la présence de paramètres non reconnus
if not validArgs[ k ] then
hasInvalidArgs = true
end
-- effectue un trim sur les paramètres non nommés,
-- et exclut les paramètres dont la valeur est vide
if type( k ) == 'number' then
v = mwTrim( v )
end
if v ~= '' then
args[ k ] = v
end
end
local cible_fr = args[ 'fr' ] or args[ 1 ] or args[ 'trad' ]
local cible_trad = args[ 'trad' ] or args[ 'fr' ] or args[ 1 ]
local lang = args[ 'langue' ] or args[ 'lang' ] or 'en'
local nolinkfr = args['lienfr'] == 'non' or args['lienfr'] == 'no'
local categorise = ( args[ 'nocat' ] ~= 'oui' )
if not cible_fr then
return frame:expandTemplate{ title = 'Fix', args = {
[1] = '[[' .. ( args[ 'texte' ] and '|' .. args[ 'texte' ] or '' ) .. ']]',
['message'] = 'modèle à corriger',
['message_lien'] = 'Catégorie:Page contenant un appel à traduction d\'un article non spécifié',
['infobulle'] = 'Cet appel à traduction est à corriger car aucun titre de page n\'est spécifié.',
['catégorie'] = ( categorise and 'Page contenant un appel à traduction d\'un article non spécifié' or nil )
} }
end
local result
if nolinkfr then
result = ( args[ 'texte' ] or cible_fr ) .. '\194\160' .. indicateurAvecLien( lang, cible_trad )
else
result = lien( nil, cible_fr, args[ 'texte' ] )
local pageExiste
local objetPage = mw.title.new( cible_fr )
if objetPage then
-- pcall permettant de masquer les erreurs « too many expensive function calls »
local success = pcall( function ()
pageExiste = objetPage.exists
end )
if not success then
pageExiste = false
end
else
pageExiste = false -- titre de page invalide
end
if pageExiste then
result = '<span class="ExistingLink">' .. result .. '</span>'
if categorise then
result = result .. p._temoin()
end
else
result = result .. '\194\160' .. indicateurAvecLien( lang, cible_trad )
if categorise then
result = result .. p._categorisation( lang )
end
end
end
if hasInvalidArgs then
local ns = mw.title.getCurrentTitle().namespace
-- Utilisateur, Discussion utilisateur
if ns == 2 or ns == 3 then
result = result .. '[[Catégorie:Page utilisateur contenant un modèle Lien avec un paramètre inconnu]]'
else
result = result .. '[[Catégorie:Page contenant un modèle Lien avec un paramètre inconnu]]'
end
end
return result
end
function p._temoin()
local categorie
local ns = mw.title.getCurrentTitle().namespace
-- (principal), Portail, Référence
if ns == 0 or ns == 100 or ns == 104 then
categorie = '[[Catégorie:Page utilisant Lien pour un article existant]]'
-- Modèle
elseif ns == 10 then
categorie = '[[Catégorie:Page utilisant Lien pour un article existant|*]]'
-- Discussion, Wikipédia, Projet
elseif ns == 1 or ns == 4 or ns == 102 then
categorie = '[[Catégorie:Page de discussion utilisant Lien pour un article existant]]'
else
categorie = ''
end
return categorie
end
function p._categorisation( lang )
local ns = mw.title.getCurrentTitle().namespace
-- (principal), Modèle
if ns == 0 or ns == 10 then
local data = mw.loadData( 'Module:Lien interwiki/data catégorisation' )
return ( data[ lang ] or '[[Catégorie:Article contenant un appel à traduction avec un code langue inconnu|' .. lang .. ']]' )
else
return ''
end
end
return p