Module:SPARQLMentions: Difference between revisions
Jump to navigation
Jump to search
(add getTitle) |
m (1 revision imported) |
(No difference)
|
Latest revision as of 13:24, 11 December 2023
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 = ' ' .. frame:expandTemplate({ title="Reasonator", args= { [1] = id}})
.. ' ' .. frame:expandTemplate({ title="SQID", args= { [1] = id}})
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], 'wd:(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
function p.getTitle(frame)
if not mw.wikibase then
return "no mw.wikibase"
end
for line in string.gmatch(frame.args[1], "[^%c]+%c?") do
local title = string.match(line, '^#title:%s*([^%c]+)')
if not (title == nil) then
return title
end
end
end
return p