![]() | This Lua module is used on approximately 64,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module implements ((R avoided double redirect)). Please see the template page for documentation.
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 "<span class=\"plainlinks\">["..link.." "..page.."]</span>"
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 [[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
--Return either redirect template or error
local embed = args.embed
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 [[Wikipedia:Double redirects|double redirects]] are disallowed,"
.." both pages currently point to [["..otherTarget.."]].\n"
.."**In case "..noredir(otherPage).." is expanded into a separate"
.." article, this redirect should be changed to point to 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 this 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=(embed=="yes" and "Avoided double redirect" or nil))))
else
return "<span class=\"error\">Error in [[Module:R avoided double redirect]]: "..err.."</span>"
..(demo and "" or "[[Category:Avoided double redirects/error]]")
end
end
return p