Module:RfD
require('Module:No globals')
 
 local p = {}
 
 local getTargetFromText = require('Module:Redirect').getTargetFromText
 local messageBox
 
 local mboxText = &quot;%s'''The purpose of this redirect is currently being discussed by the Wikipedia community.&quot;
 	.. &quot; The outcome of the discussion may result in a change of this page, or possibly its deletion in accordance with Wikipedia's [[Wikipedia:Deletion policy|deletion policy]].''' &lt;br /&gt;&quot;
 	.. &quot; Please share your thoughts on the matter at '''[[Wikipedia:Redirects for discussion/Log/%s %s %s#%s|this redirect's entry]]''' on the [[Wikipedia:Redirects for discussion|Redirects for discussion]] page.&lt;br /&gt;&quot;
 	.. &quot;  '''Click on the link below''' to go to the current destination page.&lt;br /&gt;&quot;
 	.. &quot;&lt;small&gt;Please notify the good-faith creator and any main contributors of the redirect by placing &lt;code&gt;&amp;#123;&amp;#123;[[Wikipedia:Substitution|subst]]:[[Template:RFDNote|RFDNote]]&amp;#124;%s&amp;#125;&amp;#125; &amp;#126;&amp;#126;&amp;#126;&amp;#126;&lt;/code&gt; on their talk page.&lt;/small&gt;&quot;
 local errorMessage = '&lt;span class=&quot;error&quot;&gt;Error: Unable to determine the redirect\'s target. If this page is a soft redirect, then this error can be ignored. Otherwise, please make sure that [[WP:RFD#HOWTO|the instructions for placing this tag]] were followed correctly, in particular that the redirect\'s old content was passed in the &lt;code&gt;content&lt;/code&gt; parameter.&lt;/span&gt;&lt;br /&gt;'
 local deleteReason = '[[Wikipedia:Redirects for discussion]] debate closed as delete'
 
 local function makeRfdNotice(args)
 	local currentTitle = mw.title.getCurrentTitle()
 	if not messageBox then
 		messageBox = require('Module:Message box')
 	end
 	local discussionPage = args[1] and mw.text.trim(args[1])
 	if discussionPage == '' then
 		discussionPage = nil
 	end
 	local target = getTargetFromText(args.content)
 	local isError = not target or not mw.title.new(target)
 	local category
 	if args.category then
 		category = args.category
 	elseif args.month and args.year then
 		category = string.format('[[Category:Redirects for discussion from %s %s|%s]][[Category:All redirects for discussion|%s]]', args.month, args.year, currentTitle.text, currentTitle.text)
 	else
 		category = string.format('[[Category:Redirects for discussion|%s]][[Category:All redirects for discussion|%s]]', currentTitle.text, currentTitle.text)
 	end
 	return string.format('%s&lt;span id=&quot;delete-reason&quot; style=&quot;display:none;&quot;&gt;%s&lt;/span&gt;%s%s',
 		messageBox.main('mbox', {
 			type = 'delete',
 			image = 'none',
 			text = string.format(mboxText, isError and errorMessage or '', args.year, args.month, args.day, discussionPage or currentTitle.prefixedText, mw.text.nowiki(currentTitle.prefixedText))
 		}),
 		mw.uri.encode(deleteReason),
 		category,
 		isError and '[[Category:RfD errors]]' or ''
 	)
 end
 
 p[''] = function(frame)
 	if not frame.args.content or mw.text.trim(frame.args.content) == '' then
 		return '&lt;span class=&quot;error&quot;&gt;Error: No content was provided. The original text of the page (the #REDIRECT line and any templates) must be placed inside of the content parameter.[[Category:RfD errors]]&lt;/span&gt;'
 	end
 	local pframe = frame:getParent()
 	if pframe:preprocess('&lt;includeonly&gt;' .. os.clock() .. '&lt;/includeonly&gt;') ~= '' then
 		-- We're being transcluded, so display the content of our target.
 		local target = getTargetFromText(frame.args.content)
 		if target then
 			target = mw.title.new(target)
 		end
 		if target and not target.isRedirect and target ~= pframe:getTitle() then
 			-- We should actually be calling expandTemplate on the grandparent rather than on the parent, but we can't do that yet
 			-- Since we don't have grandparent access, though, it means the thing we're calling doesn't either, so it doesn't really matter yet
 			return pframe:expandTemplate{title = ':' .. target.prefixedText, args = pframe.args}
 		end
 	end
 	-- We're not being transcluded, or we can't figure out how to display our target.
 	-- Display the RfD banner.
 	return makeRfdNotice(frame.args) .. '\n' .. frame.args.content
 end
 
 local substText = &quot;{{&lt;includeonly&gt;safesubst:&lt;/includeonly&gt;#invoke:RfD||%s|%s%s\n&quot;
 	.. &quot;&lt;!-- The above content is generated by {{subst:rfd}}. --&gt;\n&lt;!-- End of RFD message. Don't edit anything above here, but feel free to edit below here. --&gt;|content=\n%s\n&quot;
 	.. &quot;&lt;!-- Don't add anything after this line. --&gt;\n}}&quot;
 local dateText = 'month = %B\n|day = %e\n|year = %Y\n|time = %R\n|timestamp = %Y%m%d%H%M%S'
 
 -- called during subst when the template is initially placed on the page
 function p.main(frame)
 	local titleText
 	local pframe = frame:getParent()
 	local pargs = pframe.args
 	local retval = string.format(substText, pargs.FULLPAGENAME or pargs[1] or '', os.date(dateText), pframe:getTitle() == mw.title.getCurrentTitle().prefixedText and '|category=' or '', pargs.content or '')
 	if mw.isSubsting() then
 		return retval
 	else
 		return frame:expandTemplate{title = 'Template:Error:must be substituted', args = {'rfd'}} .. frame:preprocess(retval)
 	end
 end