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

Contenuto cancellato Contenuto aggiunto
aggiungo entrypoint per ottenere l'elenco dei list types e text types
usa il modulo:Enum per le enumerazioni
 
(5 versioni intermedie di uno stesso utente non sono mostrate)
Riga 6:
require('strict')
 
local-- cfgLegge = mw.loadJsonData('[[Modulo:Criteri cancellazione immediata/Configurazione/sandbox.json')]]
-- oppure [[Modulo:Criteri cancellazione immediata/sandbox/Configurazione.json]]
local getArgs = require('Module:Arguments').getArgs
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 22 ⟶ 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 40 ⟶ 34:
-- =============================================================================
 
local Namespace = setmetatableEnum:new('ns', {
DEFAULT = {
id = 'default',
Riga 51 ⟶ 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 87 ⟶ 69:
-- =============================================================================
 
local ListType = setmetatableEnum:new('tipo elenco', {
DROPDOWN_LIST = {
arg = 'a discesa',
Riga 108 ⟶ 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
end
end
})
 
function ListType:findByArg(arg)
return self:findBy('arg', arg)
end
 
function ListType:getDefault()
return self.DROPDOWN_LIST
end
 
-- =============================================================================
Riga 126 ⟶ 104:
-- =============================================================================
 
local TextType = setmetatableEnum:new('tipo testo', {
SYSTEM_MESSAGE = {
arg = 'messaggio di sistema',
Riga 137 ⟶ 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
end
end
})
 
function TextType:findByArg(arg)
return self:findBy('arg', arg)
end
 
function TextType:getDefault()
return self.SYSTEM_MESSAGE
end
 
-- =============================================================================
Riga 256 ⟶ 230:
 
for ns, props in pairs(criterion_cfg) do
ns = Namespace.:findByName(ns)
 
if ns and type(props) == 'table' then
Riga 271 ⟶ 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 282 ⟶ 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 323 ⟶ 297:
 
local function get_args(frame)
local args = require('Module:Arguments').getArgs(frame, { parentOnly = true })
 
if args[1] and args.criterio == nil then
Riga 329 ⟶ 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.DROPDOWN_LIST:getDefault()
args['tipo testo'] = TextType.:findByArg(args['tipo testo']) or TextType.SYSTEM_MESSAGE:getDefault()
args.link = args.link ~= 'no' and true or false
args.ancore = (args.ancore == 'sì' or args.ancore == 'si') and true or false
Riga 345 ⟶ 319:
local p = {}
 
function p.get_list_typesget_default_value(frame)
local param = mw.text.trim(frame.args[1] or '')
local list_types = {}
 
if Enum[param] and Enum[param].getDefault and Enum[param]:getDefault().arg then
for _, list_type in pairs(ListType) do
return string.format('"%s"', Enum[param]:getDefault().arg)
table.insert(list_types, 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, 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
 
Riga 370 ⟶ 347:
local ns, criterion_code, add_link = args.ns, args.criterio, args.link
 
if criterion_code == nil orand cfg[criterion_code] == nil then
local criterion = to_criterion(criterion_code, cfg[criterion_code])
return
end
 
criterion:setCurrentNs(ns):setAddLink(add_link)
local criterion = to_criterion(criterion_code, cfg[criterion_code])
 
if criterion:setCurrentNsisValid(ns):setAddLink(add_link) then
return criterion[text_type.get](criterion)
 
end
if criterion:isValid() then
return criterion[text_type.get](criterion)
end
end
Riga 404 ⟶ 379:
 
for _, criterion in ipairs(valid_criteria) do
list = list string.. format('%s\n%s ', ..list, list_type.prefix .. ' ')
 
if add_anchor then
list = list .. string.format('%s<span id=%s></span>', list, criterion:getCode())
end