Module:SPARQL

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

-- Standard map function 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

-- Get all instances of certain regexp in the text local function getAll(text, regexp) items = {} seen = {} 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

if not mw.wikibase then return "no mw.wikibase" end

items = getAll(frame.args[1], 'wdt:(P%d+)') return table.concat(map(function(it) return getLabel(it, 'Property:') end, items), ", ") end

return p