Module:PersonOtherRelations
Overview
This Lua module queries the local SPARQL endpoints to show all known relations and their values (e.g. is doctoral student of) and not a value (e.g birthdate).
The module operates by executing the SPARQL query against a specified Wikidata entity. The query is constructed to retrieve a set of properties (?property), their labels (?propertyLabel), and their corresponding values (?value) for the target entity. In cases where a value is another Wikidata entity, an optional clause in the query attempts to fetch the label (?valueLabel) for these entities, enhancing the readability of the results.
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 .
?value rdfs:label ?valueLabel .
FILTER(LANG(?valueLabel) = "en")
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, 4}, {1, 3}})
end
-- Return the created html table
return p