Module:Sensitive IP addresses/summary

< Module:Sensitive IP addresses
Revision as of 05:56, 16 July 2021 by Zoran (talk | contribs) (Pywikibot 6.4.0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:Used in system 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
165.119.0.0/16, 198.137.240.0/23, 204.68.207.0/242620:10F:B000::/40The Executive Office of the President of the United States
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/202600:400::/32The 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
192.0.2.0/24RFC 5737 reserved test range

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 Template:Tag element. (optional)
  • style - custom CSS styles for the main Template:Tag element. (optional)
  • cellstyle - custom CSS styles for every Template:Tag and Template:Tag element. (optional)



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

local p = {}

-- Strips a suffix from a CIDR string if the suffix is of a given bitLength.
-- bitLength must be either 32 or 128.
-- This is intended to allow CIDR strings to be represented as a single IP
-- address if this can be done unambiguously.
local function stripCIDRSuffix(cidr, bitLength)
	assert(bitLength == 32 or bitLength == 128, 'bitLength was not 32 or 128')
	local pattern = '/' .. bitLength .. '$'
	cidr = cidr:gsub(pattern, '')
	return cidr
end

-- Takes an array of CIDR ranges and returns a new array with ranges
-- appropriate for printing.
local function prettifyRanges(ranges, bitLength)
	local ret = {}
	for i, cidr in ipairs(ranges) do
		ret[i] = stripCIDRSuffix(cidr, bitLength)
	end
	return ret
end

-- Turns an array of CIDR ranges into its string representation.
local function stringifyRanges(ranges, bitLength, separator)
	if not ranges then
		return ''
	end
	ranges = prettifyRanges(ranges, bitLength)
	return table.concat(ranges, separator)
end

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

	-- Load dependencies
	local lang = mw.language.getContentLanguage()

	-- 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(stringifyRanges(
						entityData.ipv4Ranges,
						32,
						rangeSeparator
					))
					:done()
				:tag('td')
					:cssText(options.cellstyle)
					:wikitext(stringifyRanges(
						entityData.ipv6Ranges,
						128,
						rangeSeparator
					))
					:done()
				:tag('td')
					:cssText(options.cellstyle)
					:wikitext(lang:ucfirst(entityData.description))
			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