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

Contenuto cancellato Contenuto aggiunto
fix
riscrivo
Riga 8:
local cfg = mw.loadJsonData('Modulo:Criteri cancellazione immediata/Configurazione/sandbox.json')
local getArgs = require('Module:Arguments').getArgs
 
-- =============================================================================
-- enum NamespaceType
-- =============================================================================
local NamespaceType = setmetatable({
ANY = {
value = 'any'
},
EVEN = {
value = 'ns pari'
},
ODD = {
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
})
 
-- =============================================================================
-- enum Namespace
-- =============================================================================
 
local Namespace = setmetatable({
DEFAULT = {
id = 'default',
type = NamespaceType.ANY,
name = 'default'
},
PRINCIPALE = {
id = 0,
type = NamespaceType.EVEN,
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
Namespace[ns.name:gsub(' ', '_'):upper()] = {
id = ns.id,
type = ns.id % 2 == 0 and NamespaceType.EVEN or NamespaceType.ODD,
name = ns.name
}
end
end
 
-- =============================================================================
-- enum ListType
-- =============================================================================
 
local ListType = setmetatable({
DROPDOWN_LIST = {
arg = 'a discesa',
prefix = '**',
section = setmetatable({
[6] = '* Motivi comuni di cancellazione'
}, {
__index = function (t, key)
return '* Cancellazione immediata'
end
})
},
BULLETED_LIST = {
arg = 'puntato',
prefix = '*',
section = setmetatable({}, {
__index = function (t, key)
return ''
end
})
}
}, {
__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
})
 
-- =============================================================================
-- enum TextType
-- =============================================================================
 
local TextType = setmetatable({
SYSTEM_MESSAGE = {
arg = 'messaggio di sistema',
get = 'getSystemMessage',
set = 'setSystemMessage'
},
DEFINITION = {
arg = 'definizione',
get = 'getDefinition',
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
})
 
-- =============================================================================
Riga 16 ⟶ 158:
 
-- Costruttore della classe Criterion
function Criterion:new(abbreviationcode)
local self = {}
setmetatable(self, { __index = Criterion })
 
self.abbreviationcode = abbreviationcode
self.order = { default = 0 }
self.link = { default = string.format('[[WP:IMMEDIATA|%s]]', abbreviationcode) }
self.text = { singolaresystem_message = { default = '' }, plurale = { default = '' } }
self.definition = { default = '' }
self.validity = { default = true }
 
Riga 29 ⟶ 172:
end
 
function Criterion:getAbbreviationgetCode()
return self.abbreviationcode
end
 
function Criterion:getLink(ns)
return self.link[ns.id] or self.link.default
end
 
function Criterion:getOrder(ns)
return self.order[ns.id] or self.order.default
end
 
function Criterion:getTextgetSystemMessage(ns, grammatical_number, add_link)
local textsystem_message = self.text[grammatical_number]system_message[ns.id] or self.text[grammatical_number]system_message.default
local pre = add_link and self:getLink(ns) or self:getCode()
 
return string.format('(%s) %s', pre, system_message)
if add_link then
return string.format('(%s) %s', self:getLink(ns), text)
else
return string.format('(%s) %s', self:getAbbreviation(), text)
end
end
 
function Criterion:isValidgetDefinition(ns, add_link)
local definition = self.definition[ns.id] or self.definition.default
local ns_type
local pre = add_link and self:getLink(ns) or self:getCode()
 
return string.format('(%s) %s', pre, definition)
if mw.site.namespaces[ns] then
end
ns_type = mw.site.namespaces[ns].id % 2 == 0 and 'ns pari' or 'ns dispari'
end
 
function Criterion:isValid(ns)
if self.validity[ns] ~= nil then
return if self.validity[ns.id] ~= nil then
elseif return self.validity[ns_typens.id] ~= nil then
return elseif self.validity[ns_typens.type.value] ~= nil then
return self.validity[ns.type.value]
else
return self.validity.default
Riga 68 ⟶ 209:
 
function Criterion:setLink(link, ns)
self.link[ns.id] = link
end
 
function Criterion:setOrder(order, ns)
self.order[ns.id] = order
end
 
function Criterion:setTextsetSystemMessage(text, ns, grammatical_number)
self.text[grammatical_number]system_message[ns.id] = text
end
 
function Criterion:setValidsetDefinition(validtext, ns)
self.validitydefinition[ns.id] = validtext
end
 
function Criterion:setValidForNs(valid, ns)
self.validity[ns.id] = valid
end
 
function Criterion:setValidForNsType(valid, ns_type)
self.validity[ns_type.value] = valid
end
 
Riga 86 ⟶ 235:
-- Funzioni di utilità
-- =============================================================================
 
local function to_namespace_id(ns)
if ns == 'default' then
return ns
elseif ns == 'Principale' then
return mw.site.namespaces[0].id
elseif tonumber(ns) and mw.site.namespaces[tonumber(ns)] then
return tonumber(ns)
elseif ns ~= nil and ns ~= '' and mw.site.namespaces[ns] then
return mw.site.namespaces[ns].id
end
end
 
local function to_criterion(key, value)
Riga 103 ⟶ 240:
 
for ns, props in pairs(value) do
ns = to_namespace_idNamespace.findByName(ns)
 
if ns and type(props) == 'table' then
Riga 114 ⟶ 251:
end
 
if type(props.testotesti) == 'table' then
iffor typek, v in pairs(props.testo.singolaretesti) == 'string' thendo
local text_type = TextType.findByArg(k)
criterion:setText(props.testo.singolare, ns, 'singolare')
end
 
if type(props.testo.plurale) == 'string'text_type then
criterion:setText(props[text_type.testo.pluraleset](criterion, nsv, 'plurale'ns)
end
elseif type(props.testi) == 'table' then
if type(props.testi['messaggio di sistema']) == 'string' then
criterion:setText(props.testi['messaggio di sistema'], ns, 'singolare')
end
 
if type(props.testi.definizione) == 'string' then
criterion:setText(props.testi.definizione, ns, 'plurale')
end
end
 
if ns == 'default'Namespace.DEFAULT then
if type(props['validità']) == 'table' then
iffor typek, v in pairs(props['validità']['ns pari']) == 'boolean' thendo
local ns_type = NamespaceType.findByValue(k)
criterion:setValid(props['validità']['ns pari'], 'ns pari')
end
 
if ns_type and type(props['validità']['ns dispari']v) == 'boolean' then
criterion:setValidsetValidForNsType(props['validità']['ns dispari']v, 'ns dispari'ns_type)
end
end
end
elseif type(props['validità']) == 'boolean' then
criterion:setValidsetValidForNs(props['validità'], ns)
end
end
Riga 151 ⟶ 280:
end
 
local function get_list_prefixget_suppressed_criteria(list_typearg)
local list_prefixessuppressed_criteria = setmetatable({}, {
__index = function (t, key)
['elenco a discesa'] = '**',
return false
['elenco puntato'] = '*'
}
 
return list_prefixes['elenco ' .. list_type]
end
 
local function get_criterion(args)
for key, value in pairs(cfg) do
local criterion = to_criterion(key, value)
 
if criterion:getAbbreviation() == args.criterio and criterion:isValid(args.ns) then
return criterion:getText(args.ns, args['tipo testo'], args.link)
end
end})
end
 
if arg then
local function list_criteria(args)
for criterion_code in string.gmatch(arg, '[%s,]*([^,]*[^%s,])') do
local list = ''
suppressed_criteria[criterion_code] = true
local valid_criteria = {}
 
for key, value in pairs(cfg) do
local criterion = to_criterion(key, value)
 
if criterion:isValid(args.ns) then
local is_suppressed = false
 
for _, abbreviation in ipairs(args['escludi criteri']) do
if abbreviation == criterion:getAbbreviation() then
is_suppressed = true
break
end
end
 
if is_suppressed == false then
table.insert(valid_criteria, criterion)
end
end
end
 
return suppressed_criteria
local comp = function (a, b)
if a:getOrder(args.ns) == b:getOrder(args.ns) then
return a:getAbbreviation() < b:getAbbreviation()
else
return a:getOrder(args.ns) < b:getOrder(args.ns)
end
end
 
table.sort(valid_criteria, comp)
 
for _, criterion in ipairs(valid_criteria) do
list = list .. '\n' .. get_list_prefix(args['tipo elenco']) .. ' '
 
if args.ancore then
list = list .. string.format('<span id=%s></span>', criterion:getAbbreviation())
end
 
list = list .. criterion:getText(args.ns, args['tipo testo'], args.link)
end
 
if args['tipo elenco'] == 'a discesa' then
if args.ns == 6 then
list = '* Motivi comuni di cancellazione' .. list
else
list = '* Cancellazione immediata' .. list
end
end
 
return mw.text.trim(list)
end
 
Riga 231 ⟶ 303:
end
 
args.ns = to_namespace_idNamespace.findByName(args.ns) or 'default'Namespace.findById(args.ns) or Namespace.DEFAULT
args['tipo elenco'] = ListType.findByArg(args['tipo elenco']) or ListType.DROPDOWN_LIST
 
if args['tipo elencotesto'] == nil or get_list_prefixTextType.findByArg(args['tipo elencotesto']) == nilor thenTextType.SYSTEM_MESSAGE
args['tipo elenco'] = 'a discesa'
end
 
if args['tipo testo'] ~= 'singolare' and args['tipo testo'] ~= 'plurale' then
args['tipo testo'] = 'singolare'
end
 
args.link = args.link ~= 'no' and true or false
args.ancore = (args.ancore == 'sì' or args.ancore == 'si') and true or false
args['escludi criteri'] = mw.text.splitget_suppressed_criteria(args['escludi criteri'] or '', '%s*,%s*')
 
return args
Riga 256 ⟶ 321:
function p.get_criterion(frame)
local args = get_args(frame)
local text_type = args['tipo testo']
local ns, criterion_code, add_link = args.ns, args.criterio, args.link
 
if args.criteriocriterion_code and== nil or args.ns == nil then
return get_criterion(args)
end
 
for key, value in pairs(cfg) do
local criterion = to_criterion(key, value)
 
if criterion:getCode() == criterion_code and criterion:isValid(ns) then
return criterion[text_type.get](criterion, ns, add_link)
end
end
end
Riga 264 ⟶ 339:
function p.list_criteria(frame)
local args = get_args(frame)
local list_type, text_type = args['tipo elenco'], args['tipo testo']
local ns, add_anchor, add_link = args.ns, args.ancore, args.link
local suppressed_criteria = args['escludi criteri']
 
if args.ns == nil then
return list_criteria(args)
end
 
local list = list_type.section[ns.id]
local valid_criteria = {}
 
for key, value in pairs(cfg) do
local criterion = to_criterion(key, value)
 
if criterion:isValid(ns) and suppressed_criteria[criterion:getCode()] == false then
table.insert(valid_criteria, criterion)
end
end
 
local comp = function (a, b)
if a:getOrder(ns) == b:getOrder(ns) then
return a:getCode() < b:getCode()
else
return a:getOrder(ns) < b:getOrder(ns)
end
end
 
table.sort(valid_criteria, comp)
 
for _, criterion in ipairs(valid_criteria) do
list = list .. '\n' .. list_type.prefix .. ' '
 
if add_anchor then
list = list .. string.format('<span id=%s></span>', criterion:getCode())
end
 
list = list .. criterion[text_type.get](criterion, ns, add_link)
end
 
return mw.text.trim(list)
end