Module:R avoided double redirect

local p = {}

function p.main(frame)

local mRedirect = require("Module:Redirect") local function noredir(page) local link = mw.title.new(page):fullUrl("redirect=no") return "["..link.." "..page.."] " end local function process(page) return mw.title.new(page).prefixedText end local function exists(page) return mw.title.new(page).exists end

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

--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

--"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)

--Determine redirect targets (getTarget returns nil if page is not a redirect) local thisTarget = thisDemoTarget or mRedirect.getTarget(thisPage) local otherTarget = otherDemoTarget or mRedirect.getTarget(otherPage) --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

--Errors local err if not thisTarget and exists(thisPage) then --Check existence of thisPage to avoid errors in preview mode 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 double redirect." .." Please change the redirect target to "..otherTarget.."." elseif otherTarget and thisDoubleTarget then err = "This is a double redirect to "..thisDoubleTarget..", while " ..noredir(otherPage).." redirects to "..otherTarget.."." elseif thisDoubleTarget then err = "This is a double redirect to "..thisDoubleTarget.."." elseif otherTarget==thisPage then err = noredir(otherPage).." is a double redirect." .." Please change its target to "..thisTarget.."." elseif otherDoubleTarget then err = noredir(otherPage).." is a double redirect" .." to "..otherDoubleTarget.." via "..otherTarget.."." elseif otherTarget and thisTarget~=otherTarget then err = "This page and "..noredir(otherPage).." redirect to different articles." end

--Return either redirect template or error local from, info, cat if not err or noError then if otherTarget then from = "an alternative title for "..noredir(otherPage)..", another redirect to the same title" info = "Because double redirects are disallowed," .." both pages currently point to "..otherTarget..".\n" .."**If and when "..noredir(otherPage).." is expanded into a separate" .." article, or its target article is renamed to "..noredir(otherPage)..", this" .." redirect will be recategorized to be changed to target that article instead." cat = demo and "" or "Avoided double redirects" else from = "an alternative title for "..otherPage..", a former redirect to the same title" info = "\n**Since "..otherPage.." is now a separate article, please" .." update this redirect's target to "..otherPage.." and remove the" .." template." .."\n**If "..otherPage.." is the wrong target for this redirect," .." edit this page and correct the page name in the template." cat = demo and "" or "Avoided double redirects to be updated" end return frame:expandTemplate({title="Redirect template",                              args={from=from,info=info,["main category"]=cat,                                     name="From an avoided double redirect"}}) else return "Error in Module:R avoided double redirect: "..err.." " ..(demo and "" or "") end

end

return p