Jump to content

Modulus:Redirection

E Vicipaedia
Icon documentationis Documentatio moduli[crea]
require[[strict]]


local iface = {}


iface.call_info_template = function (frame)
	local rname
	local tname
	local srcpage
	local opts = frame.args
	if opts[1] ~= nil then tname = opts[1]:match'^%s*(.*%S)' end
	if tname == nil then error('No template name was provided', 0) end
	if opts[2] ~= nil then rname = opts[2]:match'^%s*(.*%S)' end
	if rname == nil then srcpage = mw.title.getCurrentTitle()
	else
		srcpage = mw.title.new(rname)
		if srcpage == nil then srcpage = mw.title.getCurrentTitle() end
	end
	local targetpage = srcpage.redirectTarget
	if not targetpage then return '' end
	local newargs = {
		sourcePage = srcpage.fullText
	}
	for key, val in pairs(targetpage) do
		if type(val) == 'string' then newargs[key] = val
		elseif type(val) == 'number' or type(val) == 'boolean' then
			newargs[key] = tostring(val)
		end
	end
	for key, val in pairs(opts) do
		if type(key) ~= 'number' or key < 1 then newargs[key] = val
		elseif key > 2 then newargs[key - 2] = val end
	end
	return frame:getParent():expandTemplate{
		title = tname,
		args = newargs
	}
end


iface.get_target_page = function (frame)
	local rname
	local srcpage
	local opts = frame.args
	if opts[1] ~= nil then rname = opts[1]:match'^%s*(.*%S)' end
	if rname == nil then srcpage = mw.title.getCurrentTitle()
	else
		srcpage = mw.title.new(rname)
		if srcpage == nil then srcpage = mw.title.getCurrentTitle() end
	end
	local targetpage = srcpage.redirectTarget
	if targetpage then return targetpage.fullText end
	return ''
end


iface.walk_through = function (frame)
	local rname
	local srcpage
	local opts = frame.args
	if opts[1] ~= nil then rname = opts[1]:match'^%s*(.*%S)' end
	if rname == nil then srcpage = mw.title.getCurrentTitle()
	else
		srcpage = mw.title.new(rname)
		if srcpage == nil then srcpage = mw.title.getCurrentTitle() end
	end
	local thiswikilink = ''
	local targetpage = srcpage.redirectTarget
	local nextwikilink
	local walkedthrough = { srcpage.fullText }
	while targetpage do
		nextwikilink = targetpage.fullText
		if (walkedthrough[nextwikilink]) then break end
		thiswikilink = nextwikilink
		walkedthrough[nextwikilink] = true
		targetpage = targetpage.redirectTarget
	end
	return thiswikilink
end


return iface