Modulo:Criteri cancellazione immediata/sandbox
Aspetto
--[[
* Modulo che implementa i template {{Criterio cancellazione immediata}} e
* {{Criteri cancellazione immediata}}.
]]
require('strict')
local cfg = mw.loadJsonData('Modulo:Criteri cancellazione immediata/Configurazione/sandbox.json')
local getArgs = require('Module:Arguments').getArgs
-- =============================================================================
-- classe Criterion
-- =============================================================================
local Criterion = {}
-- Costruttore della classe Criterion
function Criterion:new(abbreviation)
local self = {}
setmetatable(self, { __index = Criterion })
self.abbreviation = abbreviation
self.order = { default = 0 }
self.link = { default = string.format('[[WP:IMMEDIATA|%s]]', abbreviation) }
self.text = { singolare = { default = '' }, plurale = { default = '' } }
self.validity = { default = true }
return self
end
function Criterion:getAbbreviation()
return self.abbreviation
end
function Criterion:getLink(ns)
return self.link[ns] or self.link.default
end
function Criterion:getOrder(ns)
return self.order[ns] or self.order.default
end
function Criterion:getText(ns, grammatical_number, add_link)
local text = self.text[grammatical_number][ns] or self.text[grammatical_number].default
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:isValid(ns)
local ns_type
if mw.site.namespaces[ns] then
ns_type = mw.site.namespaces[ns].id % 2 == 0 and 'ns pari' or 'ns dispari'
end
if self.validity[ns] ~= nil then
return self.validity[ns]
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[ns] = link
end
function Criterion:setOrder(order, ns)
self.order[ns] = order
end
function Criterion:setText(text, ns, grammatical_number)
self.text[grammatical_number][ns] = text
end
function Criterion:setValid(valid, ns)
self.validity[ns] = valid
end
-- =============================================================================
-- 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)
local criterion = Criterion:new(key)
for ns, props in pairs(value) do
ns = to_namespace_id(ns)
if ns and type(props) == 'table' then
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.testo) == 'table' then
if type(props.testo.singolare) == 'string' then
criterion:setText(props.testo.singolare, ns, 'singolare')
end
if type(props.testo.plurale) == 'string' then
criterion:setText(props.testo.plurale, ns, 'plurale')
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' then
if type(props['validità']) == 'table' then
if type(props['validità']['ns pari']) == 'boolean' then
criterion:setValid(props['validità']['ns pari'], 'ns pari')
end
if type(props['validità']['ns dispari']) == 'boolean' then
criterion:setValid(props['validità']['ns dispari'], 'ns dispari')
end
end
elseif type(props['validità']) == 'boolean' then
criterion:setValid(props['validità'], ns)
end
end
end
return criterion
end
local function get_list_prefix(list_type)
local list_prefixes = {
['elenco a discesa'] = '**',
['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
local function list_criteria(args)
local list = ''
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
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
local function get_args(frame)
local args = getArgs(frame, { parentOnly = true })
if args[1] and args.criterio == nil then
args.criterio, args[1] = args[1], nil
end
args.ns = to_namespace_id(args.ns) or 'default'
if args['tipo elenco'] == nil or get_list_prefix(args['tipo elenco']) == nil then
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ì' and true or false
args['escludi criteri'] = mw.text.split(args['escludi criteri'] or '', '%s*,%s*')
return args
end
-- =============================================================================
-- Funzioni esportate
-- =============================================================================
local p = {}
function p.get_criterion(frame)
local args = get_args(frame)
if args.criterio and args.ns then
return get_criterion(args)
end
end
function p.list_criteria(frame)
local args = get_args(frame)
if args.ns then
return list_criteria(args)
end
end
return p