Module:PageLinks/sandbox
Appearance
From Wikipedia, the free encyclopedia
This is the module sandbox page for Module:PageLinks (diff).
See also the companion subpage for test cases (run).
See also the companion subpage for test cases (run).
Warning This Lua module is used on approximately 71,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 depends on the following other modules:
This module makes {{page-multi }} work, and is modeled on {{user-multi }}. See that template's documentation for more information.
Tracking/maintenance category
[edit ]The above documentation is transcluded from Module:PageLinks/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Add categories to the /doc subpage. Subpages of this module.
localToolbarBuilder=require('Module:Toolbar') localp={}-- Page object localtrackingCategories={}-- Table for storing the tracking categories. localdemo -- Define a custom error message for this module. localfunctionerr(msg,section) localhelp ifsectionthen help=' ([[Template:Page-multi#'..section..'|help]])' else help='' end localcat ifdemo=='yes'then cat='' else cat='[[Category:PageLinks transclusions with errors]]' end return'<span class="error">[[Template:Page-multi|Page-multi]] error: '..msg ..help..'.</span>'..cat end ---------------------------------------------------------------------------------------------- -- 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'' end localfunctionmakeTalkLink() return'[['..tostring(p.talkPageTitle)..'|talk]]' end localfunctionmakeTalkOrSubjectLink() ifp.isTalkPagethen return'[[:'..tostring(p.subjectPageTitle)..'|subject]]' else return'[['..tostring(p.talkPageTitle)..'|talk]]' end end localfunctionmakeWhatLinksHereLink() return'[[Special:WhatLinksHere/'..p.fullText..'|links]]' end localfunctionmakeRelatedChangesLink() return'[[Special:RelatedChanges/'..p.fullText..'|related]]' end localfunctionmakeEditLink() return'[[Special:EditPage/'..p.fullText..'|edit]]' end localfunctionmakeHistoryLink() return'[[Special:PageHistory/'..p.fullText..'|history]]' end localfunctionmakeWatchLink() return'['..p:fullUrl('action=watch')..' watch]' end localfunctionmakeTargetLogsLink() localurl=mw.uri.fullUrl('Special:Log','page='..mw.uri.encode(p.fullText)) return'['..tostring(url)..' logs]' end localfunctionmakeEditFilterLogLink() localurl=mw.uri.fullUrl('Special:AbuseLog','wpSearchTitle='..mw.uri.encode(p.fullText)) return'['..tostring(url)..' abuse filter log]' end localfunctionmakePageViewsLink(args) localendDate,startDate="","" ifargs.datethen localdate=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)) else endDate=os.date("%Y-%m-%d") startDate=os.date("%Y-%m-%d",os.time()-(60*86400)) end localproject=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]' end return'[[:toolforge:pageviews/'..urlargs..'|stats]]' end localfunctionmakeHistory500Link() 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] iffuncthen returnfunc(args) else returnerr('"'..linktype..'" is not a valid link code','Not a valid link code') end end localfunctionmakeToolbar(args) localtargs={} localnumArgsExist=false fork,vinpairs(args)do iftype(k)=='number'andpthen numArgsExist=true targs[k]=getLink(v,args) end end targs.style=args.smalland'font-size: 90%;' targs.separator=args.separatoror'dot' targs.class='lx' ifnumArgsExist==falsethen returnnil-- Don't return a toolbar if no numeric arguments exist. -- this bit looks odd else returnToolbarBuilder.main(targs) end end localfunctiongeneratePageDataStrings(args) -- If the page name is absent or blank, return an error and a tracking category. ifargs.page==''ornotargs.pagethen returnerr('no page detected') end localnoError noError,p=pcall(mw.title.new,args.page) ifnotnoErrorthen returnerr('pcall mw.title failed') end ifargs.existsand(notporp['id']==0)then returnerr('page not found') end end localfunctiongenerateTrackingCategories() ifdemo=='yes'then return'' else returntable.concat(trackingCategories) end end -- 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) ifdataStringErrorthen returndataStringError end -- 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)do localcode=value[1] result=result.."\n<tr><td>'''"..code.."'''</td><td>"..getLink(code,args)..'</td></tr>' end result=result..'\n</table>' returnresult end localfunctiongetSingleLink(args) demo=args.demo-- Set the demo variable. -- Generate the page data strings and return any errors. localdataStringError=generatePageDataStrings(args) ifdataStringErrorthen returndataStringError end locallinktype=args[1] ifnotlinktypethen returnerr('no link type specified') end localresult=getLink(linktype,args) result=result..generateTrackingCategories() returnresult end localfunctiongetLinksToolbar(args) demo=args.demo-- Set the demo variable. -- Generate the page data strings and return any errors. localdataStringError=generatePageDataStrings(args) ifdataStringErrorthen returndataStringError end -- Build the template output. localresult=makeToolbar(args)-- Get the toolbar contents. result=(resultor'')..generateTrackingCategories() returnresult end localfunctiongetLinks(args) localresult=getLinksToolbar(args) ifresultthen ifargs.supthen result='<sup>'..result..'</sup>' end result=' '..result else result=''-- If there are no links specified, don't return the toolbar at all. end ifargs.nopagethen result='<span>'..result..'</span>' else ifpthen result='<span>'..makePageLink()..result..'</span>' else result='<span>[['..args.page..']]'..result..'</span>' end end returnresult end localfunctiongetExampleLinks(args) -- This function enables example output without having to specify any -- parameters to #invoke. args.demo='yes' args.page='Example' returngetLinks(args) end localfunctionmakeWrapper(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. localorigArgs ifframe==mw.getCurrentFrame()then origArgs=frame:getParent().args fork,vinpairs(frame.args)do origArgs=frame.args break end else origArgs=frame end -- 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)do v=mw.text.trim(v) ifv~=''ork=='page'ork=='separator'then args[k]=v end end returnfunc(args) end end return{ main=makeWrapper(getLinks), single=makeWrapper(getSingleLink), toolbar=makeWrapper(getLinksToolbar), linktable=makeWrapper(getLinkTable), example=makeWrapper(getExampleLinks) }