![]() |
VOOZH | about |
This module makes {{page-multi}} work, and is modeled on wikipedia:Template:User-multi. See that template's documentation for more information.
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 localfunctionmakeWhatLinksHereLink() return'[[Special:WhatLinksHere/'..p.fullText..'|links]]' end localfunctionmakeRelatedChangesLink() return'[[Special:RelatedChanges/'..p.fullText..'|related]]' end localfunctionmakeEditLink() localurl=p:fullUrl('action=edit'); return'['..url..' edit]' end localfunctionmakeHistoryLink() localurl=p:fullUrl('action=history'); return'['..url..' history]' end localfunctionmakeWatchLink() localurl=p:fullUrl('action=watch'); return'['..url..' 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 localfunctionStatsGrokSeURL(lang) localurl='http://stats.grok.se/'..lang..'/latest60/'..mw.uri.encode(p.fullText,"PATH") returnurl end localfunctionmakeViewsLastMonthStatsGrokSeLink() return'['..StatsGrokSeURL('en')..' stats]' end localfunctionpageViewsURL(project,startDate,endDate) localurl=tostring(mw.uri.fullUrl('Toollabs:pageviews'))..'?start='..startDate..'&end='..endDate..'&project='..project..'&pages='..mw.uri.encode((string.gsub(p.fullText," ","_"))) returnurl end localfunctionmakePageViews(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 return'['..pageViewsURL('en.wikipedia.org',startDate,endDate)..' 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}, {'wlh',makeWhatLinksHereLink}, {'rc',makeRelatedChangesLink}, {'edit',makeEditLink}, {'h',makeHistoryLink}, {'w',makeWatchLink}, {'tl',makeTargetLogsLink}, {'efl',makeEditFilterLogLink}, {'vlm-sgs',makePageViews}, {'pv',makePageViews}, {'h500',makeHistory500Link}, } localfunctiongetLink(linktype,args) locallinkNumber fori,valueinipairs(linktypes)do ifvalue[1]==linktypethen linkNumber=i break end end ifnotlinkNumberthen returnerr('"'..linktype..'" is not a valid link code','Not a valid link code') end localresult=linktypes[linkNumber][2](args) iftype(result)~='string'then returnerr( 'the function for code "'..linktype..'" did not return a string value', 'Function did not return a string value' ) end returnresult 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) }