Difference between revisions of "Module:SPARQLMentions"
Jump to navigation
Jump to search
wd>Smalyshev (WMF) (add new normalized predicates) |
m (2 revisions imported: Import of useful Templates and Modules from Wikidata) |
||
(One intermediate revision by one other user not shown) | |||
Line 20: | Line 20: | ||
end | end | ||
link = mw.wikibase.getEntityUrl( id ) | link = mw.wikibase.getEntityUrl( id ) | ||
− | return '[' .. link .. ' ' .. linktext .. ']' | + | return '[' .. link .. ' ' .. linktext .. ']'; |
end | end | ||
Latest revision as of 20:47, 6 January 2022
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 .. ']';
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
return p