Module:Infobox Console
Apparence
La documentation pour ce module peut être créée à Module:Infobox Console/doc
local p = {}
local PROPRIETES = {
type_console = 'P117',
date_sortie = 'P118',
region_sortie = 'P119',
generation_dediee = 'P120',
generation_compatible = 'P121',
mode_compatibilite = 'P123',
}
local ORDRE_SORTIES = { 'Japon', 'Amérique du Nord', 'Australie', 'Europe' }
local GENERATIONS = {
['Première génération'] = '[[Première génération|1]]',
['Deuxième génération'] = '[[Deuxième génération|2]]',
['Troisième génération'] = '[[Troisième génération|3]]',
['Quatrième génération'] = '[[Quatrième génération|4]]',
['Cinquième génération'] = '[[Cinquième génération|5]]',
['Sixième génération'] = '[[Sixième génération|6]]',
['Septième génération'] = '[[Septième génération|7]]',
['Huitième génération'] = '[[Huitième génération|8]]',
['Neuvième génération'] = '[[Neuvième génération|9]]',
['Dixième génération'] = '[[Dixième génération|10]]',
}
local NOTES_COMPATIBILITE = {
['Console Virtuelle'] = 'Sur Console Virtuelle.',
}
local function non_vide(valeur)
return valeur ~= nil and mw.text.trim(valeur) ~= ''
end
local function get_args(frame)
local args = {}
local parent = frame:getParent()
if parent then
for cle, valeur in pairs(parent.args) do
if non_vide(valeur) then
args[cle] = valeur
end
end
end
for cle, valeur in pairs(frame.args) do
if non_vide(valeur) then
args[cle] = valeur
end
end
return args
end
local function get_entity(args)
if non_vide(args.item) then
return mw.wikibase.getEntity(mw.text.trim(args.item))
end
return mw.wikibase.getEntity()
end
local function get_string_value(statement)
if not statement or not statement.mainsnak or not statement.mainsnak.datavalue then
return nil
end
return statement.mainsnak.datavalue.value
end
local function get_first_value(entity, property_id)
local statements = entity:getBestStatements(property_id)
return get_string_value(statements[1])
end
local function get_first_qualifier_value(statement, property_id)
if not statement.qualifiers or not statement.qualifiers[property_id] then
return nil
end
local qualifier = statement.qualifiers[property_id][1]
if not qualifier or not qualifier.datavalue then
return nil
end
return qualifier.datavalue.value
end
local function majuscule_initiale(texte)
if not non_vide(texte) then
return texte
end
return mw.ustring.upper(mw.ustring.sub(texte, 1, 1)) .. mw.ustring.sub(texte, 2)
end
local function format_time(statement)
local value = get_string_value(statement)
if type(value) ~= 'table' or type(value.time) ~= 'string' then
return nil
end
local annee, mois, jour = value.time:match('^%+?(%d%d%d%d)%-(%d%d)%-(%d%d)T')
if not annee then
return nil
end
local precision = tonumber(value.precision)
local iso = annee .. '-' .. mois .. '-' .. jour
if precision == 9 then
return annee
end
if precision == 10 then
if mois == '00' then
return annee
end
return majuscule_initiale(
mw.getContentLanguage():formatDate('F Y', annee .. '-' .. mois .. '-01')
)
end
if mois == '00' or jour == '00' then
return nil
end
if jour == '01' then
return '1<sup>er</sup> ' .. mw.getContentLanguage():formatDate('F Y', iso)
end
return mw.getContentLanguage():formatDate('j F Y', iso)
end
local function format_sortie(statement)
if not statement or not statement.mainsnak then
return nil
end
if statement.mainsnak.snaktype == 'novalue' then
return "''Non sorti''"
end
if statement.mainsnak.snaktype == 'somevalue' then
return "''Inconnue''"
end
return format_time(statement)
end
local function get_sorties(entity)
local sorties = {}
for _, statement in ipairs(entity:getBestStatements(PROPRIETES.date_sortie)) do
local region = get_first_qualifier_value(statement, PROPRIETES.region_sortie)
local date = format_sortie(statement)
if non_vide(region) and non_vide(date) then
sorties[region] = date
end
end
return sorties
end
local function format_generation(valeur)
if not non_vide(valeur) then
return nil
end
return GENERATIONS[valeur] or valeur
end
local function format_note_compatibilite(frame, statement)
local mode = get_first_qualifier_value(statement, PROPRIETES.mode_compatibilite)
if not non_vide(mode) then
return ''
end
local texte = NOTES_COMPATIBILITE[mode] or ('Sur ' .. mode .. '.')
return frame:expandTemplate{
title = 'Infobulle',
args = { '*', texte }
}
end
local function format_generation_statement(frame, statement, property_id)
local generation = format_generation(get_string_value(statement))
if not non_vide(generation) then
return nil
end
if property_id == PROPRIETES.generation_compatible then
generation = generation .. format_note_compatibilite(frame, statement)
end
return generation
end
local function concatener(valeurs)
if #valeurs == 0 then
return '{{?}}'
end
if #valeurs == 1 then
return valeurs[1]
end
if #valeurs == 2 then
return valeurs[1] .. ' et ' .. valeurs[2]
end
return table.concat(valeurs, ', ', 1, #valeurs - 1) .. ' et ' .. valeurs[#valeurs]
end
local function format_generations(frame, entity, property_id)
local resultat = {}
for _, statement in ipairs(entity:getBestStatements(property_id)) do
if statement.mainsnak and statement.mainsnak.snaktype == 'novalue' then
if property_id == PROPRIETES.generation_compatible then
return 'Aucune'
end
else
local generation = format_generation_statement(frame, statement, property_id)
if non_vide(generation) then
table.insert(resultat, generation)
end
end
end
return concatener(resultat)
end
function p.infobox(frame)
local args = get_args(frame)
local entity = get_entity(args)
if not entity then
return ''
end
local nom_page = non_vide(args.titre) and mw.text.trim(args.titre) or mw.title.getCurrentTitle().text
local sorties = get_sorties(entity)
local entete = nom_page
local resultat = {
'{| class="tableaustandard ficheinfo"',
}
if args.miniature == 'oui' then
entete = '<div style="float:right;">[[Fichier:Miniat ' .. nom_page .. '.png]]</div>' .. entete
end
table.insert(resultat, '|-')
table.insert(resultat, '! colspan="2" class="entêtesection" | ' .. entete)
table.insert(resultat, '|-')
table.insert(resultat, '| colspan="2" class="illustration" | [[Fichier:' .. nom_page .. '.png|200px]]')
if args.logo ~= 'non' then
table.insert(resultat, '|-')
table.insert(resultat, '! width="30%" | Logo')
table.insert(resultat, '| [[Fichier:Logo ' .. nom_page .. '.png|x30px]]')
end
table.insert(resultat, '|-')
table.insert(resultat, '! Type de console')
table.insert(resultat, '| ' .. (get_first_value(entity, PROPRIETES.type_console) or ''))
table.insert(resultat, '|-')
table.insert(resultat, '! colspan="2" class="entêtesection" | Dates de sortie')
for _, region in ipairs(ORDRE_SORTIES) do
if non_vide(sorties[region]) then
table.insert(resultat, '|-')
table.insert(resultat, '! ' .. region)
table.insert(resultat, '| ' .. sorties[region])
end
end
table.insert(resultat, '|-')
table.insert(resultat, '! colspan="2" class="entêtesection" | Générations')
table.insert(resultat, '|-')
table.insert(resultat, '! Dédiées')
table.insert(resultat, '| ' .. format_generations(frame, entity, PROPRIETES.generation_dediee))
table.insert(resultat, '|-')
table.insert(resultat, '! Compatibles')
table.insert(resultat, '| ' .. format_generations(frame, entity, PROPRIETES.generation_compatible))
table.insert(resultat, '|}')
return table.concat(resultat, '\n')
end
return p