ಮಾಡ್ಯೂಲ್:Databox/test
ಗೋಚರ
-- A modified version based on sv.wikipedia.org/wiki/Modul:Databox
-- Displays Wikidata properties dynamically inside an infobox.
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 useImage = nil
if argsLocal.useImage then
useImage = argsLocal["useImage"]
end
-- Option to display aliases below the title (defaults to true)
local useAliases = true
if argsLocal.useAliases and (argsLocal.useAliases == "false" or argsLocal.useAliases == "no" or argsLocal.useAliases == "0") then
useAliases = false
end
-- local excludeProperties parameter allows excluding specific properties per page if needed
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'] = '250px',
padding = '0 0.4em',
margin = '0 0 0.4em 0.4em',
})
-- Title
local titleDiv = 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)
-- Aliases display (Color set to #6f9f9c)
if useAliases and item.aliases then
local langCode = lang:getCode()
local aliases = item.aliases[langCode]
if aliases and #aliases > 0 then
local aliasList = {}
for _, aliasObj in ipairs(aliases) do
table.insert(aliasList, aliasObj.value)
end
titleDiv:tag('div')
:css({
['font-size'] = '75%',
['font-weight'] = 'normal',
['font-style'] = 'italic',
['color'] = '#6f9f9c',
['margin-top'] = '0.2em'
})
:wikitext(table.concat(aliasList, ', '))
end
end
-- 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|240px|center]]')
end
-- Signature (P109)
local signatureImage = nil
local signatureImages = item:getBestStatements('P109')
if #signatureImages >= 1 then
signatureImage = signatureImages[1].mainsnak.datavalue.value
end
if signatureImage then
databoxRoot
:tag('div')
:css({
['text-align'] = 'center',
['font-size'] = '90%',
['font-style'] = 'italic',
padding = '0.2em 0',
})
:wikitext('Signature')
databoxRoot
:tag('div')
:wikitext('[[File:' .. signatureImage .. '|frameless|200px|center]]')
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())
-- Excluded property table handles local exclusions
local excludeProperties_hash = valuesToKeys(excludeProperties)
excludeProperties_hash['P31'] = true -- Instance of
excludeProperties_hash['P373'] = true -- Commons category
excludeProperties_hash['P935'] = true -- Commons gallery
excludeProperties_hash['P910'] = true -- Main category of topic
excludeProperties_hash['P1792'] = true -- Category of associated people
excludeProperties_hash['P9495'] = true -- Category for maps or plans
excludeProperties_hash['P1014'] = true -- Category for pictures taken with this camera
excludeProperties_hash['P5777'] = true -- Category for the view of the item
excludeProperties_hash['P8744'] = true -- Economy of topic
excludeProperties_hash['P8745'] = true -- Demographics of topic
excludeProperties_hash['P1889'] = true -- Different from
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 excludeProperties_hash[property] and valueCount > 0 and valueCount <= 5 then
local propertyValue = item:formatStatements(property)
if propertyValue and propertyValue.value then
-- Get the raw value to make it clickable
local displayValue = propertyValue.value
local claims = item.claims[property]
if claims then
local linkedValues = {}
for _, claim in ipairs(claims) do
if claim.mainsnak and claim.mainsnak.datavalue then
local value = claim.mainsnak.datavalue.value
if type(value) == 'table' and value.id then
-- It's a Wikidata item, create a link
local entity = mw.wikibase.getEntity(value.id)
if entity then
local label = entity:getLabel()
if label then
table.insert(linkedValues, '[[:d:' .. value.id .. '|' .. label .. ']]')
else
table.insert(linkedValues, '[[:d:' .. value.id .. '|' .. value.id .. ']]')
end
else
table.insert(linkedValues, '[[:d:' .. value.id .. '|' .. value.id .. ']]')
end
elseif type(value) == 'string' then
table.insert(linkedValues, value)
elseif type(value) == 'number' then
table.insert(linkedValues, tostring(value))
end
end
end
if #linkedValues > 0 then
displayValue = table.concat(linkedValues, ', ')
end
end
dataTable:tag('tr')
:tag('th')
:attr('scope', 'row')
:wikitext(lang:ucfirst(propertyValue.label)):done()
:tag('td')
:wikitext(displayValue)
: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
end
-- Helper function to clean category name
local function cleanCategoryName(value)
if not value then return nil end
if type(value) == 'table' then
value = tostring(value)
end
value = value:gsub("^Category:", "")
value = value:gsub("^%s+", ""):gsub("%s+$", "")
return value
end
-- Helper function to get entity label or value
local function getEntityLabel(value)
if type(value) == 'table' and value.id then
local entity = mw.wikibase.getEntity(value.id)
if entity then
return entity:getLabel() or value.id
end
return value.id
elseif type(value) == 'table' then
return tostring(value)
end
return value
end
-- Economy of topic (P8744)
local economyStatements = item:getBestStatements('P8744')
if #economyStatements >= 1 and economyStatements[1].mainsnak.datavalue then
local economyValue = economyStatements[1].mainsnak.datavalue.value
local displayValue = getEntityLabel(economyValue)
if displayValue then
dataTable:tag('tr')
:tag('th')
:attr('scope', 'row')
:wikitext('Economy of topic'):done()
:tag('td')
:wikitext("'''" .. displayValue .. "'''")
:wikitext(' [[File:OOjs UI icon edit-ltr.svg|' .. edit_message .. '|12px|baseline|class=noviewer|link=https://www.wikidata.org/wiki/' .. item.id .. '#P8744]]')
end
end
-- Demographics of topic (P8745)
local demographicsStatements = item:getBestStatements('P8745')
if #demographicsStatements >= 1 and demographicsStatements[1].mainsnak.datavalue then
local demographicsValue = demographicsStatements[1].mainsnak.datavalue.value
local displayValue = getEntityLabel(demographicsValue)
if displayValue then
dataTable:tag('tr')
:tag('th')
:attr('scope', 'row')
:wikitext('Demographics of topic'):done()
:tag('td')
:wikitext("'''" .. displayValue .. "'''")
:wikitext(' [[File:OOjs UI icon edit-ltr.svg|' .. edit_message .. '|12px|baseline|class=noviewer|link=https://www.wikidata.org/wiki/' .. item.id .. '#P8745]]')
end
end
-- Different from (P1889)
local differentFromStatements = item:getBestStatements('P1889')
if #differentFromStatements >= 1 and differentFromStatements[1].mainsnak.datavalue then
local differentFromValue = differentFromStatements[1].mainsnak.datavalue.value
local displayValue = getEntityLabel(differentFromValue)
if displayValue then
dataTable:tag('tr')
:tag('th')
:attr('scope', 'row')
:wikitext('Different from'):done()
:tag('td')
:wikitext("'''" .. displayValue .. "'''")
:wikitext(' [[File:OOjs UI icon edit-ltr.svg|' .. edit_message .. '|12px|baseline|class=noviewer|link=https://www.wikidata.org/wiki/' .. item.id .. '#P1889]]')
end
end
-- Commons Gallery (P935)
local commonsGalleryStatements = item:getBestStatements('P935')
if #commonsGalleryStatements >= 1 and commonsGalleryStatements[1].mainsnak.datavalue then
local commonsGallery = commonsGalleryStatements[1].mainsnak.datavalue.value
local displayValue = getEntityLabel(commonsGallery)
if displayValue then
local cleanValue = cleanCategoryName(displayValue)
dataTable:tag('tr')
:tag('th')
:attr('scope', 'row')
:wikitext('Commons gallery'):done()
:tag('td')
:wikitext('[[:commons:' .. cleanValue .. '|' .. cleanValue .. ']]')
:wikitext(' [[File:OOjs UI icon edit-ltr.svg|' .. edit_message .. '|12px|baseline|class=noviewer|link=https://www.wikidata.org/wiki/' .. item.id .. '#P935]]')
end
end
-- Commons Category (P373)
local commonsCategoryStatements = item:getBestStatements('P373')
if #commonsCategoryStatements >= 1 and commonsCategoryStatements[1].mainsnak.datavalue then
local commonsCat = commonsCategoryStatements[1].mainsnak.datavalue.value
local displayValue = getEntityLabel(commonsCat)
if displayValue then
-- Split categories if multiple (comma separated)
local categories = {}
for cat in string.gmatch(displayValue, "[^,]+") do
local trimmedCat = cat:gsub("^%s+", ""):gsub("%s+$", "")
local cleanCat = cleanCategoryName(trimmedCat)
categories[#categories + 1] = '[[:commons:Category:' .. cleanCat .. '|' .. cleanCat .. ']]'
end
local categoryLinksText = table.concat(categories, ' • ')
dataTable:tag('tr')
:tag('th')
:attr('scope', 'row')
:wikitext('Commons category'):done()
:tag('td')
:wikitext(categoryLinksText)
:wikitext(' [[File:OOjs UI icon edit-ltr.svg|' .. edit_message .. '|12px|baseline|class=noviewer|link=https://www.wikidata.org/wiki/' .. item.id .. '#P373]]')
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
-- 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 = 240,
width = 240,
frameless = 'frameless',
align = 'center',
latitude = latitude,
longitude = longitude,
zoom = zoom
}))
end
-- Wikidata Link (Footer)
databoxRoot:tag('div')
:css({
['display'] = 'flex',
['align-items'] = 'center',
['justify-content'] = 'center',
padding = '0.3em 0',
['width'] = '100%',
['font-size'] = '90%',
['text-align'] = 'center',
})
: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 0 0 0.3em' })
:wikitext('[[d:' .. item.id .. '|From Wikidata]]')
return tostring(databoxRoot)
end
return p








