Module:Wikipedia ads
This module displays banner-style adverts for various Wikipedia projects, processes and pages. It implements Template:Wikipedia ads.
Example
Usage
From wikitext
From wikitext, usually this module should be used via Template:Wikipedia ads. However, it is also possible to use the syntax {{#invoke:wikipedia ads|main|...}}, where "..." are the template arguments. See the template page for documentation and for a list of available parameters.
From Lua
First, load the module:
localmWikipediaAds=require('Module:Wikipedia ads')
Then you can generate Wikipedia ads using the "_main" function:
mWikipediaAds._main(args)
args is a table of arguments that can be used by the module. See Template:Wikipedia ads for a complete list of arguments and for other documentation.
Adding and removing ads
For instructions on creating the animated GIF files used in the ads, see Template:Wikipedia ads#Creating ads. For instructions on adding and removing images from the module, see Module:Wikipedia ads/list.
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.
------------------------------------------------------------------------------- -- Module:Wikipedia ads -- -- This module displays a random banner-style advert for a Wikipedia project, -- page or process. It implements [[Template:Wikipedia ads]]. ------------------------------------------------------------------------------- -- Set constants localLIST_MODULE='Module:Wikipedia ads/list' localDATA_MODULE='Module:Wikipedia ads/data' localp={} localwarnings={} localfunctionaddWarning(msg) table.insert(warnings,msg) end localfunctionmakeWikilink(page,display) returnstring.format('[[%s|%s]]',page,display) end localfunctionmakeUrlLink(url,display) url=tostring(url) returnstring.format('[%s %s]',url,display) end localfunctioncolorText(s,color) returnstring.format('<span style="color:%s">%s</span>',color,s) end localfunctiongetImageData(args) -- This function gets an image data from the data module. It also tracks -- whether the image data choice was random. localdata=mw.loadData(DATA_MODULE) localfunctiongetSomeImageData(id,param) id=tonumber(id)orid localsomeImageData=data.ids[id] ifsomeImageDatathen returnsomeImageData else addWarning(string.format("ID '%s' does not exist",tostring(id))) returnnil end end -- Get the image data of the ad to display. localimageData,isRandom ifargs.adthen imageData=getSomeImageData(args.ad,'ad') ifnotimageDatathen returnnil end isRandom=false else localimageDataArray,length ifargs[1]then imageDataArray={} fori,idinipairs(args)do imageDataArray[#imageDataArray+1]=getSomeImageData(id,i) end length=#imageDataArray iflength<1then returnnil end else imageDataArray=data.list length=data.noAds end assert(length>=1,string.format( 'no ads were found in [[%s]]', DATA_MODULE )) isRandom=length>1 ifisRandomthen math.randomseed(os.clock()*1000000000) imageData=imageDataArray[math.random(length)] else imageData=imageDataArray[1] end end -- Check that the image data has the required fields. We have already -- checked the ID in the data module. fori,fieldinipairs{'image','link'}do assert(imageData[field],string.format( "Invalid image data in [[%s]]; table with ID '%s' has no '%s' field", LIST_MODULE,tostring(imageData.id),field )) end returnimageData,isRandom end localfunctionrenderAd(imageData,args,title,isRandom) localwidth=tonumber(args.width)or468 localmaxWidth=width+9 locallinkColor=args.linkcoloror'#002bb8' -- Table root localroot=mw.html.create('table') root :addClass('plainlinks qxz-ads') :css('color',args.coloror'#555555') :css('border','none') :css('background',args.background) :css('line-height','1em') :css('font-size','90%') :css('display','block') :css('overflow','auto') :css('max-width',maxWidth..'px') ifargs.floatthen root:css('float',args.float) root:css('margin',args.margin) else root:css('margin',args.marginor'0 auto') end -- Image row root :tag('tr') :tag('td') :attr('colspan',2) :css('border','none') :wikitext(string.format( '[[File:%s|%dpx|alt=Wikipedia ad for %s|link=%s]]', imageData.image, width, imageData.link, imageData.link )) -- Links row ifnotargs.nolinksthen locallinksRow=root:tag('tr') -- Wikipedia ads link linksRow :tag('td') :css('border','none') :wikitext(makeWikilink( 'Template:Wikipedia ads', colorText('Wikipedia ads',linkColor) )) -- File info, purge and ID locallinks={} links[#links+1]=makeWikilink( ':File:'..imageData.image, colorText('file info',linkColor) ) ifargs.showpurgeorisRandomthen links[#links+1]=makeUrlLink( title:fullUrl{action='purge'}, colorText('show another',linkColor) ) end links[#links+1]='#'..tostring(imageData.id) linksRow :tag('td') :css('text-align','right') :css('border','none') :wikitext(table.concat(links,' – ')) end returntostring(root) end localfunctionrenderWarnings(args,title) if#warnings<1then returnnil end -- Error list localroot=mw.html.create('div') :css('width','468px') ifargs.floatthen root :css('float',args.float) :css('clear','both') else root:css('margin','0 auto') end locallist=root:tag('ul') :addClass('error') :css('font-size','90%') for_,msginipairs(warnings)do list :tag('li') :wikitext(string.format( 'Wikipedia ads error: %s ([[Template:Wikipedia ads#Errors|help]]).', msg )) end -- Category. We use [[Module:Category handler]] for its blacklist. localmCatHandler=require('Module:Category handler') localcategory=mCatHandler._main{ all='[[Category:Wikipedia ads templates with errors]]', nocat=args.nocat, page=titleandtitle.prefixedText } localret=tostring(root) ifcategorythen ret=ret..category end returnret end functionp._main(args,title) title=titleormw.title.getCurrentTitle() localret={} localimageData,isRandom=getImageData(args) ifimageDatathen ret[#ret+1]=renderAd(imageData,args,title,isRandom) end ret[#ret+1]=renderWarnings(args,title) if#ret>0then returntable.concat(ret) else returnnil end end functionp.main(frame) localargs=require('Module:Arguments').getArgs(frame,{ wrappers='Template:Wikipedia ads' }) returnp._main(args) end returnp