Module:Bacasable
Apparence
La documentation de ce module est générée par le modèle {{Documentation module}}.
Les éditeurs peuvent travailler dans le bac à sable (créer).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
-- Ce module implémente les modèles {{Albumchart}} et {{Singlechart}}.
local p = {}
local data = {
cfg = {
albumData = 'Module:Classement musical/Données/Album',
singleData = 'Module:Classement musical/Données/Single'
},
formats = {
}
}
-- Useful definitions.
local format = mw.ustring.format
local insert = table.insert
local isTrue = require('Module:Yesno')
local Chart = {}
Chart.__index = Chart
function Chart.new(args, cfg, chartType)
local cleanArgs = {}
for k, v in pairs(args) do
if type(k) == 'string' and v ~= '' then
cleanArgs[k] = mw.text.trim(v)
end
end
local chartName = mw.ustring.lower(cleanArgs[1] or '')
-- Loading the data module.
local data = data--mw.loadData('Module:Classement musical/Données')
local cfg = data.cfg
local moduleName = chartType == 'album' and cfg.albumData or cfg.singleData
local chartData = mw.loadData(moduleName)[chartName]
local root = mw.html.create()
-- To reproduce the behavior of the wikicode, the HTML code is deliberately
-- raw and does not use mw.html.
if isTrue(args.rowheader) then
root:wikitext('! scope="row" style="font-weight:normal;"')
end
root:wikitext('|')
local fields = {
args = cleanArgs,
isAlbumChart = chartType == 'album',
chartName = chartName,
chartData = chartData,
formatData = data.formats[chartData.format] or {},
cfg = cfg,
root = mw.html.create(),
errorDetect = false
}
return setmetatable(fields, Chart)
end
function Chart:__tostring()
return tostring(self.root)
end
local function formattingError(missingParams)
local res = mw.html.create('span')
:addClass('error')
:css('font-size', '100%')
:wikitext('Erreur : ')
-- Catégorie de détection pour maintenance.
res:wikitext(donnees.cfg['catDetection' .. typeChart])
-- Exception de type classement non répertorié.
if type(argsManquants) == 'string' then
res:wikitext(format(donnees.cfg['chartErreur'], argsManquants))
return tostring(res)
end
--Mise en forme des arguments manquants.
for cle, val in ipairs(argsManquants) do
argsManquants[cle] = '<code>|' .. val .. '=</code>'
end
-- Maximum de 2 arguments manquants.
res:wikitext(format(donnees.cfg['argErreur' .. #argsManquants],
args[1],
unpack(argsManquants)))
return res
end
function Chart:checkFormat()
local missingParams = {}
local requiredParams = self.formatData.params
for _, requiredParam in pairs(requiredParams) do
if not self.args[requiredParam] then
insert(missingParams, requiredParam)
end
end
if next(missingParams) then
self.root:node(formattingError(missingParams))
self.errorDetect = true
end
end
function Chart:buildRefLink()
-- Fonction retournant la mise en forme du lien URL dans le format [lien titre].
local argLien, argTitre = {}, {}
local res
for cleFormats, valFormats in pairs(donnees.formatsLiens) do
if valChart[cleFormats] then
-- Récupération du lien et du titre du classement.
insert(argLien, valChart[cleFormats])
insert(argTitre, valChart[valFormats.titre])
-- Récupération des arguments nécessaires au lien et au titre.
for _, nomArg in ipairs(valFormats) do
if nomArg ~= 'artisteid'
and nomArg ~= 'semaine'
and nomArg ~= 'année'
and nomArg ~= 'date' then
insert(argLien, encodeUrl(args[nomArg], valFormats.encode))
else
-- Cas de l'insertion de l'année à deux reprises (lienAnnee2).
if valFormats.doubleAnnee and nomArg == 'année' then
insert(argLien, args[nomArg])
end
insert(argLien, args[nomArg])
end
if nomArg == 'chanson' or nomArg == 'album' then
insert(argTitre, format("''%s''", args[nomArg]))
elseif nomArg ~= 'artisteid' then
insert(argTitre, args[nomArg])
end
end
break
end
end
-- Récupération des valeurs de la table et transformation en
-- argument pour mise en forme avec la fonction de formatage.
local lien = format(unpack(argLien))
local titre = format(unpack(argTitre))
-- Mise en forme de l'URL.
res = format('[http://%s %s]', lien, titre)
if valChart.noteInsertion then
if args.semaine and args['année'] then
res = res .. format(donnees.cfg['noteInsertion'], args['année'] .. args.semaine)
else
res = res .. format(donnees.cfg['noteInsertion'], "l'année et la semaine")
end
end
return res
end
function Chart:buildRefContent()
local res = mw.html.create()
local frame = mw.getCurrentFrame()
if chartData.langue then
res
:wikitext(frame:expandTemplate{title = chartData.langue})
:wikitext(' ')
end
res:wikitext(format('%s. %s. %s. ',
self:buildRefLink(),
chartData.noteChart or chartData.chart,
chartData.editeur))
if args['en ligne le'] then
res:wikitext(format('Mis en ligne le %s. ', args['en ligne le']))
end
if args['consulté le'] then
res:wikitext(format('Consulté le %s.', args['consulté le']))
end
return res
end
function Chart:buildChartRow()
if self.errorDetect then return end
local frame = mw.getCurrentFrame()
local refContent = self:buildRefContent()
node
:wikitext(frame:expandTemplate{title = chartData.flag})
:wikitext(format(' (%s)', chartData.name))
:wikitext(frame:extensionTag('ref', tostring(refContent), {
name = args.refname or chartType .. 'c_' .. chartName,
group = args.refgroup
}))
if args.note then
node
:tag('br')
:done()
:tag('span')
:css('font-style', 'italic')
:css('font-size', '89%')
:wikitext(args.note)
end
end
function Chart:buildPositionRow()
self.root
:newline()
:wikitext('| style="text-align:center;"|')
:wikitext(self.args[2] or '{{{2}}}')
end
-- Access in the module space.
function p._main(args, chartType)
local chart = Chart.new(args, cfg, chartType)
return tostring(chart:checkFormat():buildChartRow():buildPositionRow())
end
-- Access outside the module space.
function p.main(frame)
local argsParent = frame:getParent().args
return p._main(argsParent, frame.args[1])
end
return p