Module:Infobox Wikidata
Jump to navigation
Jump to search
Uses Lua: |
This is a Lua Module that is invoked by {{Infobox Wikidata}}
.
Methods
[edit]build
main
Code
--[[ Module building an infobox
submodules
content: defines the content of the rows
common:
]]--
local p = {}
local wikidata = require('Module:Wikidata')
local style = require('Module:Infobox Wikidata/Style')
local infobox = mw.html.create('div')
local maintenance = ''
local lang, item, itemID, content, modulename -- set by the main function
local i18n = { -- should go to a separate i18n page
['see module'] = 'see module code',
['edit item'] = 'edit item'
}
local function getvalue(value) -- helper function
if type(value) == 'function' then
return value(item, lang)
elseif type(value) == 'string' then
return wikidata._formatStatements({property = value, entity = item, lang = lang})
else
return error()
end
end
local function buildtitle(params)
local style = params.style or style.entete
local text = item:getLabel(lang)
local title = mw.html.create('p')
:css(style)
:wikitext(text)
:done()
return title
end
local function buildnavbox(params) --does not work
local class = params.class or "overflow nav"
local style = params.style or {['border-width'] = '1px'}
local previousval = getvalue(params.previous)
local nextval = getvalue(params['next'])
local navbox =
mw.html.create('p')
:css(style)
:tag('span')
:addClass('prev_bloc')
:wikitext(previousval)
:done()
:tag('span')
:addClass('next_bloc')
:wikitext(nextval)
:done()
:done()
return navbox
end
local function buildimages(params)
images = getvalue(params.value)
if (not images) or #images == 0 then
return nil
end
local captiontext = ''
local div = mw.html.create('div')
local style = params.style or ''
local size = params.size or '220px'
local class = ''
if #images == 2 then
class = image2
end
-- Partie image
local imagesString = ''
for i,image in pairs(images) do
imagesString = imagesString .. '[[File:' .. image .. '|' .. size
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
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 = getvalue(params.value)
if not text then
return nil -- à ajouter : option cat de maintenance
end
local formattedtext = mw.html.create('tr'):tag('td')
:addClass(class)
:css(style)
:wikitext(text)
:done()
return formattedtext
end
local function buildmixedrow(params)
local value = getvalue(params.value)
if not value then
return nil
end --or params.defaultvalue
local label
if params.label then
label = getvalue(params.label)
end
if not label then
label = wikidata._getLabel(params.labelitem, lang)
end
local class = params.class
local rowstyle = params.style or {}
local labelstyle = params.labelstyle or style.rowlabel
local valuestyle = params.valuestyle or style.rowvalue
-- formatting
local formattedvalue = mw.html.create('div')
:wikitext(value)
:css(valuestyle)
if (params.hidden == true)then
formattedvalue
:attr('class', 'mw-collapsible-content')
formattedvalue = mw.html.create('div')
:attr('class', 'mw-collapsible mw-collapsed')
:node(formattedvalue)
end
formattedvalue = mw.html.create('td')
:node(formattedvalue)
:css(valuestyle)
:allDone()
local formattedlabel = mw.html.create('th')
:attr('scope', 'row')
:css(labelstyle)
:wikitext(label)
:done()
local row = mw.html.create('tr')
:addClass(class)
:css(rowstyle)
: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 style.tabletitle
title = mw.html.create('th')
:css(style)
:attr('colspan', '2') --does not work
: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' then return error('infobox table rows should be table (is' .. l.type .. ')') end
local row
if l.type == 'mixed' then
row = buildmixedrow(l)
elseif (l.type == 'text') then
row = buildtext(l)
else
-- return error('unsupported row type in infobox: ' .. l.type)
end
if row then
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
local function buildmap(params) -- does not work
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('[[File:' .. 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('[[File:' .. 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
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 itemlinktext = ''
if item then
itemlinktext = '[[d:' .. item.id .. '|' .. i18n['edit item'] .. ']]'
end
local itemlink = mw.html.create('td')
:wikitext(itemlinktext)
:done()
local doclinktext = '[[Module:' .. modulename .. '|' .. i18n['see module'] .. ']]'
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(doclink)
:node(itemlink)
:done()
:done()
return footer
end
function p.main()
-- find most appropriate infobox module
local isinstance = require('Module:Wikidata/utils').isinstance
if isinstance(item, 'Q5') then
modulename = 'Infobox Wikidata/Human'
elseif isinstance(item, 'Q2221906') then
modulename = 'Infobox Wikidata/Place'
else
return "type of item not supported"
end
if modulename then
content = require('Module:' .. modulename)
end
-- analyz
local style = style.div
local parts = content.parts
local footerparams = content.footer or {}
infobox:css(style)
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 == 'map' then
infobox:node(buildmap(j))
elseif j.type == 'navbox' then
infobox:node(buildnavbox(j))
elseif j.type == 'table' then
infobox:node(buildtable(j))
else
return error('unsupported infobox element: ' .. j.type)
end
end
infobox
:node(buildfooter(footerparams))
:done()
return tostring(infobox) .. maintenance
end
function p.build(frame)
-- set the item and language variables for the whole module
itemID = frame.args.item
lang = frame.args.lang
if not lang or lang == '' then
lang = frame:preprocess('{{int:lang}}')
end
item = mw.wikibase.getEntityObject(itemID)
if not item then return
"bad item ID"
end
-- build infobox
return p.main()
end
return p