Jump to content

Module:Redirect hatnote

Permanently protected module
From Wikipedia, the free encyclopedia

--[[-- 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
close