localToolbarBuilder=require('Module:Toolbar')localp={}-- Page objectlocaltrackingCategories={}-- Table for storing the tracking categories.localdemo-- Define a custom error message for this module.localfunctionerr(msg,section)localhelpifsectionthenhelp=' ([[Template:Page-multi#'..section..'|help]])'elsehelp=''endlocalcatifdemo=='yes'thencat=''elsecat='[[Category:PageLinks transclusions with errors]]'endreturn'<span class="error">[[Template:Page-multi|Page-multi]] error: '..msg..help..'.</span>'..catend------------------------------------------------------------------------------------------------ To add more link types, write a function that produces an individual link, and put ---- it at the bottom of the list below. Then, add a link code for your function to the ---- "linktypes" table. Try and make the code three letters or less. ---- If you want more helper strings, you can define them in the generatePageDataStrings ---- function below. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ LINK FUNCTIONS START ------------------------------------------------------------------------------------------------localfunctionmakePageLink()returnp.fullTextand'[[:'..p.fullText..'|'..p.fullText..']]'or''endlocalfunctionmakeTalkLink()return'[['..tostring(p.talkPageTitle)..'|talk]]'endlocalfunctionmakeTalkOrSubjectLink()ifp.isTalkPagethenreturn'[[:'..tostring(p.subjectPageTitle)..'|subject]]'elsereturn'[['..tostring(p.talkPageTitle)..'|talk]]'endendlocalfunctionmakeWhatLinksHereLink()return'[[Special:WhatLinksHere/'..p.fullText..'|links]]'endlocalfunctionmakeRelatedChangesLink()return'[[Special:RelatedChanges/'..p.fullText..'|related]]'endlocalfunctionmakeEditLink()return'[[Special:EditPage/'..p.fullText..'|edit]]'endlocalfunctionmakeHistoryLink()return'[[Special:PageHistory/'..p.fullText..'|history]]'endlocalfunctionmakeWatchLink()return'['..p:fullUrl('action=watch')..' watch]'endlocalfunctionmakeTargetLogsLink()localurl=mw.uri.fullUrl('Special:Log','page='..mw.uri.encode(p.fullText))return'['..tostring(url)..' logs]'endlocalfunctionmakeEditFilterLogLink()localurl=mw.uri.fullUrl('Special:AbuseLog','wpSearchTitle='..mw.uri.encode(p.fullText))return'['..tostring(url)..' abuse filter log]'endlocalfunctionmakePageViewsLink(args)localendDate,startDate="",""ifargs.datethenlocaldate=string.gsub(args.date,"-","")date=os.time{year=string.sub(date,1,4),month=string.sub(date,5,6),day=string.sub(date,7,8)}endDate=os.date("%Y-%m-%d",date-(1*86400))startDate=os.date("%Y-%m-%d",date-(30*86400))elseendDate=os.date("%Y-%m-%d")startDate=os.date("%Y-%m-%d",os.time()-(60*86400))endlocalproject=string.sub(mw.site.server,3)localurl=mw.uri.encode(p.fullText,"WIKI")localurlargs='?start='..startDate..'&end='..endDate..'&project='..project..'&pages='..tostring(url)if#urlargs>255then-- Extra chars can tip it over the max length for an IW link.return'[https://pageviews.toolforge.org/?'..urlargs..' stats]'endreturn'[[:toolforge:pageviews/'..urlargs..'|stats]]'endlocalfunctionmakeHistory500Link()localurl=p:fullUrl('action=history&limit=500');return'['..url..' long history]'end------------------------------------------------------------------------------------------------ LINK FUNCTIONS END ---- To enable new link functions, add the code to the "linktypes" table directly below. ------------------------------------------------------------------------------------------------locallinktypes={['t']=makeTalkLink,['ts']=makeTalkOrSubjectLink,['wlh']=makeWhatLinksHereLink,['rc']=makeRelatedChangesLink,['edit']=makeEditLink,['h']=makeHistoryLink,['w']=makeWatchLink,['tl']=makeTargetLogsLink,['efl']=makeEditFilterLogLink,['vlm-sgs']=makePageViewsLink,['pv']=makePageViewsLink,['h500']=makeHistory500Link,}localfunctiongetLink(linktype,args)localfunc=linktypes[linktype]iffuncthenreturnfunc(args)elsereturnerr('"'..linktype..'" is not a valid link code','Not a valid link code')endendlocalfunctionmakeToolbar(args)localtargs={}localnumArgsExist=falsefork,vinpairs(args)doiftype(k)=='number'andpthennumArgsExist=truetargs[k]=getLink(v,args)endendtargs.style=args.smalland'font-size: 90%;'targs.separator=args.separatoror'dot'targs.class='lx'ifnumArgsExist==falsethenreturnnil-- Don't return a toolbar if no numeric arguments exist. -- this bit looks oddelsereturnToolbarBuilder.main(targs)endendlocalfunctiongeneratePageDataStrings(args)-- If the page name is absent or blank, return an error and a tracking category.ifargs.page==''ornotargs.pagethenreturnerr('no page detected')endlocalnoErrornoError,p=pcall(mw.title.new,args.page)ifnotnoErrorthenreturnerr('pcall mw.title failed')endifargs.existsand(notporp['id']==0)thenreturnerr('page not found')endendlocalfunctiongenerateTrackingCategories()ifdemo=='yes'thenreturn''elsereturntable.concat(trackingCategories)endend-- This function generates a table of all available link types, with their previews.-- It is used in the module documentation.localfunctiongetLinkTable(args)demo=args.demo-- Set the demo variable.-- Generate the page data strings and return any errors.localdataStringError=generatePageDataStrings(args)ifdataStringErrorthenreturndataStringErrorend-- Build a table of all of the links.localresult='<table class="wikitable plainlinks sortable">'..'\n<tr><th>Code</th><th>Preview</th></tr>'fori,valueinipairs(linktypes)dolocalcode=value[1]result=result.."\n<tr><td>'''"..code.."'''</td><td>"..getLink(code,args)..'</td></tr>'endresult=result..'\n</table>'returnresultendlocalfunctiongetSingleLink(args)demo=args.demo-- Set the demo variable.-- Generate the page data strings and return any errors.localdataStringError=generatePageDataStrings(args)ifdataStringErrorthenreturndataStringErrorendlocallinktype=args[1]ifnotlinktypethenreturnerr('no link type specified')endlocalresult=getLink(linktype,args)result=result..generateTrackingCategories()returnresultendlocalfunctiongetLinksToolbar(args)demo=args.demo-- Set the demo variable.-- Generate the page data strings and return any errors.localdataStringError=generatePageDataStrings(args)ifdataStringErrorthenreturndataStringErrorend-- Build the template output.localresult=makeToolbar(args)-- Get the toolbar contents.result=(resultor'')..generateTrackingCategories()returnresultendlocalfunctiongetLinks(args)localresult=getLinksToolbar(args)ifresultthenifargs.supthenresult='<sup>'..result..'</sup>'endresult=' '..resultelseresult=''-- If there are no links specified, don't return the toolbar at all.endifargs.nopagethenresult='<span>'..result..'</span>'elseifpthenresult='<span>'..makePageLink()..result..'</span>'elseresult='<span>[['..args.page..']]'..result..'</span>'endendreturnresultendlocalfunctiongetExampleLinks(args)-- This function enables example output without having to specify any-- parameters to #invoke.args.demo='yes'args.page='Example'returngetLinks(args)endlocalfunctionmakeWrapper(func)returnfunction(frame)-- If called via #invoke, use the args passed into the invoking template.-- Otherwise, for testing purposes, assume args are being passed directly in.localorigArgsifframe==mw.getCurrentFrame()thenorigArgs=frame:getParent().argsfork,vinpairs(frame.args)doorigArgs=frame.argsbreakendelseorigArgs=frameend-- Strip whitespace, and treat blank arguments as nil.-- 'page', and 'separator' have different behaviour depending on-- whether they are blank or nil, so keep them as they are.localargs={}fork,vinpairs(origArgs)dov=mw.text.trim(v)ifv~=''ork=='page'ork=='separator'thenargs[k]=vendendreturnfunc(args)endendreturn{main=makeWrapper(getLinks),single=makeWrapper(getSingleLink),toolbar=makeWrapper(getLinksToolbar),linktable=makeWrapper(getLinkTable),example=makeWrapper(getExampleLinks)}