« Module:Infobox » : différence entre les versions
mAucun résumé des modifications |
petite réorganisation. Mise en cohérence du fonctionnement de localdata et d' item (comme variables globales c:[] mais ça peut vraiment simplifier le code) |
||
| Ligne 1 : | Ligne 1 : | ||
local p = {} | local p = {} | ||
-- variables globales alimentées par les paramètres utilisés | |||
item = nil -- l'élément Wikidata lié | |||
moduledata = nil -- la sous-page de module:Infobox demandée | |||
templatedata = {}-- données concernant les paramètres passés au modèle | |||
page = { -- données concernant la page où est affichée l'infobox | |||
name = mw.title.getCurrentTitle().prefixedText, | |||
namespace = mw.title.getCurrentTitle().namespace, | |||
} | |||
-- l'objet principal à retourner | |||
local infobox = mw.html.create('div') | local infobox = mw.html.create('div') | ||
local | |||
-- objes secondaires à retourner | |||
local maintenance = '' -- chaîne retournfoée avec le module : cats de maintenance | |||
local externaltext = '' -- par exemple coordonnées en titre | |||
-- modules importés | |||
local wikidata = require('Module:Wikidata') | local wikidata = require('Module:Wikidata') | ||
local defaultstyle = require('Module:Infobox/Style') | local defaultstyle = require('Module:Infobox/Style') | ||
local icons = require('Module:Infobox/Icône') | local icons = require('Module:Infobox/Icône') | ||
local mapmod = require('Module:Carte') | local mapmod = require('Module:Carte') | ||
local i18n = { | local i18n = { | ||
| Ligne 51 : | Ligne 60 : | ||
local function getvalue(val, params) | local function getvalue(val, params) | ||
if type(val) == 'string' then | if type(val) == 'string' then | ||
return | return templatedata[val] | ||
elseif type(val) == 'function' then | elseif type(val) == 'function' then | ||
return val( | return val(templatedata, item, params) | ||
end | end | ||
end | end | ||
local function addmaintenancecat(cat, sortkey) | local function addmaintenancecat(cat, sortkey) | ||
if namespace ~= 0 then | if page.namespace ~= 0 then | ||
return '' | return '' | ||
end | end | ||
if cat then | if cat then | ||
maintenance = maintenance .. '[[Category:' .. cat .. '|' .. (sortkey or | maintenance = maintenance .. '[[Category:' .. cat .. '|' .. (sortkey or page.name) .. ']]' | ||
end | end | ||
end | end | ||
| Ligne 109 : | Ligne 118 : | ||
then style['border-color'] = maincolor | then style['border-color'] = maincolor | ||
end | end | ||
local previousval = | local previousval = templatedata[params.previousparameter] | ||
local nextval = | local nextval = templatedata[params.nextparameter] | ||
local navbox = | local navbox = | ||
mw.html.create('p') | mw.html.create('p') | ||
| Ligne 136 : | Ligne 145 : | ||
end | end | ||
for j, k in ipairs(params.imageparameters) do | for j, k in ipairs(params.imageparameters) do | ||
table.insert(images, | table.insert(images, templatedata[k]) | ||
end | end | ||
if #images == 0 and item then | if #images == 0 and item then | ||
| Ligne 174 : | Ligne 183 : | ||
end | end | ||
end | end | ||
local captiontext = | local captiontext = templatedata[params.captionparameter] or params.defaultcaption | ||
local size = | local size = templatedata[params.sizeparameter] or params.defaultsize or '280x400px' | ||
local numsize = mw.ustring.gsub(size, 'x.*', '') | local numsize = mw.ustring.gsub(size, 'x.*', '') | ||
numsize = numsize:gsub('px', '') | numsize = numsize:gsub('px', '') | ||
| Ligne 183 : | Ligne 192 : | ||
end | end | ||
if type(captiontext) == 'function' then | if type(captiontext) == 'function' then | ||
captiontext = captiontext( | captiontext = captiontext(templatedata) | ||
end | end | ||
local style = params.style or {padding ='2px 0',} | local style = params.style or {padding ='2px 0',} | ||
| Ligne 277 : | Ligne 286 : | ||
label = params.label | label = params.label | ||
if type(label) == 'function' then | if type(label) == 'function' then | ||
label = label( | label = label(templatedata, item) | ||
end | end | ||
end | end | ||
| Ligne 349 : | Ligne 358 : | ||
end | end | ||
if type(l) == 'function' then --accepte les fonctions qui retournent des tables | |||
l = l( | l = l(templatedata, item) | ||
end | end | ||
if type(l) == 'nil' then | if type(l) == 'nil' then | ||
| Ligne 389 : | Ligne 398 : | ||
local maptype = params.maptype -- choisit le type de carte le plus approprié (relief, administratif, etc.) | local maptype = params.maptype -- choisit le type de carte le plus approprié (relief, administratif, etc.) | ||
if type(maplist) == 'function' then | if type(maplist) == 'function' then | ||
maplist = maplist( | maplist = maplist(templatedata, item) | ||
end | end | ||
local width = tonumber(params.width) or 280 | local width = tonumber(params.width) or 280 | ||
| Ligne 398 : | Ligne 407 : | ||
local latitude, longitude | local latitude, longitude | ||
if type(params.latitude) == 'function' then | if type(params.latitude) == 'function' then | ||
latitude, longitude = params.latitude( | latitude, longitude = params.latitude(templatedata, item), params.longitude(templatedata, item) | ||
else | else | ||
latitude, longitude = | latitude, longitude = templatedata[params.latitude], templatedata[params.longitude] | ||
end | end | ||
if (not latitude or not longitude) and item and params.wikidata then | if (not latitude or not longitude) and item and params.wikidata then | ||
| Ligne 443 : | Ligne 452 : | ||
style['font-size'] = '80%' | style['font-size'] = '80%' | ||
end | end | ||
local backlinkstr = '[' .. tostring( mw.uri.fullUrl( | local backlinkstr = '[' .. tostring( mw.uri.fullUrl( page.name, '&veaction=edit§ion=0' ) ) .. ' ' .. i18n['edit'] .. ']' | ||
.. ' - [' .. tostring( mw.uri.fullUrl( | .. ' - [' .. tostring( mw.uri.fullUrl( page.name, '&action=edit§ion=0' ) ) .. ' ' .. i18n['edit code'] .. ']' | ||
local itemlinkstr | local itemlinkstr | ||
| Ligne 460 : | Ligne 469 : | ||
:done() | :done() | ||
local doclinkstr = '[[Image:Gtk-dialog-info.svg|12px|link=' .. 'Template:Infobox ' .. | local doclinkstr = '[[Image:Gtk-dialog-info.svg|12px|link=' .. 'Template:Infobox ' .. moduledata.name .. '|' .. i18n['see doc'] .. ']]' | ||
-- si ce lien ne marche pas toujours, il faut ajouter un variable pour le nom de l'infobox récupéré par le frame | -- si ce lien ne marche pas toujours, il faut ajouter un variable pour le nom de l'infobox récupéré par le frame | ||
local doclinkspan = mw.html.create('span') | local doclinkspan = mw.html.create('span') | ||
| Ligne 493 : | Ligne 502 : | ||
end | end | ||
function p._build( | function p._build(modulename, params) | ||
-- | |||
local style = | -- fill-up global variable templatedata using params | ||
for i, j in pairs(params) do | |||
if j and mw.text.trim(j) ~= '' then -- empty parameters are ignored | |||
templatedata[i] = j | |||
end | |||
end | |||
-- load wikidata item as a global variable: | |||
if templatedata.wikidata == 'non' then | |||
item = nil | |||
elseif (not templatedata.wikidata) or (templatedata.wikidata == '') then | |||
item = mw.wikibase.getEntityObject() | |||
else | |||
item = nil -- topic of the infobox corresponds to another Wikidata item, should eventually be fetched through arbitrary access | |||
infoboxrank = 'secondary' | |||
end | |||
-- load infobox module page | |||
moduledata = require('Module:Infobox/' .. modulename) | |||
moduledata.name = modulename | |||
-- guess "rank" of the infobox | |||
local infoboxrank = 'main' -- main infobox of the page, with coordinates displayed in title etc. | |||
if page.namespace ~= 0 then | |||
infoboxrank = 'secondary' | |||
end | |||
templatedata.infoboxrank = infoboxrank | |||
-- adds style to infobox, basd on values defined in the module | |||
local style = moduledata.style or {} | |||
if not style['max-wdith'] then | if not style['max-wdith'] then | ||
style['max-width'] = '300px' | style['max-width'] = '300px' | ||
end | end | ||
local class = | local class = moduledata.class or "infobox_v3" | ||
local parts = | local parts = moduledata.parts | ||
maincolor = | maincolor = templatedata['couleur infobox'] or moduledata.maincolor or "#E1E1E1" | ||
local footerparams = | local footerparams = moduledata.footer or {} | ||
infobox:css(style) | infobox:css(style) | ||
infobox:addClass(class) | infobox:addClass(class) | ||
-- build infobox, block by block | |||
for i, j in pairs(parts) do | for i, j in pairs(parts) do | ||
local block = p.buildblock(j) | local block = p.buildblock(j) | ||
| Ligne 519 : | Ligne 558 : | ||
function p.build(frame) | function p.build(frame) | ||
local name = frame.args.nom | |||
local params = frame:getParent().args | |||
return p._build(name, params) | |||
return p._build( | |||
end | end | ||
return p | return p | ||
Version du 14 avril 2015 à 13:03
local p = {}
-- variables globales alimentées par les paramètres utilisés item = nil -- l'élément Wikidata lié moduledata = nil -- la sous-page de module:Infobox demandée templatedata = {}-- données concernant les paramètres passés au modèle page = { -- données concernant la page où est affichée l'infobox name = mw.title.getCurrentTitle().prefixedText, namespace = mw.title.getCurrentTitle().namespace, }
-- l'objet principal à retourner local infobox = mw.html.create('div')
-- objes secondaires à retourner local maintenance = -- chaîne retournfoée avec le module : cats de maintenance local externaltext = -- par exemple coordonnées en titre -- modules importés local wikidata = require('Module:Wikidata') local defaultstyle = require('Module:Infobox/Style') local icons = require('Module:Infobox/Icône') local mapmod = require('Module:Carte')
local i18n = { ['see doc'] = 'Documentation du modèle', ['edit'] = 'modifier', ['edit code'] = 'modifier le code', ['edit item'] = 'modifier Wikidata', ['tracking cat'] = "Page utilisant des données de Wikidata", ['invalid block type'] = "Bloc de données invalide dans le module d'infobox", ['default cat'] = "Maintenance des infobox", }
local function addwikidatacat(prop) maintenance = maintenance .. '[[Category:' .. i18n['tracking cat'] .. '/' .. string.upper(prop) .. ']]' end
local function getWikidataValue(params, wikidataparam) -- Récupère la valeur Wikidata pour la valeur, soit dans le paramètre "wikidata" soit dans le praramètre "property" if not item then return nil end local value
if not wikidataparam then -- par défaut la valeur wikidata est dans le paramètre "wikidata" mais dans les structures composées comme "title", il y a plusieurs paramètres wikidata wikidataparam = 'wikidata' end if params[wikidataparam] then if type(params[wikidataparam]) == 'function' then value = params[wikidataparam](item) else value = params[wikidataparam] end elseif params.property then value = wikidata.formatAndCat({entity = item, property = params.property, conjtype = 'comma'}) end return value end
local function getvalue(val, params) if type(val) == 'string' then return templatedata[val] elseif type(val) == 'function' then return val(templatedata, item, params) end end
local function addmaintenancecat(cat, sortkey) if page.namespace ~= 0 then return end if cat then maintenance = maintenance .. end end
function p.buildtitle(params)
local text = getvalue(params.value, params) or params.textdefaultvalue or getWikidataValue(params) or mw.title.getCurrentTitle().text
local subtext = getvalue(params.subtitle) or getWikidataValue(params, 'wikidatasubtitle') or params.subtitledefaultvalue
if subtext and (subtext ~= text) then
text = text .. '
' .. subtext .. ''
end
local icon =
if params.icon then
icon = icons[params.icon] or params.icon
icon = 'Fichier:' .. icon .. ''
end
local style = defaultstyle.entete
-- overwrites with those provided in the module if maincolor then style['background-color'] = maincolor end if params.style then for i, j in pairs(params.style) do style[i] = j end end local padleft = '0'-- add padding to center text when there is an icon if icon ~= then padleft = '40' end local title = mw.html.create('div') :css(style) :tag('div') :wikitext(text) :cssText("display:table-cell; vertical-align:middle; zoom:1;width:70%!ie; padding-left:" .. padleft .. 'px;') :done() :tag('div') :addClass('icon') :cssText("display:table-cell; vertical-align:middle;") :wikitext(icon) :done() :done() :done() return title end
function p.buildnavbox(params) local class = "overflow nav " .. (params.class or ) local style = params.style or {['border-width'] = '1px'} if not style['border-color'] then style['border-color'] = maincolor end local previousval = templatedata[params.previousparameter] local nextval = templatedata[params.nextparameter] local navbox = mw.html.create('p') :addClass(class) :css(style) :tag('span') :addClass('prev_bloc') :wikitext(previousval) :done() :tag('span') :addClass('next_bloc') :wikitext(nextval) :done() :done() return navbox end
function p.buildimages(params) local images = {} if type(params.imageparameters) == 'string' then params.imageparameters = {params.imageparameters} end if not params.imageparameters then -- s'il n'y a pa de paramètre image, continuer, peut-être y-a-t-il une image par défaut définie dans le module d'infobox params.imageparameters = {} end for j, k in ipairs(params.imageparameters) do table.insert(images, templatedata[k]) end if #images == 0 and item then if params.property then images = wikidata.getClaims{item= item, property = params.property} or {} if #images > 0 then addwikidatacat(params.property) end end if params.wikidata then images = params.wikidata() if type(images) == 'string' then return images end -- c'est probablement une erreur dans la requête => afficher le message end end if ((not images) or (#images == 0)) and params.defaultimages then images = params.defaultimages if type(images) == 'string' then images = {images} end end if (not images) or #images == 0 then if params.maintenancecat then local maintenancecat = getvalue(params.maintenancecat, params) addmaintenancecat(maintenancecat, params.sortkey) end return end -- extrait les images des valeurs Wikidata if type(images[1]) == 'table' then for i, j in pairs(images) do if j.mainsnak.snaktype ~= 'value' then return end if i > (params.numval) then images[i] = nil else images[i] = j.mainsnak.datavalue.value end end end local captiontext = templatedata[params.captionparameter] or params.defaultcaption local size = templatedata[params.sizeparameter] or params.defaultsize or '280x400px' local numsize = mw.ustring.gsub(size, 'x.*', ) numsize = numsize:gsub('px', ) numsize = tonumber(numsize) if type(numsize) ~= 'number' or numsize > 280 then addmaintenancecat("taille d'image invalide") end if type(captiontext) == 'function' then captiontext = captiontext(templatedata) end local style = params.style or {padding ='2px 0',}
-- Partie image
local imagesString = for i,image in pairs(images) do if image == '-' then return end imagesString = imagesString .. 'upright=' .. ( 1 / #images ) end imagesString = imagesString .. ''
end
local image = mw.html.create('div') :addClass("images") :css(style) :wikitext(imagesString)
-- Partie légende local caption = mw.html.create('p') if captiontext then caption :wikitext(captiontext) :css(params.style or {}) :addClass("legend") :done() end return mw.html.create('div'):node(image):node(caption):done() end
function p.buildtext(params) local class = params.class or local style = defaultstyle['boldline'] if params.style then for i, j in pairs(params.style) do style[i] = j end end local text = getvalue(params.value, params) or getWikidataValue(params) or params.defaultvalue if text == '-' then return end if not text then addmaintenancecat(params.maintenancecat, params.sortkey) return nil end local formattedtext = mw.html.create('p') :addClass(class) :css(style) :wikitext(text) :done() return formattedtext end
function p.buildrow(params) local class = params.class or local style = params.style or {} local value = getvalue(params.value, params) local label
if not value then value = getWikidataValue(params, 'wikidata') if (value and params.wikidatalabel) then label = getWikidataValue(params, 'wikidatalabel') end end
if not value then value = params.defaultvalue end
if value == '-' then return nil end
if not value then if params.maintenancecat then local maintenancecat = getvalue(params.maintenancecat, params) addmaintenancecat(maintenancecat, params.sortkey) end return nil end
if not label then label = params.label if type(label) == 'function' then label = label(templatedata, item) end end
-- format
-- Le '\n' est requis lorsque value est une liste commençant par '*' ou '#'
local formattedvalue = mw.html.create('div') :wikitext('\n' .. value)
if (params.hidden == true)then formattedvalue :attr({class="NavContent", style="display: none; text-align: left;"}) formattedvalue = mw.html.create('div') :attr({class="NavFrame", title="[Afficher]/[Masquer]", style="border: none; padding: 0;"}) :node(formattedvalue) end formattedvalue = mw.html.create('td') :node(formattedvalue) :allDone()
local formattedlabel if label then formattedlabel = mw.html.create('th') :attr('scope', 'row') :wikitext(label) :done() end local row = mw.html.create('tr') :addClass(class) :css(style) :node(formattedlabel) :node(formattedvalue) :done()
return row end
function p.buildtable(params) local tab = mw.html.create('table') local title if params.title then local text if type(params.title) == 'string' then -- raccourci moche mais pratique : n'utiliser qu'une chaîne pour le titre text = params.title params.title = {} else text = params.title.value or error('no value provided for this title') end local style = params.title.style or {['text-align'] = 'center', color = '000000'} if not style['background-color'] then style['background-color'] = maincolor end local colspan = params.title.colspan or '2' title = mw.html.create('caption') :attr({colspan = 2}) :css(style) :wikitext(text) :done() end local rows = {} -- does not add the rows directly to tab: check if some rows are non empty beoforehand so that we do not add a title if there are no data to show
for k, l in pairs(params.rows) do if type(l) == 'table' and l.type == 'multi' then -- when a single function is used for return several rows table.remove(params.rows, k) local count = 0 for m, n in pairs(l.rows) do table.insert(params.rows, k + count, n) count = count + 1 end l = params.rows[k] end
if type(l) == 'function' then --accepte les fonctions qui retournent des tables l = l(templatedata, item) end if type(l) == 'nil' then --ne rien faire (quand la valeur est originellemenet une fonctin elle peut retourner nil) elseif type(l) ~= 'table' then return error('les lignes d\'infobox ("rows") doivent être des tables, est ' .. type(l)) else local row = p.buildblock(l) table.insert(rows, row) end end if title and (#rows > 0) then tab:node(title) end if #rows > 0 then for i, j in pairs (rows) do tab:node(j) end end tab:allDone() return tab end
function p.buildinvalidblock(args) addmaintenancecat(defaultcat) local text = if type(args) ~= 'table' then text = "Les blocs d'infobox doivent être des tables" else text = i18n["invalid block type"] .. ' : ' .. (args.type or '??') end return text end
function p.buildmap(params) local maplist = getvalue(params.maps) local pointimage = params.pointimage local maptype = params.maptype -- choisit le type de carte le plus approprié (relief, administratif, etc.) if type(maplist) == 'function' then maplist = maplist(templatedata, item) end local width = tonumber(params.width) or 280 if width > 280 then addmaintenancecat("Erreur d'Infobox/Image trop grande") return 'image trop grande, la largeur doit être inférieure ou égale à 280px' end local latitude, longitude if type(params.latitude) == 'function' then latitude, longitude = params.latitude(templatedata, item), params.longitude(templatedata, item) else latitude, longitude = templatedata[params.latitude], templatedata[params.longitude] end if (not latitude or not longitude) and item and params.wikidata then if not type(params.wikidata) == 'function' then return error('valeur wikidata incorrecte pour la géolocalisation') end local val = params.wikidata(item, query) if not val then return nil end if (not type(val) == 'table') then return error("mauvaise requête pour les coordonnées de géolocalisation") end val = val[1] if val.mainsnak.snaktype == 'value' then latitude, longitude = val.mainsnak.datavalue.value.latitude, val.mainsnak.datavalue.value.longitude end end if not latitude or not longitude then return nil end local newparams = {maplist = maplist, pointimage = pointimage, width = width, item = item, latitude = latitude, longitude = longitude} if params.params and type(params.params) == 'table' then -- paramètres additionnels for i, j in pairs(params.params) do newparams[i] = j end end return mw.html.create('div'):addClass('images'):node(mapmod.multimap(newparams)):done() end
function p.buildfooter(params) if not params then params = {} end
local class = 'navbar noprint ' .. (params.class or ) local style = params.style or {} if not style['border-top-style'] then style['border-top-style'] = 'solid' end if not style['border-width'] then style['border-width'] = '1px' end if not style['border-color'] then style['border-color'] = maincolor end if not style['font-size'] then style['font-size'] = '80%' end local backlinkstr = '[' .. tostring( mw.uri.fullUrl( page.name, '&veaction=edit§ion=0' ) ) .. ' ' .. i18n['edit'] .. ']' .. ' - [' .. tostring( mw.uri.fullUrl( page.name, '&action=edit§ion=0' ) ) .. ' ' .. i18n['edit code'] .. ']'
local itemlinkstr if item then itemlinkstr = '' .. i18n['edit item'] .. '' end local editstr = backlinkstr if itemlinkstr then editstr = editstr .. ' - ' .. itemlinkstr end local editlinkspan = mw.html.create('span') :css({['text-align'] = "left"}) :addClass('plainlinks') :wikitext(editstr) :done()
local doclinkstr = '
'
-- si ce lien ne marche pas toujours, il faut ajouter un variable pour le nom de l'infobox récupéré par le frame
local doclinkspan = mw.html.create('span')
:css({['text-align'] = "right"})
:wikitext(doclinkstr)
:done()
local footer = mw.html.create('p') :addClass(class) :css(style) :node(editlinkspan) :node(doclinkspan) return footer end
function p.buildblock(block) local blocktypes = { -- maps a block type to the function building it ['invalid'] = p.buildinvalidblock, ['external text'] = p.buildexternaltext, ['footer'] = p.buildfooter, ['images'] = p.buildimages, ['map']= p.buildmap, ['mixed'] = p.buildrow, ['navbox'] = p.buildnavbox, ['table'] = p.buildtable, ['row'] = p.buildrow, ['text'] = p.buildtext, ['title'] = p.buildtitle, } local fun = blocktypes[block.type] or blocktypes['invalid'] return fun(block) end
function p._build(modulename, params)
-- fill-up global variable templatedata using params for i, j in pairs(params) do if j and mw.text.trim(j) ~= then -- empty parameters are ignored templatedata[i] = j end end
-- load wikidata item as a global variable: if templatedata.wikidata == 'non' then item = nil elseif (not templatedata.wikidata) or (templatedata.wikidata == ) then item = mw.wikibase.getEntityObject() else item = nil -- topic of the infobox corresponds to another Wikidata item, should eventually be fetched through arbitrary access infoboxrank = 'secondary' end
-- load infobox module page moduledata = require('Module:Infobox/' .. modulename) moduledata.name = modulename
-- guess "rank" of the infobox local infoboxrank = 'main' -- main infobox of the page, with coordinates displayed in title etc. if page.namespace ~= 0 then infoboxrank = 'secondary' end templatedata.infoboxrank = infoboxrank
-- adds style to infobox, basd on values defined in the module local style = moduledata.style or {} if not style['max-wdith'] then style['max-width'] = '300px' end local class = moduledata.class or "infobox_v3" local parts = moduledata.parts maincolor = templatedata['couleur infobox'] or moduledata.maincolor or "#E1E1E1" local footerparams = moduledata.footer or {} infobox:css(style) infobox:addClass(class)
-- build infobox, block by block for i, j in pairs(parts) do local block = p.buildblock(j) infobox:node(block) end local div = mw.html.create('div') infobox:node(div):node(tab) infobox :node(p.buildfooter(footerparams)) :done() return tostring(infobox) .. externaltext, maintenance end
function p.build(frame) local name = frame.args.nom local params = frame:getParent().args return p._build(name, params) end
return p