Module:Databox
This module provides an as simple as possible infobox system based on Wikidata.

It is fully automated, does not require any configuration to be used and does not have any declination based on the type of entity (person, place...).
How does it work?
This module is the backend code of the Databox template.
It uses the Wikidata item linked to the current page or the item which id is filled into the item parameter to automatically build an infobox.
Its basic algorithm is:
- Use the label of the item or, if none exists, the title of the page for the infobox title.
- Use the value of image (P18) for the main image.
- Use the value(s) of instance of (P31) for the data table title.
- Take all the properties used by the item, sort them according to MediaWiki:Wikibase-SortedProperties, and for each one:
- If the property has the datatype External identifier, Commons media file or Quantity[a 1], do not display anything.
- If the property is in the
site_excluded_propertiesarray of the module, do not display anything (it is done in order to filter properties not very relevant for displaying in infoboxes). - If the property has more than 5 values do not display anything (to avoid long lists in the infobox).
- Use default Wikibase rendering system to display the values that have the "best" rank (i.e. the values with the rank "preferred", if some exists, and, if not, the ones with the rank "normal").
- If there is value for coordinate location (P625), then display a map using Kartographer.
Easy to install instructions
Add the Module code
1. Copy-paste the Databox Module to your Wiki
- Go to
Module:Databox-Codeand copy all contents in the code block. - Paste these contents into your own Wiki's
Module:Databoxpage (if one doesn't exist yet, create one).
- Go to
2. Add the sitelink of the newly-created Wikipedia page (Module:Databox) to the Wikidata item of Module:Databox (Q53931871)
Add the Template code
3. Copy-paste the following template code into your own Wiki Template:Databox page (if one doesn't exist yet, create one).
{{#invoke:Databox|databox|useImage={{{useImage|}}}|excludeProperties={{{excludeProperties|}}}}}
4. Connect the newly-created Template:Databox page to the Wikidata item of Template:Databox (Q20702632)
5. Test the template
- Edit an article that is connected to a Wikidata item, for example, the capital City of your Country or Territory.
- Enter the edit source view and add the template code {{Databox}} to the top of the page.
- Preview the page to verify the databox appeared on the page, and publish to save your changes.
- Congratulations, you have installed your first databox!
Edit Databox data
Sometimes data may be incorrect or outdated and needs to be changed or updated, but you cannot do that from the current page. Data seen in the databox is not stored locally, it is transcluded from Wikidata.
To change or edit data shown in databox:
- Click the pencil icon
next to the statement in the databox. - This will take you to where on the Wikidata item the statement is stored.
- Click the pencil icon
again, on the side of the Statement box. This opens the edit mode on Wikidata. - Enter the new value in the input field. Add qualifiers or references as appropriate.
- If the value is outdated but still correct (such as Census data), click the + icon to the right of the pencil icon instead, and enter the latest or newest value.
- Click the tick/checkmark
button to publish the statement. The databox will immediately update and the new information will appear.
Notes
- ↑ The current display of quantities by Wikibase is very limited, this filter should be removed after improvements
Examples
Early Modern Swedish
Lucy |
ethanol |
Hubble Space Telescope
|
Vesta |
atheism
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Iron Man
|
Airbus SAS
|
Esperanto
![]() |
Berlin
![]() |
Douglas Adams
|
No description.
| Parameter | Description | Type | Status | |
|---|---|---|---|---|
| from | from | Forces Databox to retrieve data from a specific Wikidata item (Q123) instead of page's linked Wikidata item. | String | optional |
| useImage | useImage | File name for the preferred image to use from Wikidata Entity | String | optional |
| excludeProperties | excludeProperties | Comma separated list of Wikidata Property Ids to remove from the databox | String | optional |
Wikitext Databox examples
The examples below show how the databox template can appear in the edit source view.
{{Databox}}
- Default choice. It will retrieve data from the Wikidata item connected to the page.
{{Databox|from=Q123}}
- Forces the databox to retrieve data from a specified Wikidata item. This can be a different item than the one connected/sitelinked to the page, but it cannot be both. Databox data can only be retrieved from one item.
{{Databox|useImage=filename.jpg}}
- Manually specify another image (P18) to display in the databox.
{{Databox|excludeProperties=P123}}
- Specify a comma-separated list of Wikidata properties (PID's) that should not be shown in the current databox.
{{Databox|useImage=filename.jpg|excludeProperties=P123}}
- Combining parameters allows more content control.
How to use the new parameters
Step-by-step instructions to invoke and use the additional Databox parameters.
useImage
- Open the edit source view of a page you want to add Databox to, or edit an existing one.
- Add the code:
|useImage=to the {{Databox}} template. - Navigate to the Wikidata item of the connected databox, and find the Image (P18) statement. Tip: you can add #P18 to the of the Wikidata item URL to quickly navigate to it.
- To use a different image (locally) than the currently applied one, locate the filename (underneath the image itself) and highlight it and copy it (Ctrl+C).
- Navigate back to the page where you are invoking the {{Databox|useImage=}} and paste (Ctrl+V) the copied filename into the template.
- The template code should now look like: {{Databox|useImage=highlighted_text.jpg}}.
- Preview the changes to see the new image in the Databox, and publish the page to save your changes.
excludeProperties
- Add a {{Databox}} to a page or view an existing one, and notedown the Property ID/number of the rows and data you wish to hide from the local databox.
- You can do this quickly by hovering the mouse-cursor over the
icon on the corresponding row of the databox. - In the bottom of your browser-window, the URL of the corresponding property will be previewed, locate and notedown the PID of that URL.
Example: if the URL looks likewww.wikidata.org/wiki/Q123456#P789, notedown P789. - Do the same for all rows/properties that are to be hidden.
- You can do this quickly by hovering the mouse-cursor over the
- Add the code:
|excludeProperties=to the {{Databox}} template. - Add the Property ID/numbers to the databox template code, separated by a comma , e.g.
{{Databox|excludeProperties=P789,P123,P456}}. - Preview the changes to ensure the rows have been hidden from the databox, and publish the page to save your changes.
Please remember: This parameter only hides the property data, it does not remove or delete it and will not affect any other Databoxes on the local or other Wikis.
See also
- ru:Template:Универсальная карточка - similar, but more developed template in Russian Wikipedia
- en:Template:Infobox person/Wikidata - similar, for people only, in English Wikipedia
- commons:Template:Wikidata Infobox - Commons' equivalent; mostly used for categories
Code
-- A more extensive version is stored and documented at the Swedish Wikipedia site, sv.wikipedia.org/wiki/Modul:Databox
-- It is supposed to work for any language.
-- Properties excluded for all Databoxes on the current site
local site_excluded_properties = {
'P360', --is a list of
'P4224', --category contains
'P935', -- Commons gallery
'P1472', -- Commons Creator page
'P1612', -- Commons Institution page
'P373', -- Commons category
'P3722', -- Commons maps category
'P1151', -- topic's main Wikimedia portal
'P1424', -- topic's main template
'P910', -- topic's main category
'P1200', -- bodies of water basin category
'P1792', -- category of associated people
'P1464', -- category for people born here
'P1465', -- category for people who died here
'P1791', -- category of people buried here
'P1740', -- category for films shot at this location
'P2033', -- Category for pictures taken with camera
'P2517', -- category for recipients of this award
'P4195', -- category for employees of the organization
'P1754', -- category related to list
'P301', -- category's main topic
'P971', -- category combines topics
'P3876', -- category for alumni of educational institution
'P1753', -- list related to category
'P3921', -- Wikidata SPARQL query equivalent
'P1204', -- Wikimedia portal's main topic
'P1423', -- template's main topic
'P1709', -- equivalent class
'P3950', -- narrower external class
'P2888', -- exact match
'P1382', -- coincident with
'P527', -- has part
'P2670', -- has parts of the class
'P3113', -- does not have part
'P2737', -- union of
'P2738', -- disjoint union of
'P2445', -- metasubclass of
'P1963', -- properties for this type
'P3176', -- uses property
'P1889', -- different from
'P460', -- said to be the same as
'P2959', -- permanent duplicated item
'P2860', -- cites
'P5125', -- wikimedia outline
'P5008', -- on focus list of Wikimedia project
'P2559', -- Wikidata usage instructions
'P1343', -- described by source
'P972', -- catalogu
'P1282', -- OSM tag or key
'P4839', -- Wolfram Language entity code
'P6104', -- Maintained by Wikiproject
'P5996' -- Category for films in this language
}
function valuesToKeys(array)
local result = {}
for _, v in pairs(array) do
result[v] = true
end
return result
end
local p = {}
function p.databox(frame)
local args = frame:getParent().args
local argsLocal = frame.args
local itemId = nil
if args.item then
itemId = args.item
end
local argsLocal = frame.args
local useImage = nil
if argsLocal.useImage then
useImage = argsLocal["useImage"]
end
-- excludeProperties is a local parameter to exclude given properties for a Databox on a specific page
local excludeProperties = {}
if argsLocal.excludeProperties then
for item in string.gmatch(argsLocal.excludeProperties, "[^,]+") do
table.insert(excludeProperties, item)
end
end
local lang = mw.language.getContentLanguage()
local item = mw.wikibase.getEntity(itemId)
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 databoxImage = nil
if useImage and useImage ~= "" then
local allWikidataImages = item:getAllStatements('P18')
if #allWikidataImages >= 1 then
for _, image in ipairs( allWikidataImages ) do
if image.mainsnak.datavalue.value == useImage then
databoxImage = useImage
break
end
end
end
end
if databoxImage == nil then
local bestWikidataImages = item:getBestStatements('P18')
if #bestWikidataImages >= 1 then
databoxImage = bestWikidataImages[1].mainsnak.datavalue.value
end
end
if databoxImage then
databoxRoot
:tag('div')
:wikitext('[[File:' .. databoxImage .. '|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')
:addClass('notheme')
:css({
['background-color'] = '#f5f5f5',
['font-weight'] = 'bold',
['margin-top'] = '0.2em',
})
:wikitext(item:formatStatements('P31').value)
local properties = mw.wikibase.orderProperties(item:getProperties())
local site_excluded_properties_hash = valuesToKeys(site_excluded_properties)
site_excluded_properties_hash['P31'] = true --Special property
local excludeProperties_hash = valuesToKeys(excludeProperties)
local edit_message = mw.message.new('vector-view-edit'):plain()
for _, property in pairs(properties) do
local datatype = item.claims[property][1].mainsnak.datatype
local valueCount = #item:getBestStatements(property)
if datatype ~= 'commonsMedia' and datatype ~= 'external-id' and datatype ~= 'quantity' and datatype ~= 'wikibase-property' and datatype ~= 'geo-shape' and datatype ~= 'tabular-data' and not site_excluded_properties_hash[property] and not excludeProperties_hash[property] and valueCount > 0 and valueCount <= 5 then
local propertyValue = item:formatStatements(property)
dataTable:tag('tr')
:tag('th')
:attr('scope', 'row')
: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
--Map
local coordinates_statements = item:getBestStatements('P625')
if #coordinates_statements >= 1 and coordinates_statements[1].mainsnak.datavalue and coordinates_statements[1].mainsnak.datavalue.value.globe == 'http://www.wikidata.org/entity/Q2' then
--We build the call to mapframe
local latitude = coordinates_statements[1].mainsnak.datavalue.value.latitude
local longitude = coordinates_statements[1].mainsnak.datavalue.value.longitude
local geojson = {
type = 'Feature',
geometry = {
type = 'Point',
coordinates = { longitude, latitude }
},
properties = {
title = item:getLabel() or mw.title.getCurrentTitle().text,
['marker-symbol'] = 'marker',
['marker-color'] = '#224422',
}
}
databoxRoot:wikitext(frame:extensionTag('mapframe', mw.text.jsonEncode(geojson), {
height = 300,
width = 300,
frameless = 'frameless',
align = 'center',
latitude = latitude,
longitude = longitude,
zoom = zoom
}))
end
--Wikidata Link
databoxRoot:tag('div')
:css({
['display'] = 'flex',
padding = '0.3em 0',
['width'] = '100%',
['font-size'] = '90%',
})
:addClass('databox-from-wikidata-link')
:wikitext(' [[File:Wikidata-logo.svg|22px|class=noviewer skin-invert|link=https://www.wikidata.org/wiki/' .. item.id .. ']]')
:tag('div')
:css({
margin = '0.05em 0 0 0',
})
:wikitext(' [[d:' .. item.id .. '|From Wikidata]]')
return tostring(databoxRoot)
end
return p

