Module:Sensitive IP addresses/API: Difference between revisions

Content deleted Content added
m Mr. Stradivarius moved page Module:Sensitive IP addresses to Module:Sensitive IP addresses/API without leaving a redirect: split the API and the summary table into separate modules
remove the summary table code, and don't use libraryUtil as everything using it was either internal or reserved
Line 7:
local IPv4Collection = mIP.IPv4Collection
local IPv6Collection = mIP.IPv6Collection
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
 
-------------------------------------------------------------------------------
Line 87 ⟶ 85:
-- otherwise. matchObj is the Subnet object that was matched, and queryObj
-- is the IPAddress or Subnet object corresponding to the input string.
checkType('matchesIPOrRange', 1, str, 'string')
 
-- Get the IPAddress or Subnet object for str
Line 426 ⟶ 423:
return result
end
end
 
-------------------------------------------------------------------------------
-- Summary table
-- A table of sensitive IP data to be used in
-- [[Template:Sensitive IP addresses]].
-------------------------------------------------------------------------------
 
local function makeSummaryTable(options)
-- Return a wikitext table summarizing all the sensitive IP ranges
-- and the entities they belong to.
 
-- Load necessary modules
local yesno = require('Module:Yesno')
 
-- 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 = 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
 
Line 526 ⟶ 431:
local p = {}
 
function p.isValidSensitivityReason_isValidSensitivityReason(s)
-- Return true if s is a valid sensitivity reason; otherwise return false.
return s ~= nil and SensitiveEntity.reasons[s] ~= nil
checkType('isValidSensitivityReason', 1, s, 'string')
return SensitiveEntity.reasons[s] ~= nil
end
 
function p.getSensitivityReasons_getSensitivityReasons()
-- Return an array of valid sensitivity reasons, ordered alphabetically.
local ret = {}
Line 544 ⟶ 448:
-- Export the API query function
p.query = query
 
-- Export the summary table function
function p.summary(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Sensitive IP addresses'
})
return makeSummaryTable(args)
end
 
return p