Module:MathModDBHelperMethods: Difference between revisions
From MaRDI portal
Created page with "-- 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 replace pattern in the comma pattern separated list function p.replace_pattern(frame) local input = frame.args[1] or "" local sep = frame.args[2] or " " -- default to space if no separator given -- Replace comma+pattern (,xxxx) with sep local result = string.gsub(input, ",xxxx%s*", sep..." |
EloiFerrer (talk | contribs) No edit summary |
||
| Line 1: | Line 1: | ||
-- Module for executing SPARQL queries | |||
local sparql = require('SPARQL') | |||
-- Required module containing helper methods | |||
local helper = require('Module:HelperMethods') | |||
-- MediaWiki library for logging and utilities | -- MediaWiki library for logging and utilities | ||
local mw = require('mw') | local mw = require('mw') | ||
| Line 13: | Line 17: | ||
local result = string.gsub(input, ",xxxx%s*", sep) | local result = string.gsub(input, ",xxxx%s*", sep) | ||
return result | return result | ||
end | |||
-- Function to generate a table listing a type of individuals | |||
function p.getList(frame) | |||
local entityType = frame.args[1] or 'Model' | |||
local height = frame.args[2] or '400px' -- Default height if not specified | |||
local width = frame.args[3] or '800px' -- Default width if not specified | |||
local baseUrl = mw.site.server -- Get the current URL | |||
-- Define mapping of entity types to their corresponding Q IDs and URL prefixes | |||
local entityConfig = { | |||
['Model'] = {qid = 'Q68663', urlPrefix = 'Model:', title = 'mathematical models'}, | |||
['Research field'] = {qid = 'Q60231', urlPrefix = 'Research_field:', title = 'research fields'}, | |||
['Research problem'] = {qid = 'Q6032837', urlPrefix = 'Research_problem:', title = 'research problems'}, | |||
['Task'] = {qid = 'Q6534247', urlPrefix = 'Task:', title = 'tasks'}, | |||
['Quantity'] = {qid = 'Q6534237', urlPrefix = 'Quantity:', title = 'quantities'}, | |||
['Quantity kind'] = {qid = 'Q6534245', urlPrefix = 'Quantity:', title = 'quantity kind items'}, | |||
['Formula'] = {qid = 'Q6481152', urlPrefix = 'Formula:', title = 'formulas'} | |||
} | |||
-- Get configuration for the specified entity type | |||
local config = entityConfig[entityType] | |||
if not config then | |||
return "Invalid entity type. Valid options are: Model, Research field, Research problem, Task, Quantity, Quantity Kind, Formula" | |||
end | |||
-- Q6534265 refers to MathModDB community | |||
local sparqlQuery = [[ | |||
SELECT ?itemLabel ?modelURL | |||
WHERE { | |||
?item wdt:P31 wd:]] .. config.qid .. [[; | |||
wdt:P1495 wd:Q6534265 . | |||
?item rdfs:label ?itemLabel . | |||
FILTER(LANG(?itemLabel) = "en") | |||
BIND(REPLACE(STR(?item), "^.*/Q", "]] .. baseUrl .. [[/wiki/]] .. config.urlPrefix .. [[") AS ?modelURL) | |||
} | |||
ORDER BY ASC(?itemLabel) | |||
]] | |||
local jsonResults = sparql.runQuery(sparqlQuery) | |||
-- Handle error in SPARQL query execution | |||
if jsonResults and jsonResults.error then | |||
mw.log("Error in SPARQL query: " .. tostring(jsonResults.error)) | |||
return nil | |||
end | |||
if not jsonResults then | |||
return "Could not fetch data." | |||
end | |||
if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then | |||
return "No records found." | |||
end | |||
-- Convert the JSON results into a Lua table | |||
local fieldOrder = {"modelURL", "itemLabel"} | |||
local dataTable = helper.convertJsonToTableOrdered(jsonResults, fieldOrder) | |||
-- Create and return HTML table from the data | |||
local headers = {"Model"} | |||
local htmlTable = helper.createHtmlTableWithMergedCols(dataTable, headers, {{1, 2}}) | |||
-- Create a parent container for the table | |||
local parentContainer = mw.html.create('div') | |||
:addClass('parent-container') | |||
:css('width', width) | |||
local heading = mw.html.create('h2') | |||
:wikitext('List of ' .. config.title) | |||
-- Add the table and chart to the parent container | |||
parentContainer | |||
:node(heading) | |||
:node(htmlTable) | |||
return tostring(parentContainer) | |||
end | end | ||
return p | return p | ||
Revision as of 11:49, 13 August 2025
Documentation for this module may be created at Module:MathModDBHelperMethods/doc
-- Module for executing SPARQL queries
local sparql = require('SPARQL')
-- Required module containing helper methods
local helper = require('Module:HelperMethods')
-- 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 replace pattern in the comma pattern separated list
function p.replace_pattern(frame)
local input = frame.args[1] or ""
local sep = frame.args[2] or " " -- default to space if no separator given
-- Replace comma+pattern (,xxxx) with sep
local result = string.gsub(input, ",xxxx%s*", sep)
return result
end
-- Function to generate a table listing a type of individuals
function p.getList(frame)
local entityType = frame.args[1] or 'Model'
local height = frame.args[2] or '400px' -- Default height if not specified
local width = frame.args[3] or '800px' -- Default width if not specified
local baseUrl = mw.site.server -- Get the current URL
-- Define mapping of entity types to their corresponding Q IDs and URL prefixes
local entityConfig = {
['Model'] = {qid = 'Q68663', urlPrefix = 'Model:', title = 'mathematical models'},
['Research field'] = {qid = 'Q60231', urlPrefix = 'Research_field:', title = 'research fields'},
['Research problem'] = {qid = 'Q6032837', urlPrefix = 'Research_problem:', title = 'research problems'},
['Task'] = {qid = 'Q6534247', urlPrefix = 'Task:', title = 'tasks'},
['Quantity'] = {qid = 'Q6534237', urlPrefix = 'Quantity:', title = 'quantities'},
['Quantity kind'] = {qid = 'Q6534245', urlPrefix = 'Quantity:', title = 'quantity kind items'},
['Formula'] = {qid = 'Q6481152', urlPrefix = 'Formula:', title = 'formulas'}
}
-- Get configuration for the specified entity type
local config = entityConfig[entityType]
if not config then
return "Invalid entity type. Valid options are: Model, Research field, Research problem, Task, Quantity, Quantity Kind, Formula"
end
-- Q6534265 refers to MathModDB community
local sparqlQuery = [[
SELECT ?itemLabel ?modelURL
WHERE {
?item wdt:P31 wd:]] .. config.qid .. [[;
wdt:P1495 wd:Q6534265 .
?item rdfs:label ?itemLabel .
FILTER(LANG(?itemLabel) = "en")
BIND(REPLACE(STR(?item), "^.*/Q", "]] .. baseUrl .. [[/wiki/]] .. config.urlPrefix .. [[") AS ?modelURL)
}
ORDER BY ASC(?itemLabel)
]]
local jsonResults = sparql.runQuery(sparqlQuery)
-- Handle error in SPARQL query execution
if jsonResults and jsonResults.error then
mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
return nil
end
if not jsonResults then
return "Could not fetch data."
end
if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
return "No records found."
end
-- Convert the JSON results into a Lua table
local fieldOrder = {"modelURL", "itemLabel"}
local dataTable = helper.convertJsonToTableOrdered(jsonResults, fieldOrder)
-- Create and return HTML table from the data
local headers = {"Model"}
local htmlTable = helper.createHtmlTableWithMergedCols(dataTable, headers, {{1, 2}})
-- Create a parent container for the table
local parentContainer = mw.html.create('div')
:addClass('parent-container')
:css('width', width)
local heading = mw.html.create('h2')
:wikitext('List of ' .. config.title)
-- Add the table and chart to the parent container
parentContainer
:node(heading)
:node(htmlTable)
return tostring(parentContainer)
end
return p