Title: Module:Redirect - freem URL Source: https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect Markdown Content: _Documentation for this module may be created at [Module:Redirect/doc](https://freemwiki.com/index.php?title=Module:Redirect/doc&action=edit&redlink=1 "Module:Redirect/doc (page does not exist)")_ [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-1)-- This module provides functions for getting the target of a redirect page.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-2)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-3)local p = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-4)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-5)-- Gets a mw.title object, using pcall to avoid generating script errors if we[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-6)-- are over the expensive function count limit (among other possible causes).[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-7)local function getTitle(...)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-8)local success, titleObj = pcall(mw.title.new, ...)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-9)if success then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-10)return titleObj[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-11)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-12)return nil[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-13)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-14)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-15)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-16)-- Gets the name of a page that a redirect leads to, or nil if it isn't a[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-17)-- redirect.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-18)function p.getTargetFromText(text)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-19)local target = string.match([](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-20)text,[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-21)"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)%]%]"[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-22)) or string.match([](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-23)text,[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-24)"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]"[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-25))[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-26)return target and mw.uri.decode(target, 'PATH')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-27)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-28)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-29)-- Gets the target of a redirect. If the page specified is not a redirect,[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-30)-- returns nil.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-31)function p.getTarget(page, fulltext)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-32)-- Get the title object. Both page names and title objects are allowed[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-33)-- as input.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-34)local titleObj[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-35)if type(page) == 'string' or type(page) == 'number' then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-36)titleObj = getTitle(page)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-37)elseif type(page) == 'table' and type(page.getContent) == 'function' then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-38)titleObj = page[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-39)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-40)error(string.format([](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-41)"bad argument #1 to 'getTarget'"[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-42).. " (string, number, or title object expected, got %s)",[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-43)type(page)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-44)), 2)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-45)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-46)if not titleObj then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-47)return nil[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-48)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-49)local targetTitle = titleObj.redirectTarget[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-50)if targetTitle then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-51)if fulltext then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-52)return targetTitle.fullText[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-53)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-54)return targetTitle.prefixedText[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-55)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-56)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-57)return nil[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-58)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-59)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-60)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-61)--[[[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-62)-- Given a single page name determines what page it redirects to and returns the[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-63)-- target page name, or the passed page name when not a redirect. The passed[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-64)-- page name can be given as plain text or as a page link.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-65)--[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-66)-- Returns page name as plain text, or when the bracket parameter is given, as a[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-67)-- page link. Returns an error message when page does not exist or the redirect[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-68)-- target cannot be determined for some reason.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-69)--]][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-70)function p.luaMain(rname, bracket, fulltext)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-71)if type(rname) ~= "string" or not rname:find("%S") then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-72)return nil[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-73)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-74)bracket = bracket and "[[%s]]" or "%s"[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-75)rname = rname:match("%[%[(.+)%]%]") or rname[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-76)local target = p.getTarget(rname, fulltext)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-77)local ret = target or rname[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-78)ret = getTitle(ret)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-79)if ret then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-80)if fulltext then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-81)ret = ret.fullText[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-82)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-83)ret = ret.prefixedText[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-84)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-85)return bracket:format(ret)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-86)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-87)return nil[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-88)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-89)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-90)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-91)-- Provides access to the luaMain function from wikitext.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-92)function p.main(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-93)local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-94)return p.luaMain(args[1], args.bracket, args.fulltext) or ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-95)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-96)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-97)-- Returns true if the specified page is a redirect, and false otherwise.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-98)function p.luaIsRedirect(page)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-99)local titleObj = getTitle(page)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-100)if not titleObj then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-101)return false[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-102)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-103)if titleObj.isRedirect then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-104)return true[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-105)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-106)return false[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-107)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-108)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-109)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-110)-- Provides access to the luaIsRedirect function from wikitext, returning 'yes'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-111)-- if the specified page is a redirect, and the blank string otherwise.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-112)function p.isRedirect(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-113)local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-114)if p.luaIsRedirect(args[1]) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-115)return 'yes'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-116)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-117)return ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-118)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-119)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-120)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&oldid=579559&title=Module%3ARedirect#L-121)return p