Jump to content

Module:Sensitive IP addresses/summary

From Simple English Wikipedia, the free encyclopedia
Revision as of 03:17, 1 September 2016 by Mr. Stradivarius (talk | changes) (add a cellstyle option)

This module generates a wikitable summarizing IP ranges that Wikipedia considers sensitive. It is used by Template:Sensitive IP addresses.

Sample output

IPv4IPv6Description
143.228.0.0/16, 143.231.0.0/16, 137.18.0.0/16, 12.185.56.0/29, 12.147.170.144/28, 74.119.128.0/222620:0:e20::/46the United States House of Representatives
156.33.0.0/162620:0:8a0::/48, 2600:803:618::/48the United States Senate
149.101.0.0/162607:f330::/32the United States Department of Justice
65.165.132.0/24, 204.248.24.0/24, 216.81.80.0/20the United States Department of Homeland Security
131.132.0.0/14, 131.136.0.0/14, 131.140.0.0/15the Canadian Department of National Defence
192.197.82.0/24the Canadian House of Commons
194.60.0.0/18the Parliament of the United Kingdom
138.162.0.0/16the United States Department of the Navy and the United States Marine Corps
91.198.174.0/24, 185.15.56.0/22, 198.35.26.0/23, 208.80.152.0/222620:0:860::/46, 2a02:ec80::/32the Wikimedia Foundation

Usage

{{#invoke:Sensitive IP addresses/summary|table
| reason          = 
| rangeseparator  = 
| notes           = yes/no
| mainheader      = 
| class           = 
| style           = 
| cellstyle       = 
}}

Parameters

  • reason - filter the entries by the reason they are sensitive. The available reasons are political and technical. (optional)
  • rangeseparator - a custom separator for IP ranges in entries with multiple IP ranges. The default is ", ".
  • notes - set to "yes" to show the "Notes" column. (optional)
  • mainheader - A custom header row at the top, spanning the whole width of the table. (optional)
  • class - custom HTML class for the main <table>...</table> element. (optional)
  • style - custom CSS styles for the main <table>...</table> element. (optional)
  • cellstyle - custom CSS styles for every <th>...</th> and <td>...</td> element. (optional)



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 rangeSeparator = options.rangeseparator 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)
			:cssText(options.cellstyle)
			:wikitext(options.mainheader)
	end

	-- Add column headers
	local headerRow = root:tag('tr')
	headerRow
		:tag('th')
			:cssText(options.cellstyle)
			:wikitext('[[IPv4]]')
			:done()
		:tag('th')
			:cssText(options.cellstyle)
			:wikitext('[[IPv6]]')
			:done()
		:tag('th')
			:cssText(options.cellstyle)
			:wikitext('Description')
	if showNotes then
		headerRow:tag('th')
			:cssText(options.cellstyle)
			: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')
					:cssText(options.cellstyle)
					:wikitext(entityData.ipv4Ranges
						and table.concat(entityData.ipv4Ranges, rangeSeparator)
						or nil
					)
					:done()
				:tag('td')
					:cssText(options.cellstyle)
					:wikitext(entityData.ipv6Ranges
						and table.concat(entityData.ipv6Ranges, rangeSeparator)
						or nil
					)
					:done()
				:tag('td')
					:cssText(options.cellstyle)
					:wikitext(entityData.description or entityData.name)
			if showNotes then
				dataRow:tag('td')
					:cssText(options.cellstyle)
					: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