Module:R avoided double redirect

From TechInfoDepot
Jump to navigationJump to search
Documentation icon Module documentation[view] [edit] [history] [purge]

This module implements {{R avoided double redirect}}. Please see the template page for documentation.

  1 local p = {}
  2 
  3 local function noredir(page)
  4 	local link = page:fullUrl("redirect=no")
  5 	return "<span class=\"plainlinks\">[" .. link .. " " .. page.fullText .. "]</span>"
  6 end
  7 
  8 function p.main(frame)
  9 
 10 	local args = require("Module:Arguments").getArgs(frame, {removeBlanks=false})
 11 
 12 	-- Demo parameters, for demonstrating behavior with certain redirect 
 13 	-- targets and avoiding categorization (do not use in articles)
 14 	local noError = args.noerror
 15 	local demo = args.demo or noError or args.thistarget or args.othertarget
 16 
 17 	local function formatError(err)
 18 		return "<span class=\"error\">Error in [[Module:R avoided double redirect]]: " .. err .. "</span>"
 19 			.. (demo and "" or "[[Category:Avoided double redirects/error]]")
 20 	end
 21 
 22 	local thisPage = mw.title.getCurrentTitle()
 23 	local otherPage = mw.title.new(args[1] or "")
 24 	if not otherPage then
 25 		return formatError("No other page was specified.");
 26 	end
 27 	if mw.title.equals(thisPage, otherPage) then
 28 		return formatError("The current page was passed as the parameter.");
 29 	end
 30 
 31 	-- Get mw.title objects for redirect targets.
 32 	-- Note that using mw.title's redirectTarget will correctly handle preview mode, unlike Module:Redirect.
 33 	local thisTarget, otherTarget
 34 	if demo and args.thistarget then
 35 		thisTarget = mw.title.new(args.thistarget)
 36 	else
 37 		thisTarget = thisPage.redirectTarget
 38 	end
 39 	if demo and args.othertarget then
 40 		otherTarget = mw.title.new(args.othertarget)
 41 	else
 42 		otherTarget = otherPage.redirectTarget
 43 	end
 44 
 45 	-- For double redirects
 46 	local thisDoubleTarget = thisTarget and thisTarget.redirectTarget
 47 	local otherDoubleTarget = otherTarget and otherTarget.redirectTarget
 48 
 49 	local function formatOutput(update, info)
 50 		local from, cat
 51 
 52 		if otherTarget then
 53 			from = "an alternative title for '''" .. noredir(otherPage) .. "''', another redirect to the same title"
 54 		else
 55 			from = "an alternative title for '''[[:" .. otherPage.fullText .. "]]''', a former redirect to the same title"
 56 		end
 57 		cat = demo and "" or update and "Avoided double redirects to be updated" or "Avoided double redirects"
 58 
 59 		return frame:expandTemplate({
 60 			title = "Redirect template",
 61 			args = {
 62 				id = 'R avoided double redirect',
 63 				from = from,
 64 				info = update and "\n**" .. info or info,
 65 				["all category"] = cat,
 66 				name = "From an avoided double redirect"
 67 			}
 68 		})
 69 	end
 70 
 71 	if not noError then
 72 		if not thisTarget then
 73 			return formatError("This page is not a redirect.", demo)
 74 		elseif mw.title.equals(thisPage, thisTarget) then
 75 			return formatOutput(true, "<span class=\"error\">This is a broken redirect (it redirects to itself).</span>")
 76 		elseif not thisTarget.exists then
 77 			return formatOutput(true, "<span class=\"error\">This is a broken redirect (its target does not exist).</span>")
 78 		elseif not otherPage.exists then
 79 			return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] does not exist.</span>")
 80 		elseif otherTarget and mw.title.equals(otherPage, otherTarget) then
 81 			return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] is a broken redirect (it redirects to itself).</span>")
 82 		elseif otherTarget and not otherTarget.exists then
 83 			return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] is a broken redirect (it redirects to a page that does not exist).</span>")
 84 		elseif mw.title.equals(thisTarget, otherPage) then
 85 			if not otherTarget then
 86 				return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] is not a redirect, and this already points to it.</span> Most likely this template should be removed.")
 87 			elseif mw.title.equals(otherTarget, thisPage) then
 88 				return formatOutput(true, "<span class=\"error\">This is a circular redirect.</span> Please change the target of both this redirect and " .. noredir(otherPage) .. " to the correct article.")
 89 			end
 90 			return formatOutput(true, "<span class=\"error\">This page redirects to " .. noredir(otherPage) .. ", which redirects to [[:" .. otherTarget.fullText .. "]].</span> Please change this redirect's target to [[:" .. otherTarget.fullText .. "]] or otherwise resolve the situation.")
 91 		elseif not otherTarget then
 92 			return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] is not a redirect.</span> Most likely this redirect should be updated to point to [[:" .. otherPage.fullText .. "]] now that it is no longer a redirect, and this template removed.\n** If that is not the correct target for this redirect, update or remove this template and/or the redirect itself and/or the other page as appropriate.")
 93 		elseif thisDoubleTarget then
 94 			if otherDoubleTarget then
 95 				if mw.title.equals(thisDoubleTarget, otherDoubleTarget) then
 96 					return formatOutput(true, "<span class=\"error\">Both this page and " .. noredir(otherPage) .. " are double redirects.</span> Please change the redirect target of both to "
 97 						.. (thisDoubleTarget.isRedirect and "the correct article." or "[[:" .. thisDoubleTarget.fullText .. "]] (or some other correct article)."))
 98 				end
 99 				return formatOutput(true, "<span class=\"error\">Both this page and " .. noredir(otherPage) .. " are double redirects.</span> Please fix them.")
100 			end
101 			return formatOutput(true, "<span class=\"error\">This is a double redirect.</span> Please fix it, possibly by changing it to [[:" .. otherTarget.fullText .. "]].")
102 		elseif not mw.title.equals(thisTarget, otherTarget) then
103 			return formatOutput(true, "<span class=\"error\">This page and " .. noredir(otherPage) .. " redirect to different articles.</span> Most likely you should change this redirect's target to [[:" .. otherTarget.fullText .. "]] to match.\n** If that is not the correct target for this redirect, update or remove this template and/or the redirect itself and/or the other page as appropriate.")
104 		elseif thisTarget.fragment ~= otherTarget.fragment then
105 			-- Should this case report for update?
106 			return formatOutput(false, "Because [[Wikipedia:Double redirects|double redirects]] are disallowed,"
107 				.. " both pages currently point to [[:" .. otherTarget.prefixedText .. "]] (but with different anchors).\n"
108 				.. "**If " .. noredir(otherPage) .. " is expanded into a separate article or it is retargeted, "
109 				.. " this redirect will be recategorized to be updated."
110 			)
111 		end
112 	end
113 
114 	return formatOutput(false, "Because [[Wikipedia:Double redirects|double redirects]] are disallowed,"
115 		.. " both pages currently point to [[:" .. otherTarget.fullText .. "]].\n"
116 		.. "**If " .. noredir(otherPage) .. " is expanded into a separate article or it is retargeted, "
117 		.. " this redirect will be recategorized to be updated."
118 	)
119 end
120 
121 return p