« Module:Infobox » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
correction
0x010D (discussion | contributions)
copie de la version test
Ligne 1 : Ligne 1 :
local Infobox = {}
local p = {}
local infobox = mw.html.create('div')
local wikidata = require('Module:Wikidata')
local maintenance = ''
local infoboxname, infoboxmodule
local localdata = {}
local pagename = mw.title.getCurrentTitle().prefixedText
 
 
local i18n = {
local i18n = {
    ['see doc'] = 'Documentation du modèle',
['see doc'] = 'Documentation du modèle',
    ['edit infobox'] = 'Modifier l\'infobox',
['edit infobox'] = 'modifier l\'infobox',
['edit item'] = 'modifier Wikidata'
}
}


function Infobox:new( args )
local function getWikidataValue(params) -- helper function
    -- Initialisation de l'object
if not item then
    local object = {
return nil
        text = "",
end
        isPart = false
params.item = item
    }
if params.wikidata then
    setmetatable(object, {
if type(params.wikidata) == 'function' then
        __index = Infobox,
return params.wikidata()
        __tostring = function( self ) return self:tostring() end
else
    })
return params.wikidata
end
elseif params.property then
return wikidata.formatStatements({property = params.property})
else
return nil
end
end


    if args.isPart then
local function buildtitle(params)
        object.isPart = true
local class = params.class or "entete"
    end
local style = params.style or {}
local text = localdata[params.textparameter] or params.textdefaultvalue or mw.title.getCurrentTitle().text
local title = mw.html.create('p')
:addClass(class)
:css(style)
:wikitext(text)
:done()
return title
end


    -- Ouverture de l'élément principal de l'infobox
local function buildimages(params)
    if object.isPart then
local images = {}
        return object -- On retourne si on construit seulement un module
for j, k in ipairs(params.imageparameters) do
    end
table.insert(images, localdata[k])
   
end
    local class = args.class
if #images == 0 then
    if class == nil or class == '' then
images =  getWikidataValue(params)
        class = "infobox_v3"
end
    end
if #images == 0 then
   
images = params.defaultimage
    local style = args.style
end
if not images then  
return nil
end
local captiontext = localdata[params.captionparameter] or params.defaultcaption
if type(captiontext) == 'function' then
captiontext = captiontext(localdata)
end
local div = mw.html.create('div')
local style = params.style or ''
local class = ''
if  #images == 2 then
class = image2
end
-- Partie image


    local str = '<div class="' .. class .. '"'
local imagesString = ''
    if args.style then
for i,image in pairs(images) do
    str = str .. ' style= "' .. table.concat( args.style, '; ' ) .. '"'
imagesString = imagesString ..  '[[Fichier:' .. image .. '|thumb'
if #images == 1 then
imagesString = imagesString .. '|center'
end
if image.upright then
imagesString = imagesString .. '|upright=' .. image.upright
else
imagesString = imagesString .. '|upright=' .. ( 1 / #images )
end
        imagesString = imagesString .. ']]'
     end
     end


object.text = str .. '>'
     div:wikitext(imagesString)
 
     return object
end


function Infobox:addTitle( args )
-- Partie légende
    local str = '<p'
local caption = mw.html.create('p')
    if args.class then
if captiontext then
        str = str .. ' class= "' .. args.class ..'"'
caption
    else
:wikitext(captiontext)
        str = str .. ' class= "entete"'
:css(params.style or {})
    end
:addClass("legend")
:done()
div:node(caption)
end


    if args.style then
return div
        str = str .. ' style= "' .. table.concat( args.style, '; ' ) .. '"'
    end
    str = str .. '>'
   
    if args.text and args.text ~= '' then
        str = str .. args.text
    else
        str = str .. mw.title.getCurrentTitle().text
    end
   
    self.text = self.text .. str .. '</p>'
end
end


function Infobox:addText( args )
local function buildtext(params)
    local str = '<p'
local class = params.class or ''
    if args.class then
local style = params.style or {}
        str = str .. ' class= "' .. args.class ..'"'
local text = localdata[params.value] or getWikidataValue(params) or params.defaulttext
    end
   
if not text then
    if args.style then
return nil -- à ajouter : option cat de maintenance
        str = str .. ' style= "' .. table.concat( args.style, '; ' ) .. '"'
end
    end
local formattedtext = mw.html.create('p')
    str = str .. '>'
:addClass(class)
   
:css(style)
    if args.text then  
:wikitext(text)
        str = str .. args.text
:done()
    end
return formattedtext
   
    self.text = self.text .. str .. '</p>'
end
end


function Infobox:openTable( args )
local function buildmixedrow(params)
    local str = '<table'
local class = params.class or ''
    if args.class then
local style = params.style or {}
        str = str .. ' class="' .. args.class .. '"'
local value
    end
-- retrieve value
if type(params.value) == 'function' then
local raw = params.value
value = raw(localdata)
elseif type(params.value) == 'string' then
value = localdata[params.value]
end
if not value then
value =  getWikidataValue(params) or params.defaultvalue
end
local label = params.label
if not value then
return nil -- todo: maintenance
end


    if args.style then
-- formatting
        str = str .. ' style= "' .. table.concat( args.style, '; ' ) .. '"'
local formattedvalue = mw.html.create('div')
    end
:wikitext(value)
    str = str .. '>'
   
if (params.hidden == true)then
    if args.text and args.text ~= '' then
formattedvalue
        str = str .. '<caption'
:attr({class="NavContent", style="display: none; text-align: left;"})
        if args.styleCaption then
formattedvalue = mw.html.create('div')
            str = str .. ' style= "' .. table.concat( args.styleCaption, '; ' ) .. '"'
:attr({class="NavFrame", title="[Afficher]/[Masquer]", style="border: none; padding: 0;"})
        end
:node(formattedvalue)
        str = str .. '>' .. args.text .. '</caption>'
end
    end
formattedvalue =  mw.html.create('td')
:node(formattedvalue)
:allDone()
local formattedlabel = mw.html.create('th')
:attr('scope', 'row')
:wikitext(label)
:done()


    self.text = self.text .. str
local row = mw.html.create('tr')
:addClass(class)
:css(style)
:node(formattedlabel)
:node(formattedvalue)
:done()
return row
end
end


function Infobox:closeTable( args )
local function buildtable(params)
    self.text = self.text .. '</table>'
local tab = mw.html.create('table')
local title
if params.title then
local text = params.title.value or error('no value provided for this title')
local style = params.title.style or {['text-align'] = 'center', ['background-color'] = "E1E1E1", color = '000000'}
local colspan = params.title.colspan or '2'
title = mw.html.create('th')
: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 == 'mixed' then
local row = buildmixedrow(l)
if row then
table.insert(rows, row)
end
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
end


function Infobox:addMixedRow( args )
local function buildmap(params) -- A faire
    if not args.value or args.value == '' then
local image = 'France_location_map-Regions_and_departements.svg'
        if args.cat then
local image2 = 'France_location_map-Regions_and_departements.svg'
            self.text = self.text .. '[[Catégorie:' .. args.cat .. ']]'-- ajoute catégorie de maintenance
        end
        return
    end
    if not args.label then
        self.text = self.text .. '<tr><td class="error">Le paramètre label n\'est pas renseigné.</td></tr>'
        return
    end
   
    local str = '<tr><th scope="row"'
    if args.styleHeader then
        str = str .. ' style= "' .. table.concat( args.styleHeader, '; ' ) .. '"'
    end


    self.text = self.text .. str .. '>' .. args.label .. '</th><td>'
local map1 = mw.html.create('div')
   
:addClass("geobox")
    if args.hidden and args.hidden == 1 then  -- option caché sélectionné: affichage de la valeur sera masqué
:tag('table')
        self.text = self.text .. '<div class="NavFrame" title="[Afficher]/[Masquer]" style="border: none; padding: 0;"><div class="NavContent" style="display: none; text-align: left;">' .. args.value ..'</div></div></td></tr>'
:addClass('DebutCarte')
    elseif args.weblink and args.weblink ~= '' then  -- option lien externe
:attr({border="0", cellspacing="0", cellpadding="0"})
        self.text = self.text .. '<span class="reflink plainlinksneverexpand">[' .. args.weblink .. args.value .. ' ' .. args.value .. ']</span></td></tr>'
:css({margin = '0', border = 'none', padding = '0', ['text-align'] = 'center'})
    else
:tag('tr')
        self.text = self.text .. args.value .. '</td></tr>' -- affichage normal de la valeur
:tag('td')
    end
:tag('div')
:css({position= 'relative', margin = "auto"})
:wikitext('[[Fichier:' .. image .. '|alt=(Voir situation sur carte : [[{{Géolocalisation/{{{1}}}|name|{{{type|}}}}}]])|{{{alt}}}]]')
:done()
:done()
:done()
:done()
local map1 = mw.html.create('div')
:addClass("geobox")
:tag('table')
:addClass('DebutCarte')
:attr({border="0", cellspacing="0", cellpadding="0"})
:css({margin = '0', border = 'none', padding = '0', ['text-align'] = 'center'})
:tag('tr')
:tag('td')
:tag('div')
:css({position= 'relative', margin = "auto"})
:wikitext('[[Fichier:' .. image2 .. '|250px|(Voir situation sur carte : [[{{Géolocalisation/{{{1}}}|name|{{{type|}}}}}]])|{{{alt}}}]]')
:done()
:done()
:done()
:done()
local div = mw.html.create('div')
:addClass("img_toogle")
:node(map1):done()
:node(map2):done()
:allDone()
return div
end
end


function Infobox:addDoubledRow( args )
local function buildfooter(params)
    if not args.value or args.value == '' then
if not params then params = {} end
        if args.cat then
            self.text = self.text .. '[[Catégorie:' .. args.cat .. ']]'-- ajoute catégorie de maintenance
local class = params.class or 'navbar noprint'
        end
local style = params.style or {border = "1px"}
        return
 
    end
local backlinktext = '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit&section=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']'
    if not args.label then
local backlink = mw.html.create('td')
        self.text = self.text .. '<tr><td class="error">Le paramètre label n\'est pas renseigné!!!</td></tr>'
:wikitext(backlinktext)
        return
:addClass('plainlinks')
    end
:done()
   
    local str = '<tr><td scope="row" colspan="2"'
local itemlinktext = ''
    if args.styleHeader then
if item then
        str = str .. ' style= "' .. table.concat( args.styleHeader, '; ' ) .. '"'
itemlinktext = '[[d:' .. item.id .. '|' .. i18n['edit item'] .. ']]'
    end
end
   
local itemlink = mw.html.create('td')
    str = str .. '>' .. args.label .. '</td></tr><tr><td scope="row" colspan="2"'
:wikitext(itemlinktext)
    if args.styleValue then
:done()
        str = str .. ' style= "' .. table.concat( args.styleValue, '; ' ) .. '"'
    end
local doclinktext = '[[Image:Gtk-dialog-info.svg|12px|link=' .. infoboxname .. '|' .. i18n['see doc'] .. ']]'
   
local doclink = mw.html.create('td')
    self.text = self.text .. str .. '>' .. args.value ..'</td></tr>'
:css({['text-align'] = "right"})
:wikitext(doclinktext)
:done()
local footer = mw.html.create('table')
:tag('tr')
:css({['font-size'] = '80%'})
:node(backlink)
:node(itemlink)
:node(doclink)
:done()
:done()
return footer
end
end


function Infobox:addImages( args )
function p._build(localinfoboxmodule)
    --Get images
-- analyse le module d'infobox
    local images = {}
local style = localinfoboxmodule.style or {}
    if not args.images then
local class = localinfoboxmodule.class or "infobox_v3"
        return
local parts = localinfoboxmodule.parts
    end
local footerparams = localinfoboxmodule.footer or {}
    for i,conf in pairs( args.images ) do
infobox:css(style)
        if conf.name and conf.name ~= '' then
infobox:addClass(class)
            table.insert( images, conf )
        end
    end
 
    if not next( images ) then
        return --Pas d'images
    end
    local imagesCount = table.maxn( images )
 
    local str = ''
    if imagesCount == 2 then
        str = '<div class="image2"'
        if args.background then
            str = str .. ' style="background: ' .. args.background .. ';"'
        end
        str = str .. '>'
    end
 
    for i,image in pairs( images ) do
        -- hack pour enlever puis remettre la catégorie de tracking Wikidata du nom de fichier
        local t = mw.text.split( image.name, '[[', true )
        local imagename = t[1]
        local cat = ''
        if t[2] then
            cat = '[[' .. t[2]
        end
        str = str .. cat ..  '[[Fichier:' .. imagename .. '|thumb'
        if imagesCount == 1 then
            str = str .. '|center'
        end
        if image.upright then
            str = str .. '|upright=' .. image.upright
        else
            str = str .. '|upright=' .. ( 1 / imagesCount )
        end
        if image.alt then
            str = str .. '|alt=' .. image.alt
        else
            str = str .. '|alt=Description de '
            if args.legend then
                str = str .. 'cette image, également commentée ci-après'
            else
                str = str .. 'l\'image ' .. image.name
            end
        end
        str = str .. ']]'
    end
 
    if imagesCount == 2 then
        str = str .. '</div>'
    end


    if args.legend then
for i, j in pairs(parts) do
        str = str .. '<p class="legend">' .. args.legend .. '</p>'
if type(j) ~= 'table' then
    end
return 'error'
   
elseif j.type == 'title' then
    self.text = self.text .. str
infobox:node(buildtitle(j))
elseif j.type == 'images' then
infobox:node(buildimages(j))
elseif j.type == 'text' then
infobox:node(buildtext(j))
elseif j.type == 'map' then
infobox:node(buildmap(j))
elseif j.type == 'table' then
infobox:node(buildtable(j))
end
end
local div = mw.html.create('div')
infobox:node(div):node(tab)
infobox
:node(buildfooter(footerparams))
:done()
return tostring(infobox) .. maintenance
end
end


function Infobox:addFooter( args )
function p.build(frame)
    local templatename = args.templatename
-- set variables
    local pagename = mw.title.getCurrentTitle().prefixedText
infoboxname = 'Module:InfoboxBuilder/' .. frame.args.nom
    local class = args.class or ''
localinfoboxmodule = require(infoboxname)
    local itemlink = ''
    if args.item then
-- get the localinfoboxmodule parameter values and clen them up
        itemlink = ' - [[d:' .. args.item .. '|modifier l\'élément Wikidata]]'
for i, j in pairs(frame:getParent().args) do
    end
if j and mw.text.trim(j) ~= '' then
    text = '<p class="navbar noprint ' .. class .. '" style="1px;">'  
localdata[i] = j
        .. '<span class="plainlinks">'
end
        .. '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit&section=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']'
end
        .. itemlink
-- fetch wikidata item and assign it to the item global variable
        .. '</span>'
if localdata.wikidata == 'non' then
        .. '[[Image:Gtk-dialog-info.svg|12px|link=' .. templatename .. '|' .. i18n['see doc'] .. ']]'
item = nil
        .. '</p>'
elseif (not localdata.wikidata) or (localdata.wikidata == '') then
    self.text = self.text .. text
item = mw.wikibase.getEntityObject()
end
else
 
item = mw.wikibase.getEntityObject() -- todo: fetch arbitrary item and check for errors
function Infobox:tostring()
end
    if self.isPart then
-- build infobox
        return self.text
return p._build(localinfoboxmodule)
    else
        return self.text .. '</div>'
    end
end
end


--Create a style property value from an array CSS property = CSS value
--function formatStyle( args )
--    local elems = {}
--    for key, val in pairs( args ) do
--        table.insert( elems, key .. ':' .. val )
--    end
--    return table.concat( elems, '; ' )
--end
local p = {}
function p.new( args )
    return Infobox:new( args )
end
function p.test()
    local a = Infobox:new( {} )
    a:addTitle( {} )
    a:addImages( {
        images = {
            {
                name = 'Pellicule.jpg'
            },
            {
                name = 'Pellicule.jpg'
            }
        },
        legend = 'legend'
    } )
    a:openTable( {} )
    a:addMixedRow( {
        label = 'Test',
        value = 'test'
    } )
    a:closeTable( {} )
    return tostring(a)
end
return p
return p

Version du 9 mai 2014 à 12:12

local p = {} local infobox = mw.html.create('div') local wikidata = require('Module:Wikidata') local maintenance = local infoboxname, infoboxmodule local localdata = {} local pagename = mw.title.getCurrentTitle().prefixedText


local i18n = { ['see doc'] = 'Documentation du modèle', ['edit infobox'] = 'modifier l\'infobox', ['edit item'] = 'modifier Wikidata' }

local function getWikidataValue(params) -- helper function if not item then return nil end params.item = item if params.wikidata then if type(params.wikidata) == 'function' then return params.wikidata() else return params.wikidata end elseif params.property then return wikidata.formatStatements({property = params.property}) else return nil end end

local function buildtitle(params) local class = params.class or "entete" local style = params.style or {} local text = localdata[params.textparameter] or params.textdefaultvalue or mw.title.getCurrentTitle().text local title = mw.html.create('p') :addClass(class) :css(style) :wikitext(text) :done() return title end

local function buildimages(params) local images = {} for j, k in ipairs(params.imageparameters) do table.insert(images, localdata[k]) end if #images == 0 then images = getWikidataValue(params) end if #images == 0 then images = params.defaultimage end if not images then return nil end local captiontext = localdata[params.captionparameter] or params.defaultcaption if type(captiontext) == 'function' then captiontext = captiontext(localdata) end local div = mw.html.create('div') local style = params.style or local class = if #images == 2 then class = image2 end -- Partie image

local imagesString = for i,image in pairs(images) do imagesString = imagesString .. 'upright=' .. ( 1 / #images ) end imagesString = imagesString .. ''

   end
   div:wikitext(imagesString)

-- Partie légende local caption = mw.html.create('p') if captiontext then caption :wikitext(captiontext) :css(params.style or {}) :addClass("legend") :done() div:node(caption) end

return div end

local function buildtext(params) local class = params.class or local style = params.style or {} local text = localdata[params.value] or getWikidataValue(params) or params.defaulttext

if not text then return nil -- à ajouter : option cat de maintenance end local formattedtext = mw.html.create('p') :addClass(class) :css(style) :wikitext(text) :done() return formattedtext end

local function buildmixedrow(params) local class = params.class or local style = params.style or {} local value

-- retrieve value if type(params.value) == 'function' then local raw = params.value value = raw(localdata) elseif type(params.value) == 'string' then value = localdata[params.value] end

if not value then value = getWikidataValue(params) or params.defaultvalue end

local label = params.label if not value then return nil -- todo: maintenance end

-- formatting local formattedvalue = mw.html.create('div') :wikitext(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 = mw.html.create('th') :attr('scope', 'row') :wikitext(label) :done()

local row = mw.html.create('tr') :addClass(class) :css(style) :node(formattedlabel) :node(formattedvalue) :done()

return row end

local function buildtable(params) local tab = mw.html.create('table') local title if params.title then local text = params.title.value or error('no value provided for this title') local style = params.title.style or {['text-align'] = 'center', ['background-color'] = "E1E1E1", color = '000000'} local colspan = params.title.colspan or '2' title = mw.html.create('th') :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 == 'mixed' then local row = buildmixedrow(l) if row then table.insert(rows, row) end 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

local function buildmap(params) -- A faire local image = 'France_location_map-Regions_and_departements.svg' local image2 = 'France_location_map-Regions_and_departements.svg'

local map1 = mw.html.create('div') :addClass("geobox") :tag('table') :addClass('DebutCarte') :attr({border="0", cellspacing="0", cellpadding="0"}) :css({margin = '0', border = 'none', padding = '0', ['text-align'] = 'center'}) :tag('tr') :tag('td') :tag('div') :css({position= 'relative', margin = "auto"}) :wikitext('(Voir situation sur carte : [[{{Géolocalisation/{{{1}}}') :done() :done() :done() :done() local map1 = mw.html.create('div') :addClass("geobox") :tag('table') :addClass('DebutCarte') :attr({border="0", cellspacing="0", cellpadding="0"}) :css({margin = '0', border = 'none', padding = '0', ['text-align'] = 'center'}) :tag('tr') :tag('td') :tag('div') :css({position= 'relative', margin = "auto"}) :wikitext('{{{alt}}}') :done() :done() :done() :done() local div = mw.html.create('div') :addClass("img_toogle") :node(map1):done() :node(map2):done() :allDone() return div end

local function buildfooter(params) if not params then params = {} end

local class = params.class or 'navbar noprint' local style = params.style or {border = "1px"}

local backlinktext = '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit&section=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']' local backlink = mw.html.create('td') :wikitext(backlinktext) :addClass('plainlinks') :done()

local itemlinktext = if item then itemlinktext = '' .. i18n['edit item'] .. '' end local itemlink = mw.html.create('td') :wikitext(itemlinktext) :done()

local doclinktext = '' .. i18n['see doc'] .. '' local doclink = mw.html.create('td') :css({['text-align'] = "right"}) :wikitext(doclinktext) :done()

local footer = mw.html.create('table') :tag('tr') :css({['font-size'] = '80%'}) :node(backlink) :node(itemlink) :node(doclink) :done() :done() return footer end

function p._build(localinfoboxmodule) -- analyse le module d'infobox local style = localinfoboxmodule.style or {} local class = localinfoboxmodule.class or "infobox_v3" local parts = localinfoboxmodule.parts local footerparams = localinfoboxmodule.footer or {} infobox:css(style) infobox:addClass(class)

for i, j in pairs(parts) do if type(j) ~= 'table' then return 'error' elseif j.type == 'title' then infobox:node(buildtitle(j)) elseif j.type == 'images' then infobox:node(buildimages(j)) elseif j.type == 'text' then infobox:node(buildtext(j)) elseif j.type == 'map' then infobox:node(buildmap(j)) elseif j.type == 'table' then infobox:node(buildtable(j)) end end local div = mw.html.create('div') infobox:node(div):node(tab) infobox :node(buildfooter(footerparams)) :done() return tostring(infobox) .. maintenance end

function p.build(frame) -- set variables infoboxname = 'Module:InfoboxBuilder/' .. frame.args.nom localinfoboxmodule = require(infoboxname)

-- get the localinfoboxmodule parameter values and clen them up for i, j in pairs(frame:getParent().args) do if j and mw.text.trim(j) ~= then localdata[i] = j end end -- fetch wikidata item and assign it to the item global variable if localdata.wikidata == 'non' then item = nil elseif (not localdata.wikidata) or (localdata.wikidata == ) then item = mw.wikibase.getEntityObject() else item = mw.wikibase.getEntityObject() -- todo: fetch arbitrary item and check for errors end -- build infobox return p._build(localinfoboxmodule) end

return p