Module:Protection

De Vouiquipèdia, l’enciclopèdia abada.

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

-- Cél modulo emplèmente los modèlos {{Protèccion}}, {{Mié-protèccion ètendua}}, {{Mié-protèccion}}, {{Mié-protèccion longe}} et {{Nom protègiê}}.

local p = {}

-- Chargement du module de données.
local balyes = mw.loadData('Module:Protection/Balyês')

-- Définition des tables de données.
local tipos = balyes.tipos
local cfg = balyes.configuracion

--Redéfinitions utiles.
local format = mw.ustring.format
local insert = table.insert

local function protectionPage(tipoProteccion, formaProteccion, titroPage)
	-- Retourne vrai si la page courante est protégée et correspond
	-- au type de protection.
	local accion = tipos[tipoProteccion].accion or 'edit'
	local nivel = titroPage.protectionLevels[accion] and titroPage.protectionLevels[accion][1]
	return formaProteccion.nivel == nivel
end

local function estSotPage(titroPage)
	-- Teste si la page actuelle est une sous-page /Documentation,
	-- /Bac à sable ou /Test de modèle ou de module. Retourne vrai
	-- si c'est le cas et faux sinon.
	local espacoNom = titroPage.nsText == 'Modèlo' or titroPage.nsText == 'Module'
	local sotPage = titroPage.isSubpage and 
		(titroPage.subpageText == 'Documentacion'
		or titroPage.subpageText == 'Bouèta de sabla'
		or titroPage.subpageText == 'Èprôva')
 
	return espacoNom and sotPage
end
 
local function fareIconaTitro(formaProteccion)
	local iconaTitro = require('Module:Icôna de titro')._main
	local argsIcona = {
		emage  = formaProteccion.emage,
		lim    = formaProteccion.lim,
		texto  = formaProteccion.texto,
		talye  = 15,
		id     = formaProteccion.id or 'proteccion-edicion'
	}
 
	return iconaTitro(argsIcona)
end

local function entrebetaCategorie(tipoProteccion, nsPage)
	local nomCategorie = tipoProteccion.categorie[nsPage]
	local defotCategorie = tipoProteccion.categorie['dèfôt']
	local limCategorie = '[[Catègorie:%s]]'
 
	return format(limCategorie, nomCategorie or defotCategorie)
end

local function bendaProteccion(titroPage, args)
	local nsPage = args.nsdoc or titroPage.nsText
	local titro = cfg[nsPage] or cfg['titroDèfôt']
	local texto = mw.html.create('span')
	local pageDiscussion, limAdmin, limJornal
 
	-- Gestion du nombre d'inclusions pour les modèles/modules.
	if nsPage == 'Modèlo' or nsPage == 'Module' then
		titro = format(titro, titroPage:partialUrl())
	end
 
	-- Gestion du paramètre 'texte' et de ses liens internes
	pageDiscussion = tostring(titroPage.talkPageTitle)
	limAdmin = mw.title.new(cfg['limAdmin'])
		:fullUrl(format(cfg['chouèxAdmin'],
			mw.uri.encode(titroPage.fullText, 'WIKI')))
	if nsPage == '' then limAdmin = limAdmin .. cfg['chouèx2Admin'] end
	limJornal = mw.title.new(cfg['limJornâl'])
		:fullUrl(format(cfg['chouèxJornâl'],
			mw.uri.encode(titroPage.fullText, 'WIKI')))
 	
	texto
		:addClass('plainlinks')
		:wikitext(format(cfg['tèxtoBenda'], 
			pageDiscussion, pageDiscussion, limAdmin, limJornal))
 
	return require('Module:Benda')._bendaSemonce{
		nivel     = 'enformacion',
		['icôna'] = tipos['protèccion'].emage,
		titro     = args[1] or titro,
		texto     = tostring(texto)
	}
end

function p.main(args, tipoProteccion, titroPage)
	tipoProteccion = tipoProteccion or 'proteccion'
	local formaProteccion = tipos[tipoProteccion]
	local titroPage = titroPage or mw.title.getCurrentTitle()
	local res = {}
 
	if tipoProteccion == 'proteccion' and args.nocat then
		return bendaProteccion(titroPage, args)
	end
 
	if protectionPage(tipoProteccion, formaProteccion, titroPage) then
		-- Création de l'icône de titre selon le mode de protection.
		insert(res, fareIconaTitro(formaProteccion))
		-- Création du bandeau de protection pour le mode 'protection'.
		if tipoProteccion == 'proteccion' then
			insert(res, bendaProteccion(titroPage, args))
		end
		-- Catégorisation selon le mode de protection.
		insert(res, entrebetaCategorie(formaProteccion, titroPage.nsText))
	elseif not estSotPage(titroPage) then
		-- Catégorisation lorsque le niveau de protection en écriture ne
		-- correspond pas ou plus. Note : les sous-pages '/Documentation',
		-- '/Bac à sable' ou '/Test' appartenant aux modèles ou aux modules
		-- ne sont pas – et ne doivent pas être – concernées.
		insert(res, '[[Catègorie:Pâge que la protèccion est a controlar]]')
	end
 
	return table.concat(res)
end

local function adaptior(nomFonccion)
	return function (frame)
		local args = {}
		local argsParent = frame:getParent().args
 
		-- Paramètres vides interprétés par Lua.
		for cle, val in pairs(argsParent) do
			if val ~= '' or cle == 'nsdoc' then
				args[cle] = mw.text.trim(val)
			end
		end
 
		return p.main(args, nomFonccion)
	end
end
 
-- Insertion dans la table p des fonctions appelées par les
-- modèles à l'aide d'un adaptateur de fonction.
local nomsFonccion = {'protèccion', 'miéProtèccionÈtendua', 'miéProtèccion', 'miéProtèccionLonge', 'nomProtègiê'} 
for _, nomFonccion in ipairs(nomsFonccion) do
	p[nomFonccion] = adaptior(nomFonccion)
end

return p