Module:SPARQLMentions: Difference between revisions

From TDWG Collection Description
Jump to navigation Jump to search
(Created page with "local p = {} -- Return the label of a given data item, formatted for linking local function getLabel(frame, id, prefix) if prefix == nil then prefix = '' end if id == "defa...")
 
No edit summary
Line 50: Line 50:
end
end


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

Revision as of 13:00, 13 May 2020

Documentation for this module may be created at Module:SPARQLMentions/doc

local p = {}

-- Return the label of a given data item, formatted for linking
local function getLabel(frame, id, prefix)
	if prefix == nil then prefix = '' end
	if id == "defaultView:BubbleChart" then id = "Q24515280" end
	if id == "defaultView:Graph" then id = "Q24515287" end
	if id == "defaultView:Map" then id = "Q24515275" end
    if id == "defaultView:ImageGrid" then id = "Q24515278" end
	if id == "wikibase:box"  then id = "Q26211169" end
	if id == "wikibase:cornerWest" then id = "Q26211177" end
	label = mw.wikibase.label( id )
	if label == nil then
		linktext = id
		extra = ''
	else 
		linktext = label .. " (" .. id .. ")"
		extra = ' '
	end
    link = mw.wikibase.getEntityUrl( id )
	return '[' .. link .. ' ' .. linktext .. ']' .. extra;
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], 'cd:(Q%d+)')
	return table.concat(map(function(it) return getLabel(frame, it) end, 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', 'wdtn', 'psn', 'pqn', 'prn', 'wdno' }
	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(frame, it, 'Property:') end, items), ", ")
end

function p.getFunctions(frame)
	if not mw.wikibase then
		return "no mw.wikibase"
	end
	
	seen = {}
	items = {}
	func = {'defaultView:BubbleChart', 'defaultView:Graph', 'defaultView:Map', 'defaultView:ImageGrid', 'wikibase:box', "wikibase:cornerWest" }
	for _, pr in ipairs(func) do
		items = getAll(frame.args[1], pr, seen, items)
	end
	return table.concat(map(function(it) return getLabel(frame, it) end, items), ", ")
end


return p