Module:QueryCode
Jump to navigation
Jump to search
Documentation for this module may be created at Module:QueryCode/doc
local p = {}
p.formatCode = function(frame)
local code, tag, is_code_page = frame.args[1], frame.args[2], frame.args[3]
local formattedCode = frame.args[4]
formattedCode = formattedCode:gsub('[QP][0-9]+', function(m)
local a = mw.html.create('a')
a:attr('href', 'https://www.wikidata.org/entity/' .. m)
a:wikitext(m)
return tostring(a)
end)
return formattedCode;
end
p.additionalOutput = function(frame, debugArgs)
local args = debugArgs or frame.args
local code, tag, is_code_page = args[1], args[2], args[3]
if args['show-results'] then
local resp = mw.ext.UnlinkedWikibase.query(code)
if not resp.results then
return
end
local tbl = mw.html.create('table')
local tr = tbl:tag('tr')
for _, var in pairs(resp.head.vars) do
tr:tag('th'):wikitext(mw.text.encode(var))
end
for _, res in pairs(resp.results.bindings) do
local tr = tbl:tag('tr')
for _, var in pairs(resp.head.vars) do
local td = tr:tag('td')
if res[var] then
td:wikitext(mw.text.encode(res[var].value))
end
end
end
return tostring(tbl)
end
end
return p