Module:Redirect/sandbox
![]() | This is the module sandbox page for Module:Redirect (diff). See also the companion subpage for test cases (run). |
![]() | This Lua module is used in system messages, and on approximately 4,740,000 pages, or roughly 8% of all pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
This module contains functions to find the target of a redirect page.
Main
The main function accepts the name of a single page. It determines if the page is a redirect; if so it looks up the page, extracts the target, and returns the target name as text. Its usage is {{#invoke:redirect|main|page-name}}
. If page-name
does not exist or is not a redirect then page-name
is returned. If page-name
is blank then blank is returned.
The function normally neither takes nor gives square brackets, so to show the result as a link use [[{{#invoke:redirect|main|page-name}}]]
.
If the parameter bracket is given a nonblank value, brackets will be passed on if present.
Examples
{{#invoke:redirect|main|WP:AFC}}
→ Wikipedia:Articles for creation{{#invoke:redirect|main|[[WP:AFD]]|bracket=yes}}
→ Wikipedia:Articles for deletion{{#invoke:redirect|main|Wikipedia:Articles for deletion}}
→ Wikipedia:Articles for deletion{{#invoke:redirect|main|Wikipedia:Articles for rumination}}
→ Wikipedia:Articles for rumination
Note: WP:AFC and WP:AFD are both redirects, but Wikipedia:Articles for deletion is not, and Wikipedia:Articles for rumination does not exist.
LuaMain
If you want to use the main function from another Lua module, you may want to use the luaMain function. This provides the same functionality as the main function, but doesn't require that a frame object be available.
To use this, first load the module.
local mRedirect = require('Module:Redirect')
Then use the function with the following syntax:
mRedirect.luaMain(rname, bracket)
rname is the name of the redirect page, and if bracket is anything but false or nil, the module will produce a link rather than just a page name.
IsRedirect
The isRedirect function is used from wiki pages to find out if a given page is a redirect or not. If the page is a redirect, the function returns "yes", and if not the output is blank. Its usage is {{#invoke:redirect|isRedirect|page-name}}
.
Examples
{{#invoke:redirect|isRedirect|WP:AFC}}
→ yes{{#invoke:redirect|isRedirect|Wikipedia:Articles for deletion}}
→{{#invoke:redirect|isRedirect|Wikipedia:Articles for rumination}}
→
LuaIsRedirect
The isRedirect can be accessed from other Lua modules in the same way as the main function:
local mRedirect = require('Module:Redirect')
mRedirect.luaIsRedirect(rname)
GetTarget
The getTarget function fetches the target page name of a redirect page, and is only usable from Lua. If the page specified is a redirect, then the target is returned; otherwise the function returns nil.
To use it, first load the module.
local mRedirect = require('Module:Redirect')
Then use the function with the following syntax:
mRedirect.getTarget(page)
page can be either the name of the redirect page as a string, or a mw.title object.
See also
- phab:T68974 - Provide a standard way to get the target of a redirect page
- mw:Extension:Scribunto/Lua reference manual#Title objects, the Scribunto interface for determining a page's redirect status
- Template:Get QID, which uses this module
- Template:Submit an edit request, which uses this module
-- Given a single page name determines what page it redirects to and returns the target page name, or the
-- passed page name when not a redirect. The passed page name can be given as plain text or as a page link.
-- Returns page name as plain text, or when the bracket parameter is given, as a page link. Returns an
-- error message when page does not exist or the redirect target cannot be determined for some reason.
-- Thus these are roughly the same:
-- [[{{#invoke:redirect|main|redirect-page-name}}]] and {{#invoke:redirect|main|redirect-page-name|bracket=yes}}
local p = {}
function warOnGsub(text, repl)
if repl then
text = mw.ustring.gsub(text, "%%", "%%%%")
else
text = mw.ustring.gsub(text, "([%?%^%$%(%)%%%.%[%]%*%+%-%]])", "%%%1")
end
return text
end
function getArgs(frame)
-- If called via #invoke, use the args passed into the invoking
-- template, or the args passed to #invoke if any exist. Otherwise
-- assume args are being passed directly in from the debug console
-- or from another Lua module.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs( frame.args ) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs( origArgs ) do
v = mw.text.trim( v )
if v ~= '' then
args[k] = v
end
end
return args
end
function getRedirect(rname)
-- Get the title object, passing the function through pcall
-- in case we are over the expensive function count limit.
-- returning nil indicates the redirect did not parse, but nonexistent or non-redirect pages are not nil.
local noError, rpage = pcall(mw.title.new, rname)
if not noError or noError and not rpage or not rpage.isRedirect then
-- mw.title.new failed, or the page is not a redirect, so use the passed page name.
return rname
end
local redirect = mw.ustring.match(rpage:getContent() or "", "^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]" )
if redirect then
-- Decode html entities and percent encodings.
redirect = mw.text.decode(redirect, true)
redirect = mw.uri.decode(redirect, 'WIKI')
end
return redirect
end
function p.main(frame)
local args = getArgs(frame)
local rname, bracket = args[1], args.bracket
bracket = bracket and "[[%s]]" or "%s"
if type(rname) ~= "string" or not mw.ustring.match(rname, "%S") then return end
rname = mw.ustring.match(rname, "%[%[(.+)%]%]") or rname
local redirect = getRedirect(rname)
if redirect then
return mw.ustring.format(bracket, redirect)
else
return mw.ustring.format('<span class="error">[[Module:redirect]] error: could not parse redirect - [[%s]]</span>', rname)
end
end
function getPage(sourceName)
if type(sourceName) ~= "string" or not mw.ustring.match(sourceName, "%S") then return end
-- the following delink doesn't make as much sense for this usage but is done for consistency.
sourceName = mw.ustring.match(sourceName, "%[%[(.+)%]%]") or sourceName
local noError, title = pcall(mw.title.new, sourceName)
if not noError then
text = nil -- should be anyway; nil means error
else
text = title:getContent()
end
return text
end
function nowikize(frame, text, nowiki, default)
if (default or nowiki) and (nowiki ~= 'no') then
text = frame:preprocess('<pre><nowiki>'..text..'</nowiki></pre>')
end
return text
end
function p.block(frame)
-- this feature takes an initial page as an argument. It obtains all the links from that page
-- starting from the first instance of some link named as start=, and replaces every link after
-- that which is a redirect with the non-redirected canonical name, up until pcall throws an error
-- due to expense. Finally, it returns the substituted page.
local args = getArgs(frame)
local sourceName, start, text, pipe, nowiki = args[1], args[2], args.text, args.pipe, args.nowiki
if type(sourceName) == "string" then sourceName = mw.text.trim(sourceName) or sourceName end
if type(start) == "string" then start = mw.text.trim(start) or start end
if type(pipe) == "string" then pipe = mw.text.trim(pipe) or pipe end
if type(text) ~= "string" then
text = getPage(sourceName)-- we're getting text = the contents of the page at args[1]
end
if not(text) then -- nothing to work from
return -- optional error here
end
local originalText = text -- I don't want to even think about conflicts...
local nextLink = mw.ustring.gmatch(originalText, "%[%[([^%]|]*)|?([^%]]-)%]%]")
local link = " " -- true
local display = "" -- false
while link do
if not(start) and link ~= " " then
newLink = getRedirect(link)
-- if the page does redirect, do it if "pipe" isn't set or a pipe is present or absent as specified
if newLink and (newLink ~= link) and ((not pipe) or (pipe == 'make') or (display and (pipe == 'yes')) or (not display and (pipe == 'no'))) then
if pipe == 'make' and (not display) then
-- if we're changing the link and there's no |something, create |the old name in the link.
newLink = newLink .. "|" .. link
end
link = warOnGsub(link, nil)
newLink = warOnGsub(newLink, true)
text = mw.ustring.gsub(text, link, newLink, 1) or text -- no sense looking past 1, we'll be back...
end
end
link, display = nextLink()
if display then
display = mw.ustring.match(display, "(%S+)") -- nil if no non-space
end
if link then link = mw.text.trim(link) or link end
if start and link then
if start == link then
start = nil
else
link = " " -- discard all links before the one to start with
end
end
end
return nowikize(frame, text, nowiki, true)
end
return p