--[[
* Modulo che implementa il template Protetta.
]]--
require('Modulo:No globals')
local getArgs = require('Modulo:Arguments').getArgs
local cfg = mw.loadData('Modulo:Protezione/Configurazione')
local p = {}
-- Restituisce la protezione della pagina per l'action richiesta o nil se non protetta
local function getProtection(action, title)
return title.protectionLevels[action] and title.protectionLevels[action][1]
end
-- Aggiunge l'icona per l'azione e la protezione specificate
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 l'azione, la protezione e la pagina specificate
local function getMsg(action, prot, title)
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 l'azione, la protezione e la pagina specificate
local function getCategory(action, prot, title)
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
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
-- Per l'utilizzo da altro modulo
function p._main(args)
local title, editProt, moveProt, editCat, moveCat, msg, ret
title = mw.title.getCurrentTitle()
editProt = getProtection('edit', title)
moveProt = getProtection('move', title)
-- se è rimasta solo moveProt=autoconfirmed è da considerarsi scaduta
if not editProt and moveProt == 'autoconfirmed' then
moveProt = nil
end
-- protezione per la modifica
if editProt then
addIcon('edit', editProt)
msg = getMsg('edit', editProt, title)
-- il parametro "cat" permette di specificare una categoria arbitraria
if args.cat then
editCat = getUserCategory(editProt, args)
else
editCat = getCategory('edit', editProt, title)
end
end
-- protezione per lo spostamento (l'icona e la categoria sono aggiunte solo se moveProt=sysop)
if moveProt == 'sysop' then
addIcon('move', moveProt)
-- la categoria per lo spostamento non è aggiunta se editProt=sysop
if editProt ~= 'sysop' then
moveCat = getCategory('move', moveProt, title)
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
-- Entry-point per il template {{Protetta}}
function p.main(frame)
return p._main(getArgs(frame, { parentOnly = true }))
end
return p