Modulo:Videogioco/sandbox

--[[
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/sandbox")
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

-- ========================================================================
-- Evita possibili 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)
    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
    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
            errors_msg[#errors_msg+1] = 'anno di pubblicazione non valido'
        elseif anno <= 1970 then
            categories[#categories+1] = 'Videogiochi fino al 1970'
        else
            categories[#categories+1] = 'Videogiochi del ' .. args.anno
        end
    end
    append_table(categories, categorizza(args, 'genere', 'genere', true, true))
    append_table(categories, categorizza(args, 'tema', 'tema', true))
    append_table(categories, categorizza(args, 'piattaforma', 'piattaforma', false, true))
    append_table(categories, categorizza(args, 'sviluppo', 'aziende'))
    append_table(categories, categorizza(args, 'pubblicazione', 'aziende'))
    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
----------------------------------------------------------------------------------
-- Ritorna la configurazione della tabella per le aziende di videogiochi
----------------------------------------------------------------------------------
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.