Modulo:Videogioco

Info Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Videogioco/man (modifica · cronologia)
Sandbox: Modulo:Videogioco/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Videogioco/test (modifica · cronologia · esegui)

Modulo Lua di appoggio al template {{Videogioco}}, per gestirne le funzioni di categorizzazione automatica.

Ha una sottopagina di configurazione: Modulo:Videogioco/Configurazione.


--[[
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 = {}
    for alias, name in pairs(cfg['alias_' .. table_name]) do
        if not reverse_alias[name] then reverse_alias[name] = {} end
        table.insert(reverse_alias[name], alias)
    end
    return reverse_alias
end

-- ========================================================================
-- Sostituisce name con il suo alias se presente nella tabella alias
-- quindi ritorna il valore corrispondente dalla tabella values
-- ========================================================================
local function get_category(name, table_name)
    name = cfg['alias_' .. table_name][name] or name
    return cfg[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, table_name, lowercase, must_exist, cat_assenza)
    local categories = {}
    local name_value = args[base_name] or args[base_name .. " 1"]
    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
        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
        errors_msg[#errors_msg+1] = 'Inserire solo uno di  ' .. base_name .. ' e ' .. base_name .. ' ' .. tostring(index)
    end
    return categories
end

-- ========================================================================
-- Categorizzazione per aziende (sviluppo N e pubblicazione N)
-- ========================================================================
local function categoria_azienda(str)
	-- converte eventuali alias
	str = cfg['alias_aziende'][str] or str
	-- se il nome contiene un wikilink, estrae la voce puntata, senza disambiguante
	local azienda = mw.ustring.match(str, "%[%[([^%]%(|]+)") or str
	azienda = mw.text.trim(azienda)
	-- 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
    end
    if args[base_name] and args[base_name .. " 1"] then
        errors_msg[#errors_msg+1] = 'Inserire solo uno di  ' .. base_name .. ' e ' .. base_name .. ' ' .. tostring(index)
    end
    return categories
end

-- ========================================================================
-- categorizza
-- ========================================================================
function p.categorie(frame)
    local args = getArgs(frame, {parentOnly = 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 categories = {}
    --if true then return mw.text.jsonEncode(categories) end
    append_table(categories, categorizza(args, 'serie', 'serie'))
    if args.anno then
        local anno = tonumber(args.anno)
        if anno == nil then
            if args.anno == 'annullato' then
                categories[#categories+1] = 'Videogiochi annullati'
            else
                errors_msg[#errors_msg+1] = 'anno di pubblicazione non valido'
            end
        elseif anno <= 1970 then
            categories[#categories+1] = 'Videogiochi fino al 1970'
        else
            categories[#categories+1] = 'Videogiochi del ' .. args.anno
        end
    else
        categories[#categories+1] = 'Videogiochi senza anno'
    end
    append_table(categories, categorizza(args, 'genere', 'genere', true, true, true))
    append_table(categories, categorizza(args, 'tema', 'tema', true))
    for i = 1, 3 do
    	local origine = args['origine ' .. i] or i == 1 and args['origine']
    	if not origine then
    		categories[#categories+1] = i == 1 and 'Videogiochi senza nazionalità' or nil
    		break
    	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))
    append_table(categories, categorizza_aziende(args, 'sviluppo'))
    append_table(categories, categorizza_aziende(args, 'pubblicazione'))
    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)
    -- Categorie madri da annullare in presenza delle figlie
    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
        categories[#categories+1] = '[[Categoria:Errori di compilazione del template Videogioco]]'
    end
    if #categories == 0 then return '' end
    return frame.args.debug and mw.text.nowiki(table.concat(categories)) or table.concat(categories)
end

-- ========================================================================
-- Visualizza le tabelle delle configurazioni per il manuale
-- ========================================================================
function p.tabella_configurazione(frame)
    local args = getArgs(frame)
    local table_name = args[1]
    if not(table_name) then return '' end
    local reverse_alias = build_reverse_alias(table_name)
    local root = mw.html.create('table')
    root
        :addClass('wikitable sortable')
        :tag('tr')
            :tag('th'):wikitext(table_name):done()
            :tag('th'):wikitext('Alias'):done()
            :tag('th'):wikitext('Categoria'):done()
    for name, cat_name in pairs(cfg[table_name]) do
        local name_code = replace_braces(name)
        local cat_code = '[[:Categoria:' .. cat_name .. ']]'
        local alias_code = '&nbsp;'
        if reverse_alias[name] then
            for i,alias in ipairs(reverse_alias[name]) do
                reverse_alias[name][i] = replace_braces(alias)
            end
            alias_code = table.concat(reverse_alias[name], '<br />')
        end
        root:tag('tr')
            :tag('td'):wikitext(name_code):done()
            :tag('td'):wikitext(alias_code):done()
            :tag('td'):wikitext(cat_code):done()
    end
    return tostring(root)
end
return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.