--[[
* 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_criterion(criterion_abbreviation, ns, text_type, add_link)
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, add_link)
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, add_link)
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, add_link))
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, add_link, 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'] == 'singolare' or args['tipo testo'] == 'plurale' then
text_type = args['tipo testo']
else
text_type = 'singolare'
end
if args['link'] == 'no' then
add_link = false
else
add_link = true
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, add_link)
else
return list_criteria(ns, list_type, text_type, add_link)
end
end
return p