Module:Redirect hatnote
Appearance
![]() | This Lua module is used on approximately 59,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 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 produces a hatnote for disambiguating a page that is linked to by a given redirect. It implements the {{redirect}} hatnote template.
Usage from wikitext
This module cannot be used directly from wikitext. Please use the {{redirect}} or {{redirect2}} templates instead.
Usage from Lua
To use this module from Lua, first load the module.
localmRedirectHatnote=require('Module:Redirect hatnote')
The module can then be used with the following syntax:
mRedirectHatnote._redirect(redirect,data,options,titleObj)
See also
--[[-- This module produces a "redirect" hatnote. It looks like this:-- '"X" redirects here. For other uses, see Y.'-- It implements the {{redirect}} template.--]]localmHatnote=require('Module:Hatnote')localmHatList=require('Module:Hatnote list')localmArguments--lazily initializelocallibraryUtil=require('libraryUtil')localcheckType=libraryUtil.checkTypelocalcheckTypeMulti=libraryUtil.checkTypeMultilocalp={}---------------------------------------------------------------------------------- Helper functions--------------------------------------------------------------------------------localfunctiongetTitle(...)--Calls mw.title.new and returns either a title object, or nil on errorlocalsuccess,titleObj=pcall(mw.title.new,...)returnsuccessandtitleObjornilend---------------------------------------------------------------------------------- Main functions--------------------------------------------------------------------------------functionp.redirect(frame)mArguments=require('Module:Arguments')localargs=mArguments.getArgs(frame,{parentOnly=true})--Get number of redirectslocalnumRedirects=tonumber(frame.args[1])or1-- Create the options table.localoptions={}options.selfref=args.selfrefreturnp._redirect(args,numRedirects,options)endfunctionp._redirect(args,numRedirects,options,currentTitle,redirectTitle,targetTitle)-- Validate the input. Don't bother checking currentTitle, redirectTitle or-- targetTitle, as they are only used in testing.checkType('_redirect',1,args,'table')checkType('_redirect',2,numRedirects,'number',true)numRedirects=numRedirectsor1checkType('_redirect',3,options,'table',true)options=optionsor{}currentTitle=currentTitleormw.title.getCurrentTitle()-- Get the table of redirectslocalredirect={}fori=1,numRedirectsdo-- Return an error if a redirect parameter is missing.ifnotargs[i]thenreturnmHatnote.makeWikitextError('missing redirect parameter','Template:Redirect#Errors',args.category)endredirect[i]=args[i]end-- Generate the text.localformattedRedirect={}fork,vinpairs(redirect)doformattedRedirect[k]=mHatnote.quote(v)endlocaltext={mHatList.andList(formattedRedirect)..' '..(#redirect==1and'redirects'or'redirect')..' here.',mHatList._forSee(args,#redirect+1,{title=redirect[1],extratext=args.text})}text=table.concat(text,' ')-- Functionality for adding categories localcategoryTable={}localfunctionaddCategory(cat)ifcatandcat~=''then-- Add by index to avoid duplicatescategoryTable[string.format('[[Category:%s]]',cat)]=trueendend--Generate tracking categorieslocalmhOptions={}localredirTitlefork,vinpairs(redirect)do-- We don't need a tracking category if the template invocation has been-- copied directly from the docs, or if we aren't in main- or category-space.ifnotv:find('^REDIRECT%d*$')andv~='TERM'-- andcurrentTitle.namespace==0orcurrentTitle.namespace==14thenredirTitle=redirectTitleorgetTitle(v)ifnotredirTitleornotredirTitle.existsthenaddCategory('Missing redirects')elseifnotredirTitle.isRedirectthenifstring.find(redirTitle:getContent(),'#invoke:RfD')thenaddCategory('Articles with redirect hatnotes impacted by RfD')elseaddCategory('Articles with redirect hatnotes needing review')endelselocaltarget=targetTitleorredirTitle.redirectTargetiftargetandtarget~=currentTitlethenaddCategory('Articles with redirect hatnotes needing review')endendend-- Generate the options to pass to [[Module:Hatnote]].ifcurrentTitle.namespace==0andnotmhOptions.selfrefandredirTitleandredirTitle.namespace~=0then-- We are on a mainspace page, and the hatnote starts with something-- like "Wikipedia:Foo redirects here", so automatically label it as-- a self-reference.mhOptions.selfref=trueelsemhOptions.selfref=options.selfrefendend--concatenate all the categorieslocalcategory=''fork,vinpairs(categoryTable)docategory=category..kendreturnmHatnote._hatnote(text,mhOptions)..categoryendreturnp