Module:Quantity
From MaRDI portal
Documentation for this module may be created at Module:Quantity/doc
-- Required modules for SPARQL queries and HTML table generation
-- Module for executing SPARQL queries
local sparql = require('SPARQL')
-- MediaWiki library for logging and utilities
local mw = require('mw')
local json = require("mw.text")
-- Main table to hold all functions
local p = {}
-- Function to get defining formulation with Quantities
function p.extractDefiningFormulationWithQuantities(frame)
local equation = mw.wikibase.getEntity()
local equationID = entity and entity.id or nil
-- Table to store formulations
local formulations = {}
-- Table to store quantity symbol and name id
local quantitySymbolNameIdPairs = {}
-- P989 refers to property defining formula
local definingFormula = equation:getBestStatements('P989')
if definingFormula and #definingFormula > 0 then
local value = definingFormula[1].mainsnak.datavalue.value
mathTag = frame:extensionTag{
name = "math",
content = value
}
table.insert(formulations, "| Defining Formula: " .. mathTag)
end
quantitySymbolNameIdPairs = p.extractQuantities(equationID)
if type(quantitySymbolNameIdPairs) == "table" then
-- Accessing the stored pairs
for i, pair in ipairs(quantitySymbolNameIdPairs) do
local pairFirstValue = pair[1]
local pairFirstValue = mw.text.decode(pairFirstValue or "")
local quantitySymbolMathTag = frame:extensionTag{
name = "math",
content = pairFirstValue
}
-- Construct the Portal URL
local quantityId = pair[3]
local numericId = quantityId:sub(2) -- remove "Q" to get "6674540"
local url = "https://portal.mardi4nfdi.de/wiki/Quantity:"
local fullUrl = url .. numericId
local labelWithUrl = string.format('[%s %s]', tostring(fullUrl), tostring(pair[2]))
table.insert(formulations, "| " .. quantitySymbolMathTag .. " symbol represents " .. labelWithUrl)
end
table.insert(formulations, "| " .. " " )
else
-- Error: extractQuantities did not return a table"
end
-- Construct the Wikitext table
local wikitextTable = [[
{| class="wikitable" style="table-layout: fixed; width: 1000px;"
]] .. table.concat(formulations, "\n|-\n") .. "\n|}"
return wikitextTable
end
function p.extractQuantities(qid)
-- Property ID for in defining formula
local pidInDefiningFormula = "P983"
-- Property ID for the (qualifier) symbol represents
local pidSymbolRepresents = "P984"
-- Attempt to retrieve entity data
local entity = mw.wikibase.getEntity(qid)
if not entity or not entity.claims[pidInDefiningFormula] then
return "No formulation found"
end
local inDefiningFormulaClaims = entity.claims[pidInDefiningFormula]
local count = 0
-- Table to store pairs of quantity symbol and quantity name
local quantitySymbolNameIdPairs = {}
for _ in pairs(inDefiningFormulaClaims or {}) do
count = count + 1
-- Get the quantity symbol
local quantitySymbol = inDefiningFormulaClaims[count].mainsnak.datavalue.value
if not quantitySymbol then
return "No valid symbol found"
end
quantity = inDefiningFormulaClaims[count].qualifiers[pidSymbolRepresents][1].datavalue.value.text
quantityId = inDefiningFormulaClaims[count].qualifiers[pidSymbolRepresents][1].datavalue.value.id
local quantityName = mw.wikibase.label(quantityId)
-- Insert pair into the table
table.insert(quantitySymbolNameIdPairs, {quantitySymbol, quantityName, quantityId})
end
return quantitySymbolNameIdPairs
end
function p.getImageWithLegend(frame)
-- Property ID for the image
local pidImage = "P356"
-- Property ID for the (qualifier) media legend
local pidMediaLegend = "P401"
local defaultLegend = "No legend available."
local entityId = frame.args[1]
-- Validate input parameter
if not entityId or entityId == '' then
return "Error: No entity ID provided"
end
-- Attempt to retrieve entity data
local entity = mw.wikibase.getEntity(entityId)
local imageClaims = entity.claims[pidImage]
local imageStrings = {}
if imageClaims and type(imageClaims) == "table" then
local numClaims = #imageClaims
-- Loop through the image claims
for index = 1, numClaims do
-- Extract the image filename
local imageFilename = imageClaims[index].mainsnak.datavalue.value
local imageLegend = imageClaims[index].qualifiers[pidMediaLegend][1].datavalue.value.text
-- Add this image line to the list
table.insert(imageStrings, string.format("[[File:%s|thumb|380px|%s]]", imageFilename, imageLegend))
if index < numClaims then
-- spacer column
table.insert(imageStrings, ' ')
end
end
-- Combine all image cells into one row of a wikitable
local imageTable = '{| class="wikitable"\n| ' .. table.concat(imageStrings, ' || ') .. '\n|}'
return imageTable .. "\n\n"
else
return " "
end
end
function p.getVideoWithLegend(frame)
-- Property ID for the video
local pidVideo = "P797"
-- Property ID for the (qualifier) media legend
local pidMediaLegend = "P401"
local defaultLegend = "No legend available."
local entityId = frame.args[1]
-- Validate input parameter
if not entityId or entityId == '' then
return "Error: No entity ID provided"
end
-- Attempt to retrieve entity data
local entity = mw.wikibase.getEntity(entityId)
local videoClaims = entity.claims[pidVideo]
local videoStrings = {}
if videoClaims and type(videoClaims) == "table" then
local numClaims = #videoClaims
-- Loop through the image claims
for index = 1, numClaims do
-- Extract the image filename
local videoFilename = videoClaims[index].mainsnak.datavalue.value
local videoLegend = videoClaims[index].qualifiers[pidMediaLegend][1].datavalue.value.text
-- Add this image line to the list
table.insert(videoStrings, string.format("[[File:%s|thumb|340px|%s]]", videoFilename, videoLegend))
if index < numClaims then
-- spacer column
table.insert(videoStrings, ' ')
end
end
-- Combine all image cells into one row of a wikitable
local videoTable = '{| class="wikitable"\n| ' .. table.concat(videoStrings, ' || ') .. '\n|}'
return videoTable .. "\n\n"
else
return " "
end
end
-- Function to get specialized quantities
function p.getSpecializedQuantities(frame)
local entityId = frame.args[1]
-- Validate input parameter
if not entityId or entityId == '' then
return "Error: No entity ID provided"
end
-- Constructing the SPARQL query with dynamic entity entityId
-- P1684: specialized by property id
local sparqlQuery = [[
PREFIX entityId: <https://portal.mardi4nfdi.de/entity/]] .. entityId .. [[>
SELECT ?URL ?Label
WHERE {
entityId: wdt:P1684 ?URL.
?URL rdfs:label ?Label
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
-- Validate results
if not jsonResults or not jsonResults.results or not jsonResults.results.bindings then
return "No specialized research fields found"
end
local specializedQuantities = {}
-- Get the number of specialized research fields
local totalQuantities = #jsonResults.results.bindings
-- Loop through the bindings
for index = 0, totalQuantities do
local item = jsonResults.results.bindings[index]
if not item.Label.value then
return "Error: Missing item.Label.value"
elseif not item.URL.value then
return "Error: Missing item.URL.value"
else
local label = item.Label.value
local url = item.URL.value
local numericId = url:match("Q(%d+)")
local urlRendered = "https://portal.mardi4nfdi.de/wiki/Quantity:" .. numericId
local labelWithUrl = string.format('[%s %s]', tostring(urlRendered), tostring(label))
table.insert(specializedQuantities, "| " .. labelWithUrl)
end
end
-- Construct the Wikitext table
local wikitextTable = "{| class='wikitable'\n" .. table.concat(specializedQuantities, "\n|-\n") .. "\n|}"
return wikitextTable
end
return p