Jump to content

Module:Wikipedia ads/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by RexxS (talk | contribs) at 17:26, 9 September 2014 (set alt text to "Wikipedia ad for " + page name). 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 = mw.loadData("Module:Wikipedia ads/list")

	if args.navbox ~= nil and args.navbox ~= "" 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 = 1
			while i < 400 == true and i <= (adslist[1] - adslist[2]) == true do
				table.insert(ads, "<tr>")
				if tonumber(adslist[i+899]) == nil then
					table.insert(errors, "Error: Argument " .. i+899+2 .. " in [[Module:Wikipedia ads/list]] is not a number")
				else
					table.insert(ads, "<th>" .. (adslist[i+899] or "") .. "</th>")
					table.insert(ads, "<td>[[" .. (adslist[adslist[i+899]*3] or "") .. "|140px]]</td>")
					table.insert(ads, "<td>[[" .. (adslist[adslist[i+899]*3+1] or "") .. "]]</td>")
					table.insert(ads, "<td>" .. (adslist[adslist[i+899]*3+2] or "") .. "</td>")
				end
				table.insert(ads, "</tr>")
				i = i + 1
			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 and args.background ~= "" 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">]])
		math.randomseed(os.time())
		cal = adslist[(math.random(adslist[1]-adslist[2])+899)]
		if tonumber(adslist[1]) and tonumber(adslist[2]) == nil then
			table.insert(errors, "Error: Argument 1 and 2 in [[Module:Wikipedia ads/list]] are not numbers")
		end
		if tonumber(args.ad) ~= nil then
			argad = args.ad
		elseif args[2] then
			local numberedargs = {}
			local index = 1
			while args[index] ~= nil do
				table.insert(numberedargs, index)
				index = index + 1
			end
			local argnum = math.random(table.maxn(numberedargs) )
			argad = args[argnum]
		else
			argad = nil
		end
		if argad and not tonumber(argad) then
			error('Invalid ad number "' .. tostring(argad) .. '"')
		end
		table.insert(ads, "[[" .. (adslist[(argad or cal)*3] or "") .. "|468px|alt=Wikipedia Ad for" .. (adslist[(argad or cal)*3+1] or "") .. "|link=" )
		table.insert(ads, (adslist[(argad or cal)*3+1] or "") .. "]]</td></tr>")
		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 adslist[(argad or cal)*3] then
				table.insert(ads, tostring(mw.uri.fullUrl(adslist[(argad or cal)*3])) )
			end
			table.insert(ads, ' <font color="' .. (args.linkcolor or "#002bb8") .. '">file info</font>]')
			if args.showpurge ~= nil and args.showpurge ~= "" then
				table.insert(ads, " – [" .. tostring(mw.uri.fullUrl(mw.title.getCurrentTitle().prefixedText, "action=purge")))
				table.insert(ads, ' <font color="' .. (args.linkcolor or "#002bb8") .. '">show another</font>]')
			elseif args.ad == nil or "" then
				table.insert(ads, " – [" .. tostring(mw.uri.fullUrl(mw.title.getCurrentTitle().prefixedText, "action=purge")))
				table.insert(ads, ' <font color="' .. (args.linkcolor or "#002bb8") .. '">show another</font>]')
			end
			table.insert(ads, " – #" .. (argad or cal) .. "</td></tr>")
		end
		table.insert(ads, "</table>")
	end
	return table.concat(errors) .. table.concat(ads)
end

return p