Module:PersonOtherProperties: Difference between revisions
Created page with "-- Required modules for SPARQL queries and HTML table generation local sparql = require('SPARQL') local mwHtml = require('mw.html') -- Main table to hold all functions local p = {} -- Function to build a HTML table from SPARQL query results function p.buildTableFromSparql(frame) -- Retrieve target1 from frame arguments or return error message if not set local target1 = frame.args[1] if not target1 or target1 == '' then return "No records found" e..." |
No edit summary |
||
(40 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
-- Required module containing helper methods | |||
local helper = require('Module:HelperMethods') | |||
-- Required modules for SPARQL queries and HTML table generation | -- Required modules for SPARQL queries and HTML table generation | ||
local sparql = require('SPARQL') | local sparql = require('SPARQL') | ||
Line 10: | Line 13: | ||
-- Retrieve target1 from frame arguments or return error message if not set | -- Retrieve target1 from frame arguments or return error message if not set | ||
local | local target = frame.args[1] | ||
if not | if not target or target == '' then | ||
return "No records found" | return "No records found" | ||
end | end | ||
Line 17: | Line 20: | ||
-- Constructing the SPARQL query with dynamic entity target1 | -- Constructing the SPARQL query with dynamic entity target1 | ||
local sparqlQuery = [[ | local sparqlQuery = [[ | ||
PREFIX | PREFIX target: <https://portal.mardi4nfdi.de/entity/]] .. target .. [[> | ||
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/> | PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/> | ||
PREFIX wd: <https://portal.mardi4nfdi.de/entity/> | PREFIX wd: <https://portal.mardi4nfdi.de/entity/> | ||
SELECT ?property ?propertyLabel ?value ?valueLabel | SELECT ?property ?propertyLabel ?value ?valueLabel | ||
WHERE { | WHERE { | ||
target: ?p ?value . | target: ?p ?value . | ||
?property ?ref ?p . | ?property ?ref ?p . | ||
?property a wikibase:Property . | ?property a wikibase:Property . | ||
OPTIONAL { | OPTIONAL { | ||
Line 39: | Line 35: | ||
} | } | ||
FILTER ( | FILTER NOT EXISTS { FILTER(STRSTARTS(STR(?value), "http")) } | ||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | ||
} | } | ||
ORDER BY ?propertyLabel | |||
]] | ]] | ||
Line 54: | Line 53: | ||
end | end | ||
if not jsonResults then | |||
return "Could not fetch data." | |||
end | |||
if | |||
if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then | |||
return "No records found." | |||
end | end | ||
-- Convert the JSON results into a Lua table | -- Convert the JSON results into a Lua table | ||
local dataTable = convertJsonToTable(jsonResults) | local dataTable = helper.convertJsonToTable(jsonResults) | ||
-- Create and return a HTML table from the data | -- Create and return a HTML table from the data | ||
local headers = {"Property", "Value"} | |||
return helper.createHtmlTableWithMergedCols(dataTable, headers, {{2, 3}, {1}}) | |||
end | end | ||
-- Return the created html table | -- Return the created html table | ||
return p | return p |
Latest revision as of 17:32, 7 February 2024
Overview
This Lua module queries the local SPARQL endpoints to show all known properties that are having a direct value (e.g. birthdate) and not a relationship (e.g doctoral student of).
Dependencies
SPARQL Module: Used for executing SPARQL queries. mw.html Module: Utilized for constructing HTML content, particularly for table generation.
Module Functions
buildTableFromSparql(frame)
Constructs an HTML table based on SPARQL query results related to a specified entity.
- Parameters
- * frame: A frame object containing arguments passed to the module. The first argument should be the entity ID.
- Returns
- * A string representing an HTML table with the SPARQL query results.
- * If the target entity ID is not provided, it returns "No records found".
- Description
- This function forms a SPARQL query using the provided entity ID, executes the query, and processes the results into a formatted HTML table. It handles errors in SPARQL query execution and logs them for debugging. The table contains columns for properties and their values related to the entity.
convertJsonToTable(jsonResults)
Converts JSON-formatted SPARQL query results into a Lua table.
- Parameters
- * jsonResults: The JSON object containing the SPARQL query results.
- Returns
- * A Lua table representation of the SPARQL query results.
- Description
- Parses the JSON results from a SPARQL query and converts them into a structured Lua table for further processing.
createHtmlTableWithMergedCols(dataTable, headers)
Generates an HTML table from a Lua table with specified headers, merging certain columns for readability.
- Parameters
- * dataTable: The data table obtained from SPARQL query results.
- * headers: A table of strings representing the headers for the HTML table.
- Returns
- * A string representing the constructed HTML table.
- Description
- Constructs an HTML table using mw.html. It merges specific columns from the data table based on their content (URLs or literal values) for better presentation in the HTML format.
Usage Example
To use this module in a MediaWiki template or page, invoke it with the necessary parameters, such as the Wikidata entity ID:
{{#invoke:YourModuleName|buildTableFromSparql|Q161115}}
Replace YourModuleName with the actual name of this module. The parameter (e.g., 'Q161115') should be the Wikidata entity ID for which you want to fetch and display data.
Debugging Example
For testing or debugging purposes, you can simulate the module's function in a Lua sandbox using:
local mockFrame = { args = { 'Q161115' } }
local result = p.buildTableFromSparql(mockFrame)
mw.log(result)
Replace 'Q161115' with the desired entity ID.
-- Required module containing helper methods
local helper = require('Module:HelperMethods')
-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local mwHtml = require('mw.html')
-- Main table to hold all functions
local p = {}
-- Function to build a HTML table from SPARQL query results
function p.buildTableFromSparql(frame)
-- Retrieve target1 from frame arguments or return error message if not set
local target = frame.args[1]
if not target or target == '' then
return "No records found"
end
-- Constructing the SPARQL query with dynamic entity target1
local sparqlQuery = [[
PREFIX target: <https://portal.mardi4nfdi.de/entity/]] .. target .. [[>
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
SELECT ?property ?propertyLabel ?value ?valueLabel
WHERE {
target: ?p ?value .
?property ?ref ?p .
?property a wikibase:Property .
OPTIONAL {
?value rdfs:label ?valueLabel .
FILTER(LANG(?valueLabel) = "en")
}
FILTER NOT EXISTS { FILTER(STRSTARTS(STR(?value), "http")) }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?propertyLabel
]]
-- Executing the SPARQL query and retrieving results in JSON format
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 dataTable = helper.convertJsonToTable(jsonResults)
-- Create and return a HTML table from the data
local headers = {"Property", "Value"}
return helper.createHtmlTableWithMergedCols(dataTable, headers, {{2, 3}, {1}})
end
-- Return the created html table
return p