Modulo:Protezione

Da Semi del Verbo, l'enciclopedia dell'influenza del Vangelo sulla cultura

La documentazione per questo modulo può essere creata in Modulo:Protezione/man

--[[
* Modulo che implementa il template Protetta.
]]--

require('strict')

local getArgs = require('Modulo:Arguments').getArgs
local cfg = mw.loadData('Modulo:Protezione/Configurazione')

-- Restituisce la protezione della pagina per l'azione richiesta o nil se non protetta.
--
-- @param {table} title
-- @param {string} action
-- @return {string}
local function getProtection(title, action)
	return title.protectionLevels[action] and title.protectionLevels[action][1]
end

-- Aggiunge l'icona per l'azione e la protezione specificate.
--
-- @param {string} action
-- @param {string} prot
local function addIcon(action, prot)
	-- l'underscore di move serve per cambiare l'ordine di visualizzazione delle icone
	local icon = string.format('<indicator name="prot%s">%s</indicator>',
							   action == 'move' and '_move' or action, cfg.icone[action][prot])
	mw.getCurrentFrame():preprocess(icon)
end

-- Restituisce il messaggio configurato per il tipo di azione e protezione sulla pagina specificata.
--
-- @param {table} title
-- @param {string} action
-- @param {string} prot
-- @return {string}
local function getMsg(title, action, prot)
	local msg = cfg.messaggi[action][prot][title.namespace]
	return msg and msg:gsub('$1', string.format('[[%s|pagina di discussione]]', title.talkPageTitle.fullText)) or nil
end

-- Restituisce la categoria configurata per il tipo di azione e protezione sulla pagina specificata.
--
-- @param {table} title
-- @param {string} action
-- @param {string} prot
-- @return {string}
local function getCategory(title, action, prot)
	local categories = cfg.categorie[action]
	local cat = categories[title.namespace] or categories.default
	if prot == 'autoconfirmed' then
		cat = cat .. ' parzialmente'
	end
	return string.format('[[Categoria:%s]]', cat)
end

-- Restituisce la categoria arbitraria scelta dall'utente.
--
-- @param {string} editProt
-- @param {table} args
-- @return {string}
local function getUserCategory(editProt, args)
	local cat
	if editProt == 'sysop' then
		cat = args.cat .. ' ' .. (args.generecat == 'm' and 'protetti' or 'protette')
	elseif editProt == 'autoconfirmed' then
		cat = args.cat .. ' ' .. (args.generecat == 'm' and 'protetti parzialmente' or 'protette parzialmente')
	end
	return cat and string.format('[[Categoria:%s]]', cat) or nil 
end

-- =============================================================================
--                            Funzioni esportate
-- =============================================================================

local p = {}

-- Funzione per l'utilizzo da un altro modulo.
function p._main(args)
	local title, editProt, moveProt, editCat, moveCat, msg, ret

	title = mw.title.getCurrentTitle()
	editProt = getProtection(title, 'edit')
	moveProt = getProtection(title, 'move')
	-- moveProt=autoconfirmed è già il default in itwiki
	if moveProt == 'autoconfirmed' then
		moveProt = nil
	end

	-- protezione per la modifica
	if editProt then
		addIcon('edit', editProt)
		msg = getMsg(title, 'edit', editProt)
		-- il parametro "cat" permette di specificare una categoria arbitraria
		if args.cat then
			editCat = getUserCategory(editProt, args)
		else
			editCat = getCategory(title, 'edit', editProt)
		end
	end

	-- protezione per lo spostamento
	if moveProt then
		addIcon('move', moveProt)
		-- la categoria per lo spostamento non è aggiunta se editProt=sysop
		if editProt ~= 'sysop' then
			moveCat = getCategory(title, 'move', moveProt)
		end
	end

	if editProt or moveProt then
		ret = (msg or '') .. (editCat or '') .. (moveCat or '')
	else
		-- la pagina non è protetta
		if title.namespace == 10 and title.isSubpage and title.subpageText:match('^[Ss]andbox$') then
			ret = '[[Categoria:Sandbox dei template]]'
		else
			ret = string.format('[[Categoria:%s]]', cfg.catSprotette)
		end
	end

	return ret
end

-- Funzione per il template {{Protetta}}.
function p.main(frame)
	return p._main(getArgs(frame, { parentOnly = true }))
end

return p