Modulo:Criteri cancellazione immediata/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m +wl
usa il modulo:Enum per le enumerazioni
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 9:
-- oppure [[Modulo:Criteri cancellazione immediata/sandbox/Configurazione.json]]
local cfg = mw.loadJsonData(mw.getCurrentFrame():getTitle() .. '/Configurazione.json')
local Enum = require('Modulo:Enum')
 
-- =============================================================================
-- enum NamespaceType
-- =============================================================================
local NamespaceType = setmetatableEnum:new('tipo ns', {
ANY = {
value = 'any'
Riga 23 ⟶ 24:
value = 'ns dispari'
}
}, {
__index = function (t, key)
if key == 'findByValue' then
return function (value)
for _, ns_type in pairs(t) do
if ns_type.value == value then
return ns_type
end
end
end
end
end
})
 
function NamespaceType:findByValue(value)
return self:findBy('value', value)
end
 
-- =============================================================================
Riga 41 ⟶ 34:
-- =============================================================================
 
local Namespace = setmetatableEnum:new('ns', {
DEFAULT = {
id = 'default',
Riga 52 ⟶ 45:
name = 'Principale'
}
}, {
__index = function (t, key)
if key == 'findById' then
return function (id)
for _, ns in pairs(t) do
if ns.id == tonumber(id) then
return ns
end
end
end
elseif key == 'findByName' then
return function (name)
for _, ns in pairs(t) do
if ns.name == name then
return ns
end
end
end
end
end
})
 
for _, ns in pairs(mw.site.namespaces) do
if ns.id ~= 0 then
rawset(Namespace[, ns.name:gsub(' ', '_'):upper()] =, {
id = ns.id,
type = ns.id % 2 == 0 and NamespaceType.EVEN or NamespaceType.ODD,
name = ns.name
})
end
end
 
function Namespace:findById(id)
return self:findBy('id', tonumber(id))
end
 
function Namespace:findByName(name)
return self:findBy('name', name)
end
 
Riga 88 ⟶ 69:
-- =============================================================================
 
local ListType = setmetatableEnum:new('tipo elenco', {
DROPDOWN_LIST = {
arg = 'a discesa',
Riga 109 ⟶ 90:
})
}
}, {
__index = function (t, key)
if key == 'findByArg' then
return function (arg)
for _, list_type in pairs(t) do
if list_type.arg == arg then
return list_type
end
end
end
elseif key == 'getDefault' then
return function ()
return t.DROPDOWN_LIST
end
end
end
})
 
function ListType:findByArg(arg)
return self:findBy('arg', arg)
end
 
function ListType:getDefault()
return self.DROPDOWN_LIST
end
 
-- =============================================================================
Riga 131 ⟶ 104:
-- =============================================================================
 
local TextType = setmetatableEnum:new('tipo testo', {
SYSTEM_MESSAGE = {
arg = 'messaggio di sistema',
Riga 142 ⟶ 115:
set = 'setDefinition'
}
}, {
__index = function (t, key)
if key == 'findByArg' then
return function (arg)
for _, text_type in pairs(t) do
if text_type.arg == arg then
return text_type
end
end
end
elseif key == 'getDefault' then
return function ()
return t.SYSTEM_MESSAGE
end
end
end
})
 
function TextType:findByArg(arg)
return self:findBy('arg', arg)
end
 
function TextType:getDefault()
return self.SYSTEM_MESSAGE
end
 
-- =============================================================================
Riga 265 ⟶ 230:
 
for ns, props in pairs(criterion_cfg) do
ns = Namespace.:findByName(ns)
 
if ns and type(props) == 'table' then
Riga 280 ⟶ 245:
if type(props.testi) == 'table' then
for text_type, text in pairs(props.testi) do
text_type = TextType.:findByArg(text_type)
 
if text_type then
Riga 291 ⟶ 256:
if type(props['validità']) == 'table' then
for ns_type, valid in pairs(props['validità']) do
ns_type = NamespaceType.:findByValue(ns_type)
 
if ns_type and type(valid) == 'boolean' then
Riga 338 ⟶ 303:
end
 
args.ns = Namespace.:findByName(args.ns) or Namespace.:findById(args.ns) or Namespace.DEFAULT
args['tipo elenco'] = ListType.:findByArg(args['tipo elenco']) or ListType.:getDefault()
args['tipo testo'] = TextType.:findByArg(args['tipo testo']) or TextType.:getDefault()
args.link = args.link ~= 'no' and true or false
args.ancore = (args.ancore == 'sì' or args.ancore == 'si') and true or false
Riga 354 ⟶ 319:
local p = {}
 
function p.get_default_list_typeget_default_value(frame)
local param = mw.text.trim(frame.args[1] or '')
return ListType.getDefault()
end
 
if Enum[param] and Enum[param].getDefault and Enum[param]:getDefault().arg then
function p.get_default_text_type()
return TextTypestring.format('"%s"', Enum[param]:getDefault().arg)
end
 
function p.get_list_types()
local list_types = {}
 
for _, list_type in pairs(ListType) do
table.insert(list_types, string.format('"%s"', list_type.arg))
end
 
return table.concat(list_types, ', ')
end
 
function p.get_text_typeslist_possible_values(frame)
local param = mw.text.trim(frame.args[1] or '')
local text_types = {}
local possible_values = {}
 
if Enum[param] then
for _, text_type in pairs(TextType) do
for _, constant in pairs(Enum[param]) do
table.insert(text_types, string.format('"%s"', text_type.arg))
if constant.arg then
table.insert(possible_values, string.format('"%s"', constant.arg))
end
end
end
 
return table.concat(text_typespossible_values, ', ')
end