--[[
* Modulo che implementa il template {{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.text = { singolare = { default = '' }, plurale = { default = '' } }
self.validity = { default = true }
return self
end
function Criterion:getAbbreviation()
return self.abbreviation
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
text = string.format('([[WP:IMMEDIATA|%s]]) %s', self:getAbbreviation(), text)
end
return text
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: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_criterion(key, value)
local criterion = Criterion:new(key)
for ns, props in pairs(value) do
if type(props) == 'table' then
if type(props.ordine) == 'number' then
criterion:setOrder(props.ordine, 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
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 numerato'] = '#',
['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
table.insert(valid_criteria, criterion)
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 = string.format('%s\n%s %s', list, get_list_prefix(args['tipo elenco']),
criterion:getText(args.ns, args['tipo testo'], args.link))
end
if args['tipo elenco'] == 'a discesa' then
if args.ns == 'File' 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
if args.ns == 'Principale' then
args.ns = ''
elseif args.ns == nil then
args.ns = 'default'
end
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
return args
end
-- =============================================================================
-- Funzioni esportate
-- =============================================================================
local p = {}
function p.get_criterion(frame)
local args = get_args(frame)
if args.criterio and (mw.site.namespaces[args.ns] ~= nil or args.ns == 'default') then
return get_criterion(args)
end
end
function p.list_criteria(frame)
local args = get_args(frame)
if mw.site.namespaces[args.ns] ~= nil or args.ns == 'default' then
return list_criteria(args)
end
end
return p