Documentation for this module may be created at Module:Sandbox/Gonnym/infoboxFR/doc
--https://fr.wikipedia.org/wiki/Module:Infobox
--https://fr.wikipedia.org/wiki/Module:Infobox/Localdata
--https://fr.wikipedia.org/wiki/Module:Infobox/Fonctions
-- Imported modules
local string2 = require('Module:String2')
local wikidata = require('Module:Wikidata')
local valueExpl = wikidata.translate("activate-query")
local p = {}
local item = nil -- The Wikidata element
local localData = {} -- Data about parameters passed to template
local page = { -- Data about the page where the infobox is placed
name = mw.title.getCurrentTitle().prefixedText,
namespace = mw.title.getCurrentTitle().namespace
}
local mainColor = '#E1E1E1'
local secondColor = '#E1E1E1'
local thirdColor = '#000000'
-- The main object to return
local infobox = mw.html.create('div')
-- The secondary objects to return
local maintenance = '' -- Maintenance categories
local externalText = '' -- Title coordinates?
local function addWikidataCategory(prop)
maintenance = maintenance .. wikidata.addTrackingCat(prop)
end
local function expandQuery(query)
-- local value, number - [seems this is not used and can be deleted]
if (not query.entity) then
query.entity = localData.item
end
if (not query.conjtype) then
query.conjtype = 'comma'
end
local claims = wikidata.getClaims(query)
if (not claims) then
return nil
end
return wikidata.formatAndCat(query), #claims -- for the plural agreement
end
local function getWikidataValue(params, wikidataParam)
-- Get the Wikidata value for the value, either in the "wikidata" parameter or in the "property" parameter
if (not localData.item) then
return nil
end
if (params.blockers) then -- Blockers are local parameters that disable the wikidata query -[TODO: does this belong in this function?]
local blockers = params.blockers
if (type(blockers) == 'string') then
blockers = {blockers}
end
for i, blocker in ipairs(blockers) do
if (localData[blocker]) then
return nil
end
end
end
local v -- The value to return
local valnum -- the number of values (for grammar agreement)
if (not wikidataParam) then -- By default the wikidata value is in the parameter "wikidata" but in the structures composed like "title", there are several wikidata parameters
wikidataParam = 'wikidata'
end
if (params[wikidataParam]) then
if (type(params[wikidataParam]) == 'function') then
v, valnum = params[wikidataParam](localData.item)
elseif (type(params[wikidataParam]) == 'table') then
v, valnum = expandQuery(params[wikidataParam])
else
v, valnum = params[wikidataParam]
end
end
if (not v) then
return nil
end
v = string2.ucfirst(v)
return v, valnum
end
local function getValue(val, params)
if (type(val) == 'string') then
return localData[val]
elseif (type(val) == 'function') then
return val(localData, localData.item, params)
elseif (type(val) == 'table') then
for i, j in pairs(val) do -- if several possible settings (old legacy code), take the first non-empty [TODO: if this is legacy code, then we shouldn't have it]
if (localData[j]) then
return localData[j]
end
end
end
end
local function addMaintenanceCategory(category, sortkey)
if (page.namespace ~= 0) then --TODO: could probably change the next if be nested if this is changed to equal
return ''
end
if (category) then
maintenance = maintenance .. '[[Category:' .. category .. '|' .. (sortkey or page.name) .. ']]'
end
end
function p.separator(params)
local style = params['separator style'] or {}
style.height = style.height or '2px'
style['background-color'] = style['background-color'] or mainColor
return mw.html.create('hr'):css( style )
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 .. '<br /><small>' .. subtext .. '</small>' --TODO: small should probably be removed
end
local icon = params.icon or ''
if ((icon ~= '') and (icon ~= 'defaut')) then
text = text .. mw.getCurrentFrame():extensionTag('templatestyles', '', {src = 'Infobox/Pictogramme/' .. mw.text.trim(icon) .. '.css'}) --TODO: this probably needs to be changed
if (not params.large) then
icon = 'icon ' .. icon
end
end
local class = 'entete ' .. icon
-- overwrites with those provided in the module
local style = {}
style['background-color'] = mainColor
style['color'] = thirdColor
if (params.style) then
for i, j in pairs(params.style) do
style[i] = j
end
end
local title = mw.html.create('div')
:addClass(class)
:css(style)
:tag('div')
:wikitext(text)
:allDone()
return title
end
function p.buildNavbox(params)
-- style definitions
local class = "overflow nav " .. (params.class or '')
local style = params.style or {}
if (params.separated) then -- options to add a separator line above
class = class .. ' bordered'
style['border-top'] = '1px solid' .. mainColor
end
-- adjustment of data parameters
params.previousvVal = params.previousVal or params.previousParameter -- obsolete parameter name [TODO: if it obsolete then we shouldn't have it]
params.nextVal = params.nextVal or params.nextParameter
if (params.previousProperty) then
params.previousWikidata = {property = params.previousProperty}
end
if (params.nextProperty) then
params.nextWikidata = {property = params.nextProperty}
end
local previousval = getValue(params.previousVal, params) or getWikidataValue(params, 'previouswikidata')
local nextval = getValue(params.nextVal, params) or getWikidataValue(params, 'nextwikidata')
local navbox
if (params.inner) then -- for those inside a table
navbox = mw.html.create('tr'):tag('th'):attr('colspan', 2)
style['font-weight'] = style['font-weight'] or 'normal'
else
navbox = mw.html.create('div')
end
navbox
:addClass(class)
:css(style)
:tag('div')
:addClass('prev_bloc')
:wikitext(previousVal)
:done()
:tag('div')
:addClass('next_bloc')
:wikitext(nextVal)
:done()
:allDone()
return navbox
end
function p.buildImages(params)
local images = {}
local upright
local link
local caption
local alt
local size -- size is deprecated [TODO: is it in ours also?]
if (type(params.imageParameters) == 'string') then
params.imageParameters = {params.imageParameters}
end
if (not params.imageParameters) then -- if there is no image parameter, continue, maybe there is a default image defined in the infobox module
params.imageParameters = {}
end
for j, k in ipairs(params.imagepParameters) do
table.insert(images, localData[k])
end
-- Wikidata images
if ((#images == 0) and (localData.item)) then
if (params.property) then
params.wikidata = {entity = localData.item, property = params.property}
end
if (params.wikidata) then
local wdq = params.wikidata
wdq.excludespecial = true
if (type(wdq) == 'table') then
wdq.entity = wdq.entity or localData.item
wdq.numval = wdq.numval or params.numval or 1
images = wikidata.getClaims(wdq)
end
if (type(wdq) == 'function') then
images = params.wikidata()
if (type(images) == 'string') then
return images
end -- that probably an error in the request => display the message
end
if (not images) then
images = {}
end
if ((#images > 0) and (params.wikidata.property)) then
addWikidataCategory(params.wikidata.property)
end
if (type(images[1]) == 'table') then
for i, image in pairs(images) do
if (image.mainsnak.snaktype ~= 'value') then
return
end
if (#images == 1) then -- if only one image, we use the legend (if two more images, how to format?) [TODO: what?]
caption = wikidata.getFormattedQualifiers(images[i], {'P2096'}, {isinlang = 'fr'}) or wikidata.getFormattedQualifiers(images[i], {'P805'})
end
images[i] = image.mainsnak.datavalue.value
end
end
end
end
-- Default images
if (#images == 0) then
if (params.maintenanceCategory) then
addMaintenanceCategory(params.maintenanceCategory, params.sortkey)
end
if (params.defaultimages) then
images = params.defaultimages
if (type(images) == 'string') then
images = {images}
end
upright = params.defaultimageupright
caption = params.defaultimagecaption
link = params.defaultimagelink
alt = params.defaultimagealt
if ((not alt) and ((images[1] == 'Defaut.svg') or (images[1] == 'Defaut 2.svg'))) then
alt = 'a free licensed illustration would be welcome' --TODO: what?
end
end
end
if (#images == 0) then
return nil
end
upright = upright or getValue(params.uprightparameter) or params.defaultupright or "1.2"
link = link or getValue(params.linkparameter) or params.defaultlink
caption = caption or getValue(params.captionparameter) or params.defaultcaption
alt = alt or getValue(params.altparameter) or params.defaultalt
-- with size "size" (obsolete) [TODO: remove?]
size = size or getValue(params.sizeparameter) or params.defaultsize -- deprecated [TODO]
if (size) then
local numsize = size:gsub('px', '')
numsize = mw.ustring.gsub(numsize, 'x.*', '')
numsize = tonumber(numsize)
if ((type(numsize) ~= 'number') or (numsize > 280)) then
addMaintenanceCategory("invalid image size")
end
if (tonumber(size)) then
size = size .. 'px'
end
size = '|' .. size
else
size = ''
end
if (tonumber(upright)) then
upright = tostring(tonumber(upright) ) / #images
end
if (tonumber(size)) then
size = tostring(tonumber(size) ) / #images
end
local style = params.style or {padding ='2px 0',}
-- Part images
local imagesString = ''
for i, image in pairs(images) do
if (image == '-') then
return
end
imagesString = imagesString .. '[[Fichier:' .. image .. size .. '|frameless'
if (alt) then
imagesString = imagesString .. '|alt=' .. alt
end
if (link) then
imagesString = imagesString .. '|link=' .. link
end
if (upright) then
imagesString = imagesString .. '|upright=' .. upright
elseif (#images > 1) then
imagesString = imagesString .. '|upright=' .. ( 1 / #images )
end
imagesString = imagesString .. ']]'
end
local image = mw.html.create('div')
:addClass("images")
:css(style)
:wikitext(imagesString)
-- Part legend
local captionObj
if (caption) then
captionObj = mw.html.create('div')
:wikitext(caption)
:css(params.legendstyle or {})
:addClass("legend")
:done()
end
-- separator
local separator
if (params.separator) then
separator = separator(params)
end
return mw.html.create('div')
:node(image)
:node(captionObj)
:node(separator)
:done()
end
function p.buildText(params)
local class = params.class or ''
local style = {
['text-align'] = 'center',
['font-weight'] = 'bold'
}
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
addMaintenanceCategory(params.maintenanceCategory, 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, number = getValue(params.value, params)
if (value == valueExpl) then
value = nil
params.wikidata.expl = false
end
if (not value) then
value, number = getWikidataValue(params, 'wikidata')
end
if ((not value) and (params.property)) then
value, number = expandQuery{property = params.property, ucfirst = params.ucfirst}
end
if (not value) then
value = params.defaultValue
end
if (value == '-') then
return nil
end
if (not number) then
number = 0 -- == undefined
end
if (not value) then
if (params.maintenanceCategory) then
local maintenanceCategory = getValue(params.maintenanceCategory, params)
addMaintenanceCategory(maintenanceCategory, params.sortkey)
end
return nil
end
local label = params.label
if ((number > 1) and (params.pluralLabel)) then
label = params.pluralLabel
elseif ((number == 1) and (params.singularLabel)) then
label = params.singularLabel
end
if (type(label) == 'function') then
label = label(localData, localData.item)
end
-- format
local formattedValue = mw.html.create('div')
:wikitext('\n' .. value) -- The '\ n' is required when value is a list starting with '*' or '#'
if (params.hidden == true)then
formattedValue
:attr({class="NavContent", style="display: none; text-align: left;"})
formattedValue = mw.html.create('div')
:attr({class="NavFrame", title="[Show]/[Hide]", 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.buildSuccession(params)
if (not params.value) then
return nil
end
local rowI = mw.html.create('tr')
local colSpan = '2'
cellI = mw.html.create('td')
:attr({colSpan = colSpan})
:attr({align = 'center'})
local styleT = {}
styleT['background-color'] = 'transparent'
styleT['width'] = '100%'
tabC = mw.html.create('table')
:attr({cellspacing = '0'})
:addClass('navigation-not-searchable')
:css(styleT)
local row = mw.html.create('tr')
local color = params.color
local style = {}
local arrowLeft
local arrowRight
if (color == 'default') then
style['background-color'] = '#E6E6E6'
else
style['background-color'] = color
end
arrowLeft = "←"
arrowRight = "→"
local styleTrans = {}
styleTrans['background-color'] = '#F9F9F9'
local values = params.value
local before = values['before']
local center = values['center']
local after = values['after']
local widthCell = '44%'
if (center) then
widthCenter = '28%'
widthCell = '29%'
end
--TODO: Understand this part
local formattedBefore
if (before) then
formattedBefore = mw.html.create('td')
:attr({valign = 'middle'})
:attr({align = 'left'})
:attr({width = '5%'})
:css(style)
:wikitext(arrowLeft)
:done()
row:node(formattedBefore)
formattedBefore = mw.html.create('td')
:attr({width = '1%'})
:css(style)
:wikitext('')
:done()
row:node(formattedBefore)
formattedBefore = mw.html.create('td')
:attr({align = 'left'})
:attr({valign = 'middle'})
:attr({width = widthCell})
:css(style)
:wikitext(before)
:done()
row:node(formattedBefore)
else
formattedBefore = mw.html.create('td')
:attr({valign = 'middle'})
:attr({align = 'left'})
:attr({width = '5%'})
:css(styleTrans)
:wikitext('')
:done()
row:node(formattedBefore)
formattedBefore = mw.html.create('td')
:attr({width = '1%'})
:css(styleTrans)
:wikitext('')
:done()
row:node(formattedBefore)
formattedBefore = mw.html.create('td')
:attr({align = 'left'})
:attr({valign = 'middle'})
:attr({width = widthCell})
:css(styleTrans)
:wikitext('')
:done()
row:node(formattedBefore)
end
local formattedCenter
formattedCenter = mw.html.create('td')
:attr({width = '1%'})
:css(styleTrans)
:wikitext('')
:done()
row:node(formattedCenter)
if (center) then
formattedCenter = mw.html.create('td')
:attr({align = 'center'})
:attr({valign = 'middle'})
:attr({width = widthCenter})
:css(style)
:wikitext(center)
:done()
row:node(formattedCenter)
formattedCenter = mw.html.create('td')
:attr({width = '1%'})
:css(styleTrans)
:wikitext('')
:done()
row:node(formattedCenter)
end
local formattedAfter
if (after) then
formattedAfter = mw.html.create('td')
:attr({align = 'right'})
:attr({valign = 'middle'})
:attr({width = widthCell})
:css(style)
:wikitext(after)
:done()
row:node(formattedAfter)
formattedBefore = mw.html.create('td')
:attr({width = '1%'})
:css(style)
:wikitext('')
:done()
row:node(formattedBefore)
formattedAfter = mw.html.create('td')
:attr({align = 'right'})
:attr({valign = 'middle'})
:attr({width = '5%'})
:css(style)
:wikitext(arrowRight)
:done()
row:node(formattedAfter)
else
formattedAfter = mw.html.create('td')
:attr({align = 'right'})
:attr({valign = 'middle'})
:attr({width = widthCell})
:css(styleTrans)
:wikitext('')
:done()
row:node(formattedAfter)
formattedBefore = mw.html.create('td')
:attr({width = '1%'})
:css(styleTrans)
:wikitext('')
:done()
row:node(formattedBefore)
formattedAfter = mw.html.create('td')
:attr({align = 'right'})
:attr({valign = 'middle'})
:attr({width = '5%'})
:css(styleTrans)
:wikitext('')
:done()
row:node(formattedAfter)
end
row:done()
tabC:node(row)
tabC:done()
cellI:node(tabC)
cellI:done()
rowI:node(cellI)
rowI:allDone()
return rowI
end
function p.buildRow1Col(params)
if (not params.value) then
return nil
end
local class = params.class
local rowcolor
if (params.color == 'secondColor') then
rowcolor = secondColor
else
rowcolor = params.color
end
local style = {}
style['padding'] = '4px'
style['text-align'] = 'center'
style['background-color'] = rowcolor or '#F9F9F9'
style['color'] = '#000000'
local text = params.value
local colspan ='2'
local formattedLabel
formattedLabel = mw.html.create('th')
:attr({colspan = colspan})
:css(style)
:wikitext(text)
:done()
local row = mw.html.create('tr')
:addClass(class)
:css(style)
:node(formattedLabel)
:done()
return row
end
function p.buildTable(params)
local tab = mw.html.create('table')
:css(params.style or {})
local rows = params.rows
-- expand parameters so that we have a list of tables
local i = 1
while (i <= #rows) do
local currentRow = rows[i]
if (type(currentRow) == 'function') then
currentRow = currentRow(localData, localData.item)
end
if ((type(currentRow) == 'table') and (currentRow.type == 'multi')) then
table.remove(rows, i)
for j, row in ipairs(currentRow.rows) do
table.insert(rows, i + j - 1, row)
end
elseif (type(currentRow) == 'nil') then
table.remove(rows, i)
elseif (type(currentRow) ~= 'table') then
return error('lines of \' infobox ("rows") must be tables, is ' .. type(currentRow)) --[TODO: ?]
else
i = i + 1
end
end
-- Create row
local expandedrows = {}
for k, row in ipairs(rows) do
local v = p.buildBlock(row)
if (v) then
table.insert(expandedrows, v)
end
end
if (#expandedrows == 0) then
return nil
end
rows = expandedrows
-- Add title
local title
if ((params.title) or (params.singularTitle) or (params.pluralTitle)) then
local text
if ((#rows > 1) and (params.pluralTitle)) then
text = params.pluralTitle
elseif ((#rows == 1) and (params.singularTitle)) then
text = params.singularTitle
else
text = params.title
end
local style = params.titleStyle or {}
style['text-align'] = style['text-align'] or 'center'
style['color'] = style['color'] or thirdColor
style['background-color'] = style['background-color'] or mainColor
local colspan ='2'
title = mw.html.create('caption')
:attr({colspan = colspan})
:css(style)
:wikitext(text)
:done()
end
if (title) then
tab:node(title)
end
for i, j in pairs(rows) do
tab:node(j)
end
if (params.separator) then
local separator = p.separator(params)
tab:node(separator)
end
tab:allDone()
return tab
end
function p.buildInvalidBlock(args)
addMaintenanceCategory(defaultCategory) --TODO: defaultCategory isn't defined anywhere!
local text = ''
if (type(args) ~= 'table') then
text = "The infobox blocks must be tables" --TODO: ?
else
--text = i18n["invalid block type"] .. ' : ' .. (args.type or '??')
end
return text
end
function p.buildMap(params)
-- Display parameter
local mapList = getValue(params.maps)
local pointType = params.pointType
local mapType = params.mapType -- chooses the most appropriate map type (relief, administrative, etc.)
if (type(mapList) == 'function') then
mapList = mapList(localData, localData.item)
end
local width = tonumber(params.width) or 280
if (width > 280) then
addMaintenanceCategory("Error Infobox / Image too large")
return 'Image too large, the width must be less than or equal to 280px'
end
-- Local data recovery
local pointTable = {}
local globe = params.globe
if (params.latitude) then
local lat, long
if (type(params.latitude) == 'function') then
lat, long = params.latitude(localData, localData.item), params.longitude(localData, localData.item)
else
lat, long = localData[params.latitude], localData[params.longitude]
end
if (lat) then
table.insert(pointTable, {latitude = lat, longitude = long})
end
end
-- Wikidata data recovery
local function processWDval(claim, displayformat)
if (not claim) then
return nil
end
local val = wikidata.formatSnak(claim.mainsnak)
return {latitude = val.latitude, longitude = val.longitude, globe = val.globe, marker = displayformat.marker}
end
local function getWDvals(query)
query.excludespecial = true
query.numval = query.numval or 1
query.entity = query.entity or localData.item
local claims = wikidata.getClaims(query)
if (not claims) then
return
end
for i, claim in ipairs(claims) do
claim = processWDval(claim, query)
table.insert(pointTable, claim)
end
end
if ((#pointTable == 0) and (localData.item) and (params.wikidata) and (params.wikidata ~= '-')) then
for i, query in ipairs(params.wikidata) do
if (type(query) == 'function') then
query = query()
end
if (query) then
getWDvals(query)
end
end
end
if ((not pointTable) or (#pointTable == 0)) then
return nil
end
local newparams = {
mapList = mapList,
pointType = pointType,
mapType = mapType,
width = width,
item = localData.item,
pointTable = pointTable,
globe = globe,
marker = params.marker,
default_zoom = params.default_zoom,
ids = params.ids,
markercolor = params.markercolor,
shapecolor = params.shapecolor
}
if ((params.params) and (type(params.params) == 'table')) then -- Additional parameters
for i, j in pairs(params.params) do
newparams[i] = j
end
end
return require('Module:Carte').multimap(newparams) --TODO: need to check module
end
function p.buildExternalText(params)
local value = getValue(params.value)
if ((value) and (type(value) == 'string')) then
externalText = externalText .. value
end
end
function p.buildFooter(params)
if (not params) then
params = {}
end
local footer = mw.html.create('p')
:addClass(class)
:css(style)
return footer
end
-- Builds a block. A block can be for example an image, a row or a table.
function p.buildBlock(block)
if (type(block) == 'function') then
block = block(localData)
end
-- List of functions for building blocks
local blockTypes = {
['invalid'] = p.buildinvalidBlock,
['external text'] = p.buildExternalText,
['footer'] = p.buildFooter,
['images'] = p.buildImages,
['map']= p.buildMap,
['mixed'] = p.buildRow,
['navbox'] = p.buildNavox,
['table'] = p.buildTable,
['row'] = p.buildRow,
['row1col'] = p.buildRow1Col,
['succession'] = p.buildNavbox,
['text'] = p.buildText,
['title'] = p.buildTitle,
}
if ((type(block) ~= 'table') or (not block.type) or (not blockTypes[block.type])) then
return blockTypes['invalid'](block)
end
return blockTypes[block.type](block)
end
-- Entry point
function p.build()
localData = require('Module:Sandbox/Gonnym/Localdata')
item = localData.item
-- Load infobox module page
local moduleData = require('Module:Infobox/' .. localData.moduleName)
moduleData.name = localData.modulenName
-- Defining colors. TODO: Need to understand what each color is used for
mainColor = moduleData.mainColor or mainColor
secondColor = moduleData.secondColor or secondColor
thirdColor = moduleData.thirdColor or thirdColor
--TODO: what does this check for?
if (mainColor:match('^%x%x%x%x%x%x$') or mainColor:match('^%x%x%x$')) then
mainColor = '#' .. mainColor
end
if (thirdColor:match('yes') or thirdColor:match('true')) then
thirdColor = '#000'
end
if (thirdColor:match('no') or thirdColor:match('false')) then
thirdColor = '#fff'
end
-- class
local class = 'infobox_v3 large'
if (moduleData.class) then
class = class .. ' ' .. moduleData.class
end
-- style
local style = moduleData.style or {}
-- build infobox
infobox:addClass(class)
:css(style)
for i, j in pairs(moduleData.parts) do
infobox:node(p.buildBlock(j))
end
infobox:node(p.buildFooter(moduleData.footer))
:done()
return tostring(infobox) .. externalText, maintenance
end
return p
