Jump to content

Module:Sensitive IP addresses/summary

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 03:05, 1 September 2016 (don't use yesno with the mainheader option). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local mSIPA_API = require('Module:Sensitive IP addresses/API')
local yesno = require('Module:Yesno')

local p = {}

function p._table(options)
	-- Return a wikitext table summarizing all the sensitive IP ranges
	-- and the entities they belong to.

	-- Set up options
	options = options or {}
	local separator = options.separator or ', '
	local showNotes = yesno(options.notes)
	local nColumns = showNotes and 3 or 4

	-- Get the entity data
	local data = mSIPA_API.query{entities={'all'}}
	if data['error'] then
		error(string.format('%s: %s', data['error'].code, data['error'].info))
	end

	-- Make the table root
	local root = mw.html.create('table')
	if options.class then
		root:addClass(options.class)
	end
	if options.style then
		root:cssText(options.style)
	end

	-- Add main header
	if options.mainheader then
		root:tag('tr'):tag('td')
			:attr('colspan', nColumns)
			:wikitext(options.mainheader)
	end

	-- Add column headers
	local headerRow = root:tag('tr')
	headerRow
		:tag('th')
			:wikitext('[[IPv4]]')
			:done()
		:tag('th')
			:wikitext('[[IPv6]]')
			:done()
		:tag('th')
			:wikitext('Description')
	if showNotes then
		headerRow:tag('th'):wikitext('Notes')
	end

	-- Add data cells
	for i, id in ipairs(data.sensitiveips['entity-ids']) do
		local entityData = data.sensitiveips.entities[id]
		if not options.reason or options.reason == entityData.reason then
			local dataRow = root:tag('tr')
			dataRow
				:tag('td')
					:wikitext(entityData.ipv4Ranges
						and table.concat(entityData.ipv4Ranges, separator)
						or nil
					)
					:done()
				:tag('td')
					:wikitext(entityData.ipv6Ranges
						and table.concat(entityData.ipv6Ranges, separator)
						or nil
					)
					:done()
				:tag('td')
					:wikitext(entityData.description or entityData.name)
			if showNotes then
				dataRow:tag('td'):wikitext(entityData.notes)
			end
		end
	end

	return tostring(root)
end

function p.table(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		frameOnly = true
	})
	return p._table(args)
end

return p