Module:QueryPage

Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

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

Code

-- This was an attempt to rewrite the logic of [[Template:query page]] in Lua.
-- Unfortunately this doesn't quite work as expected because Scribunto doesn't let us access grandparents
-- (https://phabricator.wikimedia.org/T196501#8431141).
-- This means we cannot introduce new parameters since the existing query pages don't pass them through.

local p = {}

local styles = {}

function styles.query (args)
	return args.query
end

function styles.url (args)
	if args.autoexec then
		return 'https://query.wikidata.org/embed.html#' .. mw.uri.encode(args.query, 'PATH')
	else
		return 'https://query.wikidata.org/#' .. mw.uri.encode(args.query, 'PATH')
	end
end

function styles.link (args)
	local out = link(styles.url(args), args.title or '{{{title}}}')
	if args.show_source then
		-- Using a template for localization for now, see https://phabricator.wikimedia.org/T238417.
		local label = mw.getCurrentFrame():expandTemplate{title='int source'}
		out = out .. ' (' .. link_raw_label(args.original_tweet_url or original_toot_url or error('expected original_tweet_url or original_toot_url'), label) ..')'
	end
	return out
end

function styles.link_embed (args)
	return _link(styles.url_embed(args), args)
end

function styles.full (args)
	local div = mw.html.create('div')
	local links = {}
	
	if args.title_tag then
		if not args.title_tag:match('^h[1-6]$') then
			error('expected title_tag to match h[1-6]')
		end
		div:tag(args.title_tag):wikitext(args.title or '{{{title}}}')
	end
	
	if args.original_tweet_url then
		table.insert(links, link(args.original_tweet_url, 'on Twitter'))
	end
	if args.original_toot_url then
		table.insert(links, link(args.original_toot_url, 'on Mastodon'))
	end
	
	if #links > 0 then
		local p = div:tag('p'):wikitext('Originally posted ' .. table.concat(links, ' and ') .. '.')
		
		if args.header_wikitext_after_original then
			p:wikitext(' ' .. args.header_wikitext_after_original)
		end
	end
	
	if args.header_wikitext_paragraph then
		div:tag('p'):wikitext(args.header_wikitext_paragraph)
	end
	
	local frame = mw.getCurrentFrame()
	div:wikitext(frame:expandTemplate{title='SPARQL', args={query=args.query}})
	
	if args.footer_wikitext then
		div:tag('p'):wikitext(args.footer_wikitext)	
	end
	
	return tostring(div)
end

-- debug with e.g. =p.main(nil, {query='SELECT (1 AS ?example) WHERE {}'})
function p.main (frame, debugArgs)
	local args = debugArgs or frame:getParent().args
	
	if args[1] then
		-- Most likely means the user used '|' in the query parameter and forgot to escape it as {{!}}.
		error('Parameter 1 specified, but not expected by this template. Most likely you need to replace | with {{!}} in the query page.')
	end
	
	if args.style == '' then
		args.style = 'full'
	end
	
	local style_func = styles[(args.style or 'full'):gsub(' ', '_')]
	if not style_func then
		local names = {}
		for style, _ in pairs(styles) do
			local name = style:gsub('_', ' ')
			table.insert(names, name)
		end
		table.sort(names)
		error('unknown style "' .. args.style .. '", expected one of ' .. table.concat(names, ', '))
	end
	return style_func(args)
end

-- debug with =p.listStyles()
function p.listStyles ()
	local style_names = {}
	for style, _ in pairs(styles) do
		style = style:gsub('_', ' ')
		table.insert(style_names, style)
	end
	table.sort(style_names)
	local ul = mw.html.create('ul')
	for _, style in pairs(style_names) do
		ul:tag('li'):wikitext(style)
	end
	return tostring(ul)
end

function link_raw_label(url, label)
	return '[' .. url .. ' ' .. label .. ']'
end

function link(url, label)
	return '[' .. url .. ' ' .. mw.text.nowiki(label) .. ']'
end

-- legacy styles

function styles.url_embed (args)
	args.autoexec = 'yes'
	return styles.url(args)
end

function styles.link_with_source (args)
	args.show_source = 'yes'
	return styles.link(args)
end

function styles.link_embed_with_source (args)
	args.show_source = 'yes'
	return styles.link_embed(args)
end

return p