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 09:34, 23 August 2016 (split the API and the summary table into separate modules). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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 yesno(options.mainheader) then
		local mainHeader = root:tag('tr'):tag('td')
		mainHeader:attr('colspan', nColumns)
		local mainHeaderText = '[[Wikipedia:Blocking IP addresses#Sensitive IP addresses|Sensitive IP addresses]]'
		if yesno(options.rangecalculator) then
			mainHeaderText = mainHeaderText .. '  ([http://www.subnet-calculator.com/cidr.php IPv4 range calculator] - [http://www.gestioip.net/cgi-bin/subnet_calculator.cgi IPv6 range calculator])'
		end
		if options.mainheaderrule then
			mainHeaderText = mainHeaderText .. '\n<hr style="margin: 4px 0;" />'
		end
		mainHeader:wikitext(mainHeaderText)
	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, {
		wrappers = 'Template:Sensitive IP addresses'
	})
	return p._table(args)
end

return p