Module:Databox

La bibliothèque libre.

La documentation pour ce module peut être créée à Module:Databox/Documentation

local p = {}

function p.databox(frame)
    local args = frame:getParent().args
    local itemId = nil
    if args.item then
        itemId = args.item
    end
    local lang = mw.language.getContentLanguage()
    local item = mw.wikibase.getEntity(itemId)
    local edit_message = mw.message.new('vector-view-edit'):plain()
    
    if item == nil then
        mw.addWarning("Wikidata item not found")
        return ""
    end
    
    local databoxRoot = mw.html.create('div')
        :addClass('infobox')
        :css({
            float = 'right',
            border = '1px solid #aaa',
            ['max-width'] = '300px',
            padding = '0 0.4em',
            margin = '0 0 0.4em 0.4em',
        })

    --Title
    databoxRoot:tag('div')
        :css({
            ['text-align'] = 'center',
            ['background-color'] = '#f5f5f5',
            padding = '0.5em 0',
            margin = '0.5em 0',
            ['font-size'] = '120%',
            ['font-weight'] = 'bold',
        })
        :wikitext(item:getLabel() or mw.title.getCurrentTitle().text)

     --Image
    local images = item:getBestStatements('P18')
    if #images >= 1 then
        databoxRoot
            :tag('div')
            :wikitext('[[File:' .. images[1].mainsnak.datavalue.value .. '|frameless|300px]]')
    end

    --Table
    local dataTable = databoxRoot
        :tag('table')
        :css({
            ['text-align'] = 'left',
            ['font-size'] = '90%',
            ['word-break'] = 'break-word',
            ['width'] = '100%',
            ['table-layout'] = 'fixed',
        })
    dataTable:tag('caption')
             :css({
             	['background-color'] = '#f5f5f5',
             	['font-weight'] = 'bold',
             	['margin-top'] = '0.2em',
             })
    		 :wikitext(item:formatStatements('P31').value .. ' [[File:OOjs UI icon edit-ltr.svg|' .. edit_message .. '|12px|baseline|class=noviewer|link=https://www.wikidata.org/wiki/' .. item.id .. '#P31]]')
  
    local properties = mw.wikibase.orderProperties(item:getProperties())
    for _, property in pairs(properties) do
        local datatype = item.claims[property][1].mainsnak.datatype
        local valueCount = #item:getBestStatements(property)
        if property ~= 'P31' and datatype ~= 'commonsMedia' and datatype ~= 'wikibase-property' and datatype ~= 'geo-shape' and datatype ~= 'tabular-data' and valueCount > 0 then
            local propertyValue = item:formatStatements(property)
            dataTable:tag('tr')
                :tag('th')
                    :attr('scope', 'row')
                    :css('vertical-align', 'top')
                    :wikitext(lang:ucfirst(propertyValue.label)):done()
                :tag('td')
                    :wikitext(frame:preprocess(propertyValue.value))
                    :wikitext(' [[File:OOjs UI icon edit-ltr.svg|' .. edit_message .. '|12px|baseline|class=noviewer|link=https://www.wikidata.org/wiki/' .. item.id .. '#' .. property .. ']]')
        end
    end
    return tostring(databoxRoot)
end

return p