Jump to content

Module:Wikipedia ads

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Snævar (talk | contribs) at 12:50, 19 April 2014 (use an lua table to lookup ads.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function p.main(frame)
	local pframe = frame:getParent()
	local config = frame.args
	local args = pframe.args
	
	local ads = {}
	local errors = {}
	adslist = require("Module:Wikipedia ads/list")

	if args.navbox ~= nil or "" then
		table.insert(ads, [[<table class="navbox collapsible collapsed" style="width: 100%; 
			background: #f7f8ff; text-align: left; border: solid 1px #AAAAAA; margin: 0px">]])
		table.insert(ads, [[<th style="background: transparent; font-size: 105%; text-align: center; 
			font-weight: bold">]])
		table.insert(ads, "[[Template:Wikipedia ads|Wikipedia ads]]: Overview</th><tr><td>")
		table.insert(ads, [[<table class="plainlinks sortable wikitable" style="width: 100%; 
			text-align: left; margin: 0; font-size: 90%"><tr>]])
		table.insert(ads, [[<th style="width: 3em">#</th><th class="unsortable">Image</th>
			<th>Page</th><th>Author</th></tr>]])
		if tonumber(adslist[1]) ~= nil and tonumber(adslist[2]) ~= nil then
			local i = 0
			while i < 400 == true and i < (adslist[1] - adslist[2]) == true do
				i = i + 1
				table.insert(ads, "<tr>")
				if tonumber(adslist[i+3*adslist[1]+2]) == nil then
					table.insert(errors, "Error: Argument " .. i+3*adslist[1]+2 .. " in [[Template:Wikipedia ads]] is not a number")
				else
					table.insert(ads, "<th>" .. (adslist[i+3*adslist[1]+2] or "") .. "</th>")
					table.insert(ads, "<td>[[" .. (adslist[adslist[i+3*adslist[1]+2]*3] or "") .. "|140px]]</td>")
					table.insert(ads, "<td>[[" .. (adslist[adslist[i+3*adslist[1]+2]*3+1] or "") .. "]]</td>")
					table.insert(ads, "<td>" .. (adslist[adslist[i+3*adslist[1]+2]*3+2] or "") .. "</td>")
				end
				table.insert(ads, "</tr>")
			end
		else
			table.insert(errors, "Error: Table value 1 and/or 2  in [[Module:Wikipedia ads/list]] are not numbers")
		end
		table.insert(ads, "</table></td></tr></table>")
	else
		
		table.insert(ads, '<table class="plainlinks qxz-ads" style="color:' .. (args.color or "#555555") .. 'border:none;')
		if args.background ~= nil or "" then
			table.insert(ads, "background:" .. args.background .. ";")
		end
		table.insert(ads, "float:" .. (args.float or "margin:0 auto") .. ";")
		table.insert(ads, [[line-height:1em; font-size:90%; display:block; overflow:auto; 
			max-width:477px"><tr><td colspan="2" style="border:none">]])
		local cal = math.random(adslist[1]-adslist[2])
		if adslist[1] and adslist[2] == nil then
			table.insert(error, "Error Argument 1 and 2 in [[Template:Wikipedia ads]] are not numbers")
		elseif tonumber(cal) == nil then
			table.insert(error, "Internal error: Could not calculate a random number" )
			if args.ad ~= nil then
				table.insert(ads, "[[" .. (adslist[config.ad * 3] or "") .. "|468px|link=")
				table.insert(ads, (adslist[config.ad *3+1] or "") .. "]]</td></tr>")
			end
		else
			table.insert(ads, "[[" .. (adslist[(config.ad or cal)*3] or "") .. "|468px|link=")
			table.insert(ads, (adslist[(config.ad or cal)*3+1] or "") .. "]]</td></tr>")
		end
		if args.nolinks ~= nil or "" then
			table.insert(ads, '<tr><td style="border:none">[[Template:Wikipedia ads|<font color="')
			table.insert(ads, (args.linkcolor or "#002bb8") .. '">Wikipedia Ads</font>]]</td>')
			table.insert(ads, '<td style="text-align: right; border:none"> [')
			if tonumber(cal) ~= nil and adslist[(config.ad or cal)*3] ~= nil then
				table.insert(ads, mw.uri.fullUrl(adslist[(config.ad or cal)*3]) )
			elseif config.ad ~= nil and adslist[config.ad *3] ~= nil then
				table.insert(ads, mw.uri.fullUrl(adslist[config.ad *3]) )
			end
			table.insert(ads, ' <font color="' .. (args.linkcolor or "#002bb8") .. '">file info</font>]')
			if config.showpurge ~= nil or "" then
				table.insert(ads, "- [" .. mw.uri.fullUrl(mw.title.getCurrentTitle().prefixedText , "action=purge"))
				table.insert(ads, ' <font color="' .. (args.linkcolor or "#002bb8") .. '">show another</font>]')
			end
			if cal ~= nil then
				table.insert(ads, " – #" .. (config.ad or cal) .. "</td></tr>")
			elseif config.ad ~= nil then
				table.insert(ads, " – #" .. config.ad .. "</td></tr>")
			end
		end
		table.insert(ads, "</table>")
	end
	return table.concat(errors) .. table.concat(ads)
end

return p