Module:Formula
From MaRDI portal
Documentation for this module may be created at Module:Formula/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 check whether the item has the MathModDB community tag
function p.hasMathModDB(frame)
local entity = mw.wikibase.getEntity()
if not entity then return false end
local statements = entity.claims and entity.claims['P1495']
if not statements then return false end
for _, statement in ipairs(statements) do
local val = statement.mainsnak
and statement.mainsnak.datavalue
and statement.mainsnak.datavalue.value
if val == "MathModDB" then
return true
elseif type(val) == "table" and val.id == "Q6534265" then -- if it's an item reference
return true
end
end
return false
end
-- 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 url = "https://portal.mardi4nfdi.de/wiki/Item:"
local fullUrl = url .. pair[3]
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
return p