Module:R avoided double redirect: Difference between revisions

Source: Wikipedia, the free encyclopedia.
Content deleted Content added
update per talk page
Line 1: Line 1:
local p = {}
local p = {}


function p.main(frame)

local mRedirect = require("Module:Redirect")
local function noredir(page)
local function noredir(page)
local link = mw.title.new(page):fullUrl("redirect=no")
local link = page:fullUrl("redirect=no")
return "<span class=\"plainlinks\">["..link.." "..page.."]</span>"
return "<span class=\"plainlinks\">[" .. link .. " " .. page.fullText .. "]</span>"
end
local function process(page)
return mw.title.new(page).prefixedText
end
local function exists(page)
return mw.title.new(page).exists
end
end


local args = require("Module:Arguments").getArgs(frame,{removeBlanks=false})
local thisPage = tostring(mw.title.getCurrentTitle())
local otherPage = args[1] or ""


function p.main(frame)
--Demo parameters, for demonstrating behavior with certain redirect
--targets and avoiding categorization (do not use in articles)
local thisDemoTarget = args.thistarget
local otherDemoTarget = args.othertarget
local noError = args.noerror
local demo = args.demo or noError or thisDemoTarget or otherDemoTarget


local args = require("Module:Arguments").getArgs(frame, {removeBlanks=false})
--"Process" pages to remove section links, standardize capitalization, etc.
otherPage = otherPage=="" and "" or process(otherPage)
thisDemoTarget = thisDemoTarget=="" and "" or thisDemoTarget and process(thisDemoTarget)
otherDemoTarget = otherDemoTarget=="" and "" or otherDemoTarget and process(otherDemoTarget)


-- Demo parameters, for demonstrating behavior with certain redirect
--Determine redirect targets (getTarget returns nil if page is not a redirect)
-- targets and avoiding categorization (do not use in articles)
local thisTarget = thisDemoTarget or mRedirect.getTarget(thisPage)
local noError = args.noerror
local otherTarget = otherDemoTarget or mRedirect.getTarget(otherPage)
local demo = args.demo or noError or args.thistarget or args.othertarget
--For double redirects
local thisDoubleTarget = thisTarget and mRedirect.getTarget(thisTarget)
local otherDoubleTarget = otherTarget and mRedirect.getTarget(otherTarget)
-- Allow setting demo parameters to empty string to demonstrate a non-redirect
thisTarget = thisDemoTarget~="" and thisTarget
otherTarget = otherDemoTarget~="" and otherTarget


local function formatError(err)
--Errors
return "<span class=\"error\">Error in [[Module:R avoided double redirect]]: " .. err .. "</span>"
local err
.. (demo and "" or "[[Category:Avoided double redirects/error]]")
if not thisTarget and exists(thisPage) then --Check existence of thisPage to avoid errors in preview mode
end
err = "This page is not a redirect."
elseif otherPage=="" then
err = "No other page was specified."
elseif not exists(otherPage) then
err = "[["..otherPage.."]] does not exist."
elseif thisTarget==thisPage or (not exists(thisTarget) and exists(thisPage)) then
err = "This is a broken redirect (it redirects to itself or to a non-existing page)."
elseif otherTarget and (otherTarget==otherPage or not exists(otherTarget)) then
err = noredir(otherPage).." is a broken redirect (it redirects to itself or to a non-existing page)."
elseif otherPage==thisPage then
err = "The current page was passed as parameter."
elseif not otherTarget and thisTarget==otherPage then
err = "This page already redirects to [["..otherPage.."]]. Please remove this template."
elseif otherPage==thisTarget and otherTarget==thisPage then
err = "This is a circular redirect. Please change the target of both this redirect and "..noredir(otherPage).." to the correct article."
elseif thisDoubleTarget and otherDoubleTarget and thisDoubleTarget==otherDoubleTarget then
err = "Both this page and "..noredir(otherPage).." are double redirects. Please change the redirect target of both to "
..(mRedirect.luaIsRedirect(thisDoubleTarget) and "the correct article." or "[["..thisDoubleTarget.."]].")
elseif otherTarget and (thisTarget==otherPage or thisDoubleTarget==otherTarget) then
err = "This is a [[Wikipedia:Double redirects|double redirect]]."
.." Please change the redirect target to [["..otherTarget.."]]."
elseif otherTarget and thisDoubleTarget then
err = "This is a [[Wikipedia:Double redirects|double redirect]] to [["..thisDoubleTarget.."]], while "
..noredir(otherPage).." redirects to [["..otherTarget.."]]."
elseif thisDoubleTarget then
err = "This is a [[Wikipedia:Double redirects|double redirect]] to [["..thisDoubleTarget.."]]."
elseif otherTarget==thisPage then
err = noredir(otherPage).." is a [[Wikipedia:Double redirects|double redirect]]."
.." Please change its target to [["..thisTarget.."]]."
elseif otherDoubleTarget then
err = noredir(otherPage).." is a [[Wikipedia:Double redirects|double redirect]]"
.." to [["..otherDoubleTarget.."]] via [["..otherTarget.."]]."
elseif otherTarget and thisTarget~=otherTarget then
err = "This page and "..noredir(otherPage).." redirect to different articles."
end


local thisPage = mw.title.getCurrentTitle()
--Return either redirect template or error
local otherPage = mw.title.new(args[1] or "")
local from, info, cat
if not err or noError then
if not otherPage then
return formatError("No other page was specified.");
if otherTarget then
end
from = "an alternative title for '''"..noredir(otherPage).."''', another redirect to the same title"
if mw.title.equals(thisPage, otherPage) then
info = "Because [[Wikipedia:Double redirects|double redirects]] are disallowed,"
return formatError("The current page was passed as the parameter.");
.." both pages currently point to [["..otherTarget.."]].\n"
end
.."**If and when "..noredir(otherPage).." is expanded into a separate"

.." article, or its target article is renamed to "..noredir(otherPage)..", this"
-- Get mw.title objects for redirect targets.
.." redirect will be recategorized to be changed to target that article instead."
-- Note that using mw.title's redirectTarget will correctly handle preview mode, unlike Module:Redirect.
cat = demo and "" or "Avoided double redirects"
local thisTarget, otherTarget
else
if demo and args.thistarget then
from = "an alternative title for '''[["..otherPage.."]]''', a former redirect to the same title"
thisTarget = mw.title.new(args.thistarget)
info = "\n**Since [["..otherPage.."]] is now a separate article, please"
else
.." update this redirect's target to [["..otherPage.."]] and remove the"
thisTarget = thisPage.redirectTarget
.." {{[[Template:R avoided double redirect|R avoided double redirect]]}} template."
end
.."\n**If [["..otherPage.."]] is the wrong target for this redirect,"
if demo and args.othertarget then
.." edit this page and correct the page name in the template."
otherTarget = mw.title.new(args.othertarget)
cat = demo and "" or "Avoided double redirects to be updated"
else
end
otherTarget = otherPage.redirectTarget
return frame:expandTemplate({title="Redirect template",
end
args={from=from,info=info,["main category"]=cat,

name="From an avoided double redirect"}})
-- For double redirects
else
local thisDoubleTarget = thisTarget and thisTarget.redirectTarget
return "<span class=\"error\">Error in [[Module:R avoided double redirect]]: "..err.."</span>"
local otherDoubleTarget = otherTarget and otherTarget.redirectTarget
..(demo and "" or "[[Category:Avoided double redirects/error]]")

end
local function formatOutput(update, info)
local from, cat

if otherTarget then
from = "an alternative title for '''" .. noredir(otherPage) .. "''', another redirect to the same title"
else
from = "an alternative title for '''[[:" .. otherPage.fullText .. "]]''', a former redirect to the same title"
end
cat = demo and "" or update and "Avoided double redirects to be updated" or "Avoided double redirects"

return frame:expandTemplate({
title = "Redirect template",
args = {
from = from,
info = update and "\n**" .. info or info,
["all category"] = cat,
name = "From an avoided double redirect"
}
})
end

if not thisTarget then
return formatError("This page is not a redirect.", demo)
end
if mw.title.equals(thisPage, thisTarget) then
return formatOutput(true, "<span class=\"error\">This is a broken redirect (it redirects to itself).</span>")
end
if not thisTarget.exists then
return formatOutput(true, "<span class=\"error\">This is a broken redirect (its target does not exist).</span>")
end
if not otherPage.exists then
return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] does not exist.</span>")
end
if otherTarget and mw.title.equals(otherPage, otherTarget) then
return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] is a broken redirect (it redirects to itself).</span>")
end
if otherTarget and not otherTarget.exists then
return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] is a broken redirect (it redirects to a page that does not exist).</span>")
end
if mw.title.equals(thisTarget, otherPage) then
if not otherTarget then
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.")
end
if mw.title.equals(otherTarget, thisPage) then
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.</span>")
end
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.")
end
if not otherTarget then
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.")
end
if thisDoubleTarget then
if otherDoubleTarget then
if mw.title.equals(thisDoubleTarget, otherDoubleTarget) then
return formatOutput(true, "<span class=\"error\">Both this page and " .. noredir(otherPage) .. " are double redirects.</span> Please change the redirect target of both to "
.. (thisDoubleTarget.isRedirect and "the correct article." or "[[" .. thisDoubleTarget.fullText .. "]] (or some other correct article)."))
end
return formatOutput(true, "<span class=\"error\">Both this page and " .. noredir(otherPage) .. " are double redirects.</span> Please fix them.")
end
return formatOutput(true, "<span class=\"error\">This is a double redirect.</span> Please fix it, possibly by changing it to [[:" .. otherTarget.fullText .. "]].")
end
if not mw.title.equals(thisTarget, otherTarget) then
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.")
end

if thisTarget.fragment ~= otherTarget.fragment then
-- Should this case report for update?
return formatOutput(false, "Because [[Wikipedia:Double redirects|double redirects]] are disallowed,"
.. " both pages currently point to [[" .. otherTarget.prefixedText .. "]] (but with different anchors).\n"
.. "**If " .. noredir(otherPage) .. " is expanded into a separate article or it is retargeted, "
.. " this redirect will be recategorized to be updated."
)
end

return formatOutput(false, "Because [[Wikipedia:Double redirects|double redirects]] are disallowed,"
.. " both pages currently point to [[" .. otherTarget.fullText .. "]].\n"
.. "**If " .. noredir(otherPage) .. " is expanded into a separate article or it is retargeted, "
.. " this redirect will be recategorized to be updated."
)


end
end

Revision as of 21:58, 25 June 2020

local p = {}

local function noredir(page)
	local link = page:fullUrl("redirect=no")
	return "<span class=\"plainlinks\">[" .. link .. " " .. page.fullText .. "]</span>"
end


function p.main(frame)

	local args = require("Module:Arguments").getArgs(frame, {removeBlanks=false})

	-- Demo parameters, for demonstrating behavior with certain redirect 
	-- targets and avoiding categorization (do not use in articles)
	local noError = args.noerror
	local demo = args.demo or noError or args.thistarget or args.othertarget

	local function formatError(err)
		return "<span class=\"error\">Error in [[Module:R avoided double redirect]]: " .. err .. "</span>"
			.. (demo and "" or "[[Category:Avoided double redirects/error]]")
	end

	local thisPage = mw.title.getCurrentTitle()
	local otherPage = mw.title.new(args[1] or "")
	if not otherPage then
		return formatError("No other page was specified.");
	end
	if mw.title.equals(thisPage, otherPage) then
		return formatError("The current page was passed as the parameter.");
	end

	-- Get mw.title objects for redirect targets.
	-- Note that using mw.title's redirectTarget will correctly handle preview mode, unlike Module:Redirect.
	local thisTarget, otherTarget
	if demo and args.thistarget then
		thisTarget = mw.title.new(args.thistarget)
	else
		thisTarget = thisPage.redirectTarget
	end
	if demo and args.othertarget then
		otherTarget = mw.title.new(args.othertarget)
	else
		otherTarget = otherPage.redirectTarget
	end

	-- For double redirects
	local thisDoubleTarget = thisTarget and thisTarget.redirectTarget
	local otherDoubleTarget = otherTarget and otherTarget.redirectTarget

	local function formatOutput(update, info)
		local from, cat

		if otherTarget then
			from = "an alternative title for '''" .. noredir(otherPage) .. "''', another redirect to the same title"
		else
			from = "an alternative title for '''[[:" .. otherPage.fullText .. "]]''', a former redirect to the same title"
		end
		cat = demo and "" or update and "Avoided double redirects to be updated" or "Avoided double redirects"

		return frame:expandTemplate({
			title = "Redirect template",
			args = {
				from = from,
				info = update and "\n**" .. info or info,
				["all category"] = cat,
				name = "From an avoided double redirect"
			}
		})
	end

	if not thisTarget then
		return formatError("This page is not a redirect.", demo)
	end
	if mw.title.equals(thisPage, thisTarget) then
		return formatOutput(true, "<span class=\"error\">This is a broken redirect (it redirects to itself).</span>")
	end
	if not thisTarget.exists then
		return formatOutput(true, "<span class=\"error\">This is a broken redirect (its target does not exist).</span>")
	end
	if not otherPage.exists then
		return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] does not exist.</span>")
	end
	if otherTarget and mw.title.equals(otherPage, otherTarget) then
		return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] is a broken redirect (it redirects to itself).</span>")
	end
	if otherTarget and not otherTarget.exists then
		return formatOutput(true, "<span class=\"error\">[[:" .. otherPage.fullText .. "]] is a broken redirect (it redirects to a page that does not exist).</span>")
	end
	if mw.title.equals(thisTarget, otherPage) then
		if not otherTarget then
			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.")
		end
		if mw.title.equals(otherTarget, thisPage) then
			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.</span>")
		end
		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.")
	end
	if not otherTarget then
		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.")
	end
	if thisDoubleTarget then
		if otherDoubleTarget then
			if mw.title.equals(thisDoubleTarget, otherDoubleTarget) then
				return formatOutput(true, "<span class=\"error\">Both this page and " .. noredir(otherPage) .. " are double redirects.</span> Please change the redirect target of both to "
					.. (thisDoubleTarget.isRedirect and "the correct article." or "[[" .. thisDoubleTarget.fullText .. "]] (or some other correct article)."))
			end
			return formatOutput(true, "<span class=\"error\">Both this page and " .. noredir(otherPage) .. " are double redirects.</span> Please fix them.")
		end
		return formatOutput(true, "<span class=\"error\">This is a double redirect.</span> Please fix it, possibly by changing it to [[:" .. otherTarget.fullText .. "]].")
	end
	if not mw.title.equals(thisTarget, otherTarget) then
		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.")
	end

	if thisTarget.fragment ~= otherTarget.fragment then
		-- Should this case report for update?
		return formatOutput(false, "Because [[Wikipedia:Double redirects|double redirects]] are disallowed,"
			.. " both pages currently point to [[" .. otherTarget.prefixedText .. "]] (but with different anchors).\n"
			.. "**If " .. noredir(otherPage) .. " is expanded into a separate article or it is retargeted, "
			.. " this redirect will be recategorized to be updated."
		)
	end

	return formatOutput(false, "Because [[Wikipedia:Double redirects|double redirects]] are disallowed,"
		.. " both pages currently point to [[" .. otherTarget.fullText .. "]].\n"
		.. "**If " .. noredir(otherPage) .. " is expanded into a separate article or it is retargeted, "
		.. " this redirect will be recategorized to be updated."
	)

end

return p