Module:SPARQL
Appearance
This module is for better display of SPARQL queries to Wikidata. It is used by Template:SPARQL2
It has two functions:
getItems( SPARQL )
[edit]- Produces the list of links to items mentioned in the query
getProperties( SPARQL )
[edit]- Produces the list of links to properties mentioned in the query
local p = {}
-- Return the label of a given data item, formatted for linking
local function getLabel(id, prefix)
if prefix == nil then prefix = '' end
label = mw.wikibase.label( id )
if label == nil then
linktext = id
else
linktext = label .. " (" .. id .. ")"
end
link = 'https://www.wikidata.org/wiki/' .. prefix .. id
return '[' .. link .. ' ' .. linktext .. ']';
end
local function map(func, array)
local new_array = {}
for i,v in ipairs(array) do
new_array[i] = func(v)
end
return new_array
end
local function getAll(text, regexp, seen, items)
if items == nil then items = {} end
if seen == nil then seen = {} end
for item in string.gmatch(text, regexp) do
if seen[item] == nil then
seen[item] = 1
table.insert(items, item)
end
end
return items
end
-- Produce list of items mentioned in the query
function p.getItems(frame)
if not mw.wikibase then
return "no mw.wikibase"
end
items = getAll(frame.args[1], 'wd:(Q%d+)')
return table.concat(map(getLabel, items), ", ")
end
-- Produce list of properties mentioned in the query
function p.getProperties(frame)
if not mw.wikibase then
return "no mw.wikibase"
end
seen = {}
items = {}
prefixes = {'wdt', 'p', 'ps', 'pr', 'pq', 'psv', 'pv', 'prv', 'pqv' }
for _, pr in ipairs(prefixes) do
items = getAll(frame.args[1], pr .. ':(P%d+)', seen, items)
end
return table.concat(map(function(it) return getLabel(it, 'Property:') end, items), ", ")
end
return p