--[[
* 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.short_text = { default = '' }
self.long_text = { 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:getShortText(ns)
return self.short_text[ns] or self.short_text.default
end
function Criterion:getLongText(ns)
return self.long_text[ns] or self.long_text.default
end
function Criterion:getText(ns, text_type)
if text_type == 'completo' then
return self:getLongText(ns)
elseif text_type == 'breve' then
return self:getShortText(ns)
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:setOrder(order, ns)
self.order[ns] = order
end
function Criterion:setShortText(short_text, ns)
self.short_text[ns] = short_text
end
function Criterion:setLongText(long_text, ns)
self.long_text[ns] = long_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.wikitesto) == 'string' then
criterion:setShortText(props.wikitesto, ns)
end
if type(props.testo_lungo) == 'string' then
criterion:setLongText(props.testo_lungo, ns)
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_criterion(criterion_abbreviation, ns, text_type)
for key, value in pairs(cfg) do
local criterion = to_criterion(key, value)
if criterion:getAbbreviation() == criterion_abbreviation then
return criterion:getText(ns, text_type)
end
end
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 list_criteria(ns, list_type, text_type)
local list = ''
local valid_criteria = {}
for key, value in pairs(cfg) do
local criterion = to_criterion(key, value)
if criterion:isValid(ns) == true then
table.insert(valid_criteria, criterion)
end
end
local comp = function (a, b)
if a:getOrder(ns) == b:getOrder(ns) then
return a:getAbbreviation() < b:getAbbreviation()
else
return a:getOrder(ns) < b:getOrder(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(list_type), criterion:getText(ns, text_type))
end
if list_type == 'a discesa' then
list = '* Cancellazione immediata' .. list
end
return mw.text.trim(list)
end
-- =============================================================================
-- Funzioni esportate
-- =============================================================================
local p = {}
function p.main(frame)
local args = getArgs(frame, { parentOnly = true })
local ns, list_type, text_type, criterion_abbreviation
if args.ns == 'Principale' then
ns = ''
elseif args.ns and args.ns ~= '' then
ns = args.ns
else
ns = 'default'
end
if args['tipo elenco'] and get_list_prefix(args['tipo elenco']) then
list_type = args['tipo elenco']
else
list_type = 'a discesa'
end
if args['tipo testo'] == 'completo' or args['tipo testo'] == 'breve' then
text_type = args['tipo testo']
else
text_type = 'breve'
end
if args['criterio'] and args['criterio'] ~= '' then
criterion_abbreviation = args['criterio']
end
if mw.site.namespaces[ns] == nil and ns ~= 'default' then
return
elseif criterion_abbreviation then
return get_criterion(criterion_abbreviation, ns, text_type)
else
return list_criteria(ns, list_type, text_type)
end
end
return p