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

Contenuto cancellato Contenuto aggiunto
possibilità di escludere i criteri dagli elenchi
usa il modulo:Enum per le enumerazioni
 
(12 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 = Enum:new('tipo ns', {
ANY = {
value = 'any'
},
EVEN = {
value = 'ns pari'
},
ODD = {
value = 'ns dispari'
}
})
 
function NamespaceType:findByValue(value)
return self:findBy('value', value)
end
 
-- =============================================================================
-- enum Namespace
-- =============================================================================
 
local Namespace = Enum:new('ns', {
DEFAULT = {
id = 'default',
type = NamespaceType.ANY,
name = 'default'
},
PRINCIPALE = {
id = 0,
type = NamespaceType.EVEN,
name = 'Principale'
}
})
 
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
 
-- =============================================================================
-- enum ListType
-- =============================================================================
 
local ListType = Enum:new('tipo elenco', {
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
})
}
})
 
function ListType:findByArg(arg)
return self:findBy('arg', arg)
end
 
function ListType:getDefault()
return self.DROPDOWN_LIST
end
 
-- =============================================================================
-- enum TextType
-- =============================================================================
 
local TextType = Enum:new('tipo testo', {
SYSTEM_MESSAGE = {
arg = 'messaggio di sistema',
get = 'getSystemMessage',
set = 'setSystemMessage'
},
DEFINITION = {
arg = 'definizione',
get = 'getDefinition',
set = 'setDefinition'
}
})
 
function TextType:findByArg(arg)
return self:findBy('arg', arg)
end
 
function TextType:getDefault()
return self.SYSTEM_MESSAGE
end
 
-- =============================================================================
Riga 16 ⟶ 132:
 
-- Costruttore della classe Criterion
function Criterion:new(abbreviationcode)
local self = {}
setmetatable(self, { __index = Criterion })
 
self.abbreviationcode = abbreviationcode
self.current_ns = Namespace.DEFAULT
self.order = { default = 0 }
self.add_link = false
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 ⟶ 148:
end
 
function Criterion:getAbbreviationgetCode()
return self.abbreviationcode
end
 
function Criterion:getLinkgetOrder(ns)
return self.linkorder[nsself.current_ns.id] or self.linkorder.default
end
 
function Criterion:getOrdergetLink(ns)
return self.orderlink[nsself.current_ns.id] or self.orderlink.default
end
 
function Criterion:getTextgetSystemMessage(ns, grammatical_number, add_link)
return string.format('(%s) %s',
local text = self.text[grammatical_number][ns] or self.text[grammatical_number].default
self.add_link and self:getLink() or self:getCode(),
self.system_message[self.current_ns.id] or self.system_message.default)
end
 
function Criterion:getDefinition()
return string.format('(%s) %s',
self.add_link and self:getLink() or self:getCode(),
self.definition[self.current_ns.id] or self.definition.default)
end
 
function Criterion:isValid()
if add_link then
if self.validity[self.current_ns.id] ~= nil then
return string.format('(%s) %s', self:getLink(ns), text)
return self.validity[self.current_ns.id]
elseif self.validity[self.current_ns.type.value] ~= nil then
return self.validity[self.current_ns.type.value]
else
return string.format('(%s) %s', self:getAbbreviation(), text).validity.default
end
end
 
function Criterion:isValidsetCurrentNs(ns)
self.current_ns = ns
local ns_type
return self
end
 
function Criterion:setOrder(order)
if mw.site.namespaces[ns] then
self.order[self.current_ns.id] = order
ns_type = mw.site.namespaces[ns].id % 2 == 0 and 'ns pari' or 'ns dispari'
return self
end
end
 
function Criterion:setAddLink(add_link)
if self.validity[ns] ~= nil then
self.add_link = add_link
return self.validity[ns]
return self
elseif self.validity[ns_type] ~= nil then
return self.validity[ns_type]
else
return self.validity.default
end
end
 
function Criterion:setLink(link, ns)
self.link[nsself.current_ns.id] = link
return self
end
 
function Criterion:setOrdersetSystemMessage(order, nstext)
self.ordersystem_message[nsself.current_ns.id] = ordertext
return self
end
 
function Criterion:setTextsetDefinition(text, ns, grammatical_number)
self.textdefinition[grammatical_number][nsself.current_ns.id] = text
return self
end
 
function Criterion:setValid(valid, ns)
self.validity[nsself.current_ns.id] = valid
return self
end
 
function Criterion:setValidForNsType(valid, ns_type)
self.validity[ns_type.value] = valid
return self
end
 
Riga 87 ⟶ 226:
-- =============================================================================
 
local function to_namespace_idto_criterion(nscriterion_code, criterion_cfg)
local criterion = Criterion:new(criterion_code)
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
 
for ns, props in pairs(criterion_cfg) do
local function to_criterion(key, value)
ns = Namespace:findByName(ns)
local criterion = Criterion:new(key)
 
for ns, props in pairs(value) do
ns = to_namespace_id(ns)
 
if ns and type(props) == 'table' then
criterion:setCurrentNs(ns)
 
if type(props.ordine) == 'number' then
criterion:setOrder(props.ordine, ns)
end
 
if type(props.link) == 'string' then
criterion:setLink(props.link, ns)
end
 
if type(props.testotesti) == 'table' then
for text_type, text in pairs(props.testi) do
if type(props.testo.singolare) == 'string' then
text_type = TextType:findByArg(text_type)
criterion:setText(props.testo.singolare, ns, 'singolare')
end
 
if type(props.testo.plurale) == 'string'text_type then
criterion:setText[text_type.set](props.testo.plurale, nscriterion, 'plurale'text)
end
end
end
 
if ns == 'default'Namespace.DEFAULT then
if type(props['validità']) == 'table' then
iffor typens_type, valid in pairs(props['validità']['ns pari']) == 'boolean' thendo
ns_type = NamespaceType:findByValue(ns_type)
criterion:setValid(props['validità']['ns pari'], 'ns pari')
end
 
if ns_type and type(props['validità']['ns dispari']valid) == 'boolean' then
criterion:setValidsetValidForNsType(props['validità']['ns dispari']valid, 'ns dispari'ns_type)
end
end
end
elseif type(props['validità']) == 'boolean' then
criterion:setValid(props['validità'], ns)
end
end
Riga 143 ⟶ 272:
end
 
local function get_list_prefixcompare_criteria(list_typea, b)
if a:getOrder() == b:getOrder() then
local list_prefixes = {
return a:getCode() < b:getCode()
['elenco a discesa'] = '**',
else
['elenco puntato'] = '*'
return a:getOrder() < b:getOrder()
}
 
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
 
local function list_criteriaget_suppressed_criteria(argsarg)
local listsuppressed_criteria = ''setmetatable({}, {
__index = function (t, key)
local valid_criteria = {}
return false
 
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})
 
if arg then
local comp = function (a, b)
for criterion_code in string.gmatch(arg, '[%s,]*([^,]*[^%s,])') do
if a:getOrder(args.ns) == b:getOrder(args.ns) then
suppressed_criteria[criterion_code] = true
return a:getAbbreviation() < b:getAbbreviation()
else
return a:getOrder(args.ns) < b:getOrder(args.ns)
end
end
 
return suppressed_criteria
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
 
local function get_args(frame)
local args = require('Module:Arguments').getArgs(frame, { parentOnly = true })
 
if args[1] and args.criterio == nil then
Riga 223 ⟶ 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:getDefault()
 
if args['tipo elencotesto'] == nil or get_list_prefixTextType:findByArg(args['tipo elencotesto']) ==or nil thenTextType:getDefault()
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 245 ⟶ 318:
 
local p = {}
 
function p.get_default_value(frame)
local param = mw.text.trim(frame.args[1] or '')
 
if Enum[param] and Enum[param].getDefault and Enum[param]:getDefault().arg then
return string.format('"%s"', Enum[param]:getDefault().arg)
end
end
 
function p.list_possible_values(frame)
local param = mw.text.trim(frame.args[1] or '')
local possible_values = {}
 
if Enum[param] then
for _, constant in pairs(Enum[param]) do
if constant.arg then
table.insert(possible_values, string.format('"%s"', constant.arg))
end
end
end
 
return table.concat(possible_values, ', ')
end
 
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 args.nscfg[criterion_code] then
local criterion = to_criterion(criterion_code, cfg[criterion_code])
return get_criterion(args)
 
criterion:setCurrentNs(ns):setAddLink(add_link)
 
if criterion:isValid() then
return criterion[text_type.get](criterion)
end
end
end
Riga 256 ⟶ 360:
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']
local list = list_type.section[ns.id]
local valid_criteria = {}
 
for criterion_code, criterion_cfg in pairs(cfg) do
if args.ns then
local criterion = to_criterion(criterion_code, criterion_cfg)
return list_criteria(args)
 
criterion:setCurrentNs(ns):setAddLink(add_link)
 
if criterion:isValid() and suppressed_criteria[criterion:getCode()] == false then
table.insert(valid_criteria, criterion)
end
end
 
table.sort(valid_criteria, compare_criteria)
 
for _, criterion in ipairs(valid_criteria) do
list = string.format('%s\n%s ', list, list_type.prefix)
 
if add_anchor then
list = string.format('%s<span id=%s></span>', list, criterion:getCode())
end
 
list = list .. criterion[text_type.get](criterion)
end
 
return mw.text.trim(list)
end