Title: Module:Redirect hatnote - freem URL Source: https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote Markdown Content: _Documentation for this module may be created at [Module:Redirect hatnote/doc](https://freemwiki.com/index.php?title=Module:Redirect\_hatnote/doc&action=edit&redlink=1 "Module:Redirect hatnote/doc (page does not exist)")_ [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-1)--[[[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-2)-- This module produces a "redirect" hatnote. It looks like this:[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-3)-- '"X" redirects here. For other uses, see Y.'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-4)-- It implements the {{redirect}} template.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-5)--]][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-6)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-7)local mHatnote = require('Module:Hatnote')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-8)local mHatList = require('Module:Hatnote list')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-9)local mArguments --lazily initialize[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-10)local libraryUtil = require('libraryUtil')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-11)local checkType = libraryUtil.checkType[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-12)local checkTypeMulti = libraryUtil.checkTypeMulti[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-13)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-14)local p = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-15)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-16)--------------------------------------------------------------------------------[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-17)-- Helper functions[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-18)--------------------------------------------------------------------------------[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-19)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-20)local function getTitle(...)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-21)--Calls mw.title.new and returns either a title object, or nil on error[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-22)local success, titleObj = pcall(mw.title.new, ...)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-23)return success and titleObj or nil[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-24)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-25)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-26)--------------------------------------------------------------------------------[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-27)-- Main functions[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-28)--------------------------------------------------------------------------------[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-29)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-30)function p.redirect(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-31)mArguments = require('Module:Arguments')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-32)local args = mArguments.getArgs(frame, {parentOnly=true})[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-33)--Get number of redirects[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-34)local numRedirects = tonumber(frame.args[1]) or 1[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-35)-- Create the options table.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-36)local options = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-37)options.selfref = args.selfref[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-38)return p._redirect(args, numRedirects, options)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-39)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-40)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-41)function p._redirect(args, numRedirects, options, currentTitle, redirectTitle, targetTitle)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-42)-- Validate the input. Don't bother checking currentTitle, redirectTitle or[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-43)-- targetTitle, as they are only used in testing.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-44)checkType('_redirect', 1, args, 'table')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-45)checkType('_redirect', 2, numRedirects, 'number', true)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-46)numRedirects = numRedirects or 1[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-47)checkType('_redirect', 3, options, 'table', true)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-48)options = options or {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-49)currentTitle = currentTitle or mw.title.getCurrentTitle()[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-50)-- Get the table of redirects[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-51)local redirect = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-52)for i = 1, numRedirects do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-53)-- Return an error if a redirect parameter is missing.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-54)if not args[i] then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-55)return mHatnote.makeWikitextError([](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-56)'missing redirect parameter',[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-57)'Template:Redirect#Errors',[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-58)args.category[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-59))[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-60)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-61)redirect[i] = args[i][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-62)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-63)-- Generate the text.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-64)local formattedRedirect = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-65)for k,v in pairs(redirect) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-66)formattedRedirect[k] = mHatnote.quote(v)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-67)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-68)local text = {[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-69)mHatList.andList(formattedRedirect) .. ' ' .. (#redirect == 1 and 'redirects' or 'redirect') .. ' here.',[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-70)mHatList._forSee(args, #redirect + 1, {title = redirect[1], extratext = args.text})[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-71)}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-72)text = table.concat(text, ' ')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-73)-- Functionality for adding categories [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-74)local categoryTable = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-75)local function addCategory(cat)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-76)if cat and cat ~= '' then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-77)-- Add by index to avoid duplicates[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-78)categoryTable[string.format('[[Category:%s]]', cat)] = true[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-79)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-80)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-81)--Generate tracking categories[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-82)local mhOptions = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-83)local redirTitle[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-84)for k,v in pairs(redirect) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-85)-- We don't need a tracking category if the template invocation has been[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-86)-- copied directly from the docs, or if we aren't in main- or category-space.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-87)if not v:find('^REDIRECT%d*$') and v ~= 'TERM' -- [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-88)and currentTitle.namespace == 0 or currentTitle.namespace == 14[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-89)then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-90)redirTitle = redirectTitle or getTitle(v)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-91)if not redirTitle or not redirTitle.exists then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-92)addCategory('Missing redirects')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-93)elseif not redirTitle.isRedirect then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-94)if string.find(redirTitle:getContent(), '#invoke:RfD') then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-95)addCategory('Articles with redirect hatnotes impacted by RfD')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-96)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-97)addCategory('Articles with redirect hatnotes needing review')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-98)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-99)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-100)local target = targetTitle or redirTitle.redirectTarget[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-101)if target and target ~= currentTitle then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-102)addCategory('Articles with redirect hatnotes needing review')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-103)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-104)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-105)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-106)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-107)-- Generate the options to pass to [[Module:Hatnote]].[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-108)if currentTitle.namespace == 0 and not mhOptions.selfref[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-109)and redirTitle and redirTitle.namespace ~= 0[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-110)then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-111)-- We are on a mainspace page, and the hatnote starts with something[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-112)-- like "Wikipedia:Foo redirects here", so automatically label it as[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-113)-- a self-reference.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-114)mhOptions.selfref = true[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-115)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-116)mhOptions.selfref = options.selfref[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-117)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-118)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-119)--concatenate all the categories[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-120)local category = ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-121)for k,v in pairs(categoryTable) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-122)category = category .. k[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-123)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-124)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-125)return mHatnote._hatnote(text, mhOptions) .. category[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-126)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-127) [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3ARedirect_hatnote#L-128)return p