Module:Sensitive IP addresses/summary: Difference between revisions

Content deleted Content added
split the API and the summary table into separate modules
 
more accessible and simple markup
 
(14 intermediate revisions by 3 users not shown)
Line 3:
 
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)
and return table.concat(entityData.ipv4Rangesranges, 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 separatorrangeSeparator = options.separatorrangeseparator or ', '
local showNotes = yesno(options.notes)
local nColumns = showNotes and 3 or 4
 
-- Get the entity data
Line 22 ⟶ 54:
-- Make the table root
local root = mw.html.create('table')
root:addClass('sensitive-ip-addresses')
if options.class then
root:addClass(options.class)
Line 30 ⟶ 63:
 
-- Add main header
if yesno(options.mainheader) then
local mainHeader = root:tag('trcaption'):tagwikitext('td'options.mainheader)
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
 
Line 47 ⟶ 71:
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'):wikitext('Notes')
:cssText(options.cellstyle)
:wikitext('Notes')
end
 
Line 65 ⟶ 94:
dataRow
:tag('td')
:wikitextcssText(entityDataoptions.ipv4Rangescellstyle)
:wikitext(stringifyRanges(
and table.concat(entityData.ipv4Ranges, separator)
or nilentityData.ipv4Ranges,
) 32,
rangeSeparator
))
:done()
:tag('td')
:wikitextcssText(entityDataoptions.ipv6Rangescellstyle)
:wikitext(stringifyRanges(
and table.concat(entityData.ipv6Ranges, separator)
or nilentityData.ipv6Ranges,
) 128,
rangeSeparator
))
:done()
:tag('td')
:cssText(options.cellstyle)
:wikitext(lang:ucfirst(entityData.description or entityData.name))
if showNotes then
dataRow:tag('td'):wikitext(entityData.notes)
:cssText(options.cellstyle)
:wikitext(entityData.notes)
end
end
Line 89 ⟶ 125:
function p.table(frame)
local args = require('Module:Arguments').getArgs(frame, {
frameOnly = true
wrappers = 'Template:Sensitive IP addresses'
})
return p._table(args)