« Module:Lien interwiki » : différence entre les versions

    De WikiPasokh
    (ajout d'un alias « wikidata », quand même plus descriptif qu'un simple « d » (modif associée dans Module:Lien interwiki/data catégorisation : 212203229) ; à propos au sujet de 212203032, il n'y avait pas d'item « c » dans la data catégorisation, du coup ça aurait catégorisé inopinément)
     
    m (1 version importée)
     
    (Aucune différence)

    Dernière version du 1 juin 2024 à 11:44

    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( '"', '&quot;' ) .. '">(' .. 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]              = '&lsqb;&lsqb;' .. ( args[ 'texte' ] and '&verbar;' .. args[ 'texte' ] or '' ) .. '&rsqb;&rsqb;',
    			['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
    			if categorise then
    				result = result .. p._temoin( cible_fr )
    			end
    		else
    			result = result .. '\194\160' .. indicateurAvecLien( lang, cible_trad )
    
    			if categorise then
    				result = result .. p._categorisation( lang )
    			end
    		end
    	end
    
    	if hasInvalidArgs then
    		result = result .. '[[Catégorie:Modèle Lien avec un paramètre inconnu]]'
    	end
    
    	return result
    end
    
    function p._temoin( cible_fr )
    	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 .. '<span class="ExistingLink" style="display:none">' .. cible_fr .. '</span>'
    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