Modulo:Videogioco: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
r
revisione della categorizzazione per aziende, v. dp:Videogiochi#Categorizzazione_per_azienda
 
(25 versioni intermedie di 5 utenti non mostrate)
Riga 1:
--[[
Questo modulo è in appoggio al template Videogioco, per gestirne le funzioni di
categorizzazione automatica
]]
local p = {}
local errors_msg = {}
 
local cfg = mw.loadData("Modulo:Videogioco/Configurazione")
local getArgs = require('Module:Arguments').getArgs
 
local function build_reverse_alias(table_name)
local reverse_alias = {}
Riga 22 ⟶ 20:
-- quindi ritorna il valore corrispondente dalla tabella values
-- ========================================================================
local function get_category(name, values_table, alias_tabletable_name)
ifname alias_table= thencfg['alias_' name.. = alias_table(table_name][name)] or name end
return values_tablecfg[table_name][name]
end
 
-- ========================================================================
-- Rimpiazza le parentesi quadre nella stringa con i corrispondenti codici
-- ascii
-- ========================================================================
local function replace_braces(s)
local new_s = mw.ustring.gsub(s, "%[", "[")
new_s = mw.ustring.gsub(new_s, "%]", "]")
new_s = mw.ustring.gsub(new_s, " ", " ")
return new_s
end
 
-- ========================================================================
-- Appende una tabella a un'altra tabella
-- ========================================================================
local function append_table(t1, t2)
for _,el in ipairs(t2) do
t1[#t1+1] = el
end
end
 
-- ========================================================================
-- Restituisce l'aggettivo plurale maschile del Paese specificato usando
-- il template AggNaz
-- ========================================================================
local function get_adj(paese)
local adj
local title = mw.title.new('Template:AggNaz/' .. paese)
adj = title and title.exists and
mw.getCurrentFrame():expandTemplate {
title = title.text,
args = { 'mp' }
}
 
return adj
end
 
-- ========================================================================
-- Controlla la presenza di una categoria figlia tra quelle da aggiungere
-- per evitare eventuali conflitti di categorizzazione
-- ========================================================================
local function check_redundancy(t, cat)
for _,v in ipairs(t) do
if v == cat then
return true
end
end
return false
end
 
-- ========================================================================
-- Ritorna una lista di categorie per la sequenza di parametri con
-- nome base 'base_name' consultando la tabella 'table'.
-- 'alias_table' è una tabella di nomi alternativi per i valori dei
-- parametri, 'max_index' il numero massimo dell'indice del parametro
-- da controllare
-- ========================================================================
local function categorizza(args, base_name, values_tabletable_name, alias_tablelowercase, max_indexmust_exist, lowercasecat_assenza)
local categories = {}
iflocal name_value = args[base_name] thenor args[base_name .. " 1"]
localif name_value = args[base_name]then
if lower_caselowercase then name_value = mw.mw.ustring.lower(name_value) end
local category = get_category(name_value, values_table, alias_tabletable_name)
if category then categories[category] = true end
categories[#categories+1] = category
elseif must_exist then
errors_msg[#errors_msg+1] = 'Valore non valido per ' .. base_name
end
local index = 2
while true do
name_value = args[base_name .. ' ' .. tostring(index)]
if name_value then
if lowercase then name_value = mw.ustring.lower(name_value) end
local category = get_category(name_value, table_name)
if category then
categories[#categories+1] = category
elseif must_exist then
errors_msg[#errors_msg+1] = 'Valore non valido per ' .. base_name .. ' ' .. tostring(index)
end
else
break
end
index = index + 1
end
elseif cat_assenza and not args[base_name .. " altro"] then
categories[#categories+1] = 'Videogiochi senza ' .. base_name
end
if args[base_name] and args[base_name .. " 1"] then
local index = 1
errors_msg[#errors_msg+1] = 'Inserire solo uno di ' .. base_name .. ' e ' .. base_name .. ' ' .. tostring(index)
local flag_stop = false
end
local previous_present = true
return categories
local anomaly = false
end
while not flag_stop do
 
local name = base_name .. tostring(index)
-- ========================================================================
if args[name] then
-- Categorizzazione per aziende (sviluppo N e pubblicazione N)
local name_value = args[base_name]
-- ========================================================================
if lower_case then name_value = mw.mw.ustring.lower(name_value) end
local function categoria_azienda(str)
local category = get_category(name_value, values_table, alias_table)
-- converte eventuali alias
if category then categories[category] = true end
str = cfg['alias_aziende'][str] or str
if index > 2 and not previous_present then anomaly = true end
-- se il nome contiene un wikilink, estrae la voce puntata, senza disambiguante
previous_present = true
local azienda = mw.ustring.match(str, "%[%[([^%]%(|]+)") or str
else
azienda = mw.text.trim(azienda)
previous_present = false
-- se esiste "cat:Videogiochi Azienda" la seleziona
local cat = "Videogiochi " .. azienda
local titolo = mw.title.new(cat, "Categoria")
if titolo and titolo.exists then
return cat
end
return nil
end
 
local function categorizza_aziende(args, base_name)
local categories = {}
local name_value = args[base_name] or args[base_name .. " 1"]
if name_value then
local cat = categoria_azienda(name_value)
if cat then
categories[#categories+1] = cat
end
local index = 2
while true do
name_value = args[base_name .. ' ' .. tostring(index)]
if name_value then
cat = categoria_azienda(name_value)
if cat then
categories[#categories+1] = cat
end
else
break
end
index = index + 1
end
index = index + 1
if index > max_index then flag_stop = true end
end
if args[base_name] and args[base_name .. '" 1'"] then anomaly = true end
if args errors_msg[base_name#errors_msg+1] ..= '2']Inserire andsolo notuno (args[base_name]di or args[' .. base_name .. '1 e ']) then.. anomalybase_name =.. true' end' .. tostring(index)
end
return categories, anomaly
return categories
end
 
-- ========================================================================
----------------------------------------------------------------------------------
-- categorizza
-- ========================================================================
----------------------------------------------------------------------------------
function p.categorie(frame)
local args = getArgs(frame, {parentOnly =true not frame.args.debug})
local current_page = mw.title.getCurrentTitle()
local current_namespace = current_page.namespace
if current_namespace ~= 0 and not frame.args.debug then return '' end
local errors_msgcategories = {}
--if true then return mw.text.jsonEncode(categories) end
local cat = {}
append_table(categories, categorizza(args, 'serie', 'serie'))
local anomaly = {}
categories.sviluppo, anomaly.sviluppo = categorizza(args, 'sviluppo', cfg.aziende, cfg.aliaz_aziende, 6)
categories.pubblicazione, anomaly.pubblicazione = categorizza(args, 'pubblicazione', cfg.aziende, cfg.aliaz_aziende, 3)
categories.serie, anomaly.serie = categorizza(args, 'serie', cfg.serie, cfg.alias_serie, 3, true)
if args.anno then
categories.anno = {}
local anno = tonumber(args.anno)
if anno == nil then
errors_msg[#errors_msg+1]if args.anno == 'anno di pubblicazione non validoannullato' then
categories[#categories+1] = 'Videogiochi annullati'
else
errors_msg[#errors_msg+1] = 'anno di pubblicazione non valido'
end
elseif anno <= 1970 then
categories.anno[#categories+1] = 'Videogiochi fino al 1970'] = true
else
categories.anno[#categories+1] = 'Videogiochi del ' .. args.anno] = true
end
else
categories[#categories+1] = 'Videogiochi senza anno'
end
append_table(categories.tema, anomaly.tema = categorizza(args, 'temagenere', cfg.tema'genere', cfg.alias_tematrue, 3true, true))
append_table(categories.piattaforme, anomaly.piattaforme = categorizza(args, 'piattaformatema', cfg.'tema', cfg.alias_tema, 3true))
for i = 1, 3 do
categories.distribuzione, anomaly.distribuzione = categorizza(args, 'distribuzione', cfg.tema, cfg.distribuzione_tema, 3)
local origine = args['origine ' .. i] or i == 1 and args['origine']
local cat_dict = {}
if not origine then
for cat_name,cat_list in pairs(categories) do
categories[#categories+1] = i == 1 and 'Videogiochi senza nazionalità' or nil
for cat,_ in pairs(cat_list) do
break
cat_dict[cat] = true
end
local adj = get_adj(origine)
if adj then
categories[#categories+1] = 'Videogiochi ' .. adj
end
end
append_table(categories, categorizza(args, 'piattaforma', 'piattaforma', false, true, true))
local cat_list = {}
append_table(categories, categorizza_aziende(args, 'sviluppo'))
for cat,_ in pairs(cat_dict) do
append_table(categories, categorizza_aziende(args, 'pubblicazione'))
cat_list[#cat_list+1] = '[[Categoria:' .. cat .. ']]'
append_table(categories, categorizza(args, 'distribuzione digitale', 'distribuzione', false, true))
entity = mw.wikibase.getEntity()
if entity and entity.claims and entity.claims['P3442'] then
categories[#categories+1] = 'Videogiochi per Debian'
end
local cache = mw.clone(categories)
if anomaly.sviluppo or anomaly.pubblicazione or anomaly.serie or anomaly.tema or anomaly.piattaforme or anomaly.distribuzione then
-- Categorie madri da annullare in presenza delle figlie
cat_list[#cat_list+1] = '[[Categoria:Errori di compilazione del template Videogioco - ordine parametri]]'
local sub_cat = {
['Videogiochi d\'orrore'] = 'Survival horror'
}
for index,cat in ipairs(categories) do
if sub_cat[cat] and check_redundancy(cache, sub_cat[cat]) then
categories[index] = ''
else
categories[index] = '[[Categoria:' .. cat .. ']]'
end
end
if #errors_msg>0 then
cat_listcategories[#cat_listcategories+1] = '[[Categoria:Errori di compilazione del template Videogioco]]'
end
if #cat_listcategories == 0 then return '' end
return frame.args.debug and mw.text.nowiki(table.concat(categories)) or table.concat(categories)
return table.join(cat_list)
end
 
-- ========================================================================
----------------------------------------------------------------------------------
-- Visualizza le tabelle delle configurazioni per il manuale
-- Ritorna la configurazione della tabella per le aziende di videogiochi
-- ========================================================================
----------------------------------------------------------------------------------
function p.aziendetabella_configurazione(frame)
local args = getArgs(frame)
local table_name = args[1]
Riga 130 ⟶ 246:
:tag('th'):wikitext('Categoria'):done()
for name, cat_name in pairs(cfg[table_name]) do
local name_code = '<nowiki>' .. replace_braces(name .. '</nowiki>')
local cat_code = '[[:Categoria:' .. cat_name .. ']]'
local alias_code = '&nbsp;'
if reverse_alias[name] then
localfor alias_codei,alias =in '<nowiki>' .. table.joinipairs(reverse_alias[name], '</nowiki>, <nowiki>') .. '</nowiki>'do
reverse_alias[name][i] = replace_braces(alias)
end
alias_code = table.concat(reverse_alias[name], '<br />')
end
root:tag('tr')
Riga 143 ⟶ 262:
return tostring(root)
end
return p