Module:Sensitive IP addresses/API: Difference between revisions

Content deleted Content added
add summary table function
only include query result subtables when we have data, allow returning queries as JSON, and allow checking for the reason when making the summary table
Line 273:
)
end
 
-- Insert the result subtables. We do this now rather than when we
-- create the results table, as there shouldn't be a matches subtable
-- if the user didn't specify any strings to match.
result.matches = {}
result['matched-ranges'] = {}
result.entities = {}
result['entity-ids'] = {}
 
for i, testString in ipairs(options.test) do
Line 314 ⟶ 306:
if isMatch then
-- The string was a sensitive IP address or subnet.
 
-- Set up result subtables
result.matches = result.matches or {}
result['matched-ranges'] = result['matched-ranges'] or {}
result.entities = result.entities or {}
result['entity-ids'] = result['entity-ids'] or {}
 
-- Add the match to the match subtable.data
local match = {}
-- Quick and dirty hack to find if queryObj is an IPAddress object.
local isIP = queryObj.getNextIP ~= nil and queryObj.isInSubnet ~= nil
 
-- Add the match to the match subtable.
if isIP then
match.type = 'ip'
Line 413 ⟶ 411:
end
 
-- Add any missing reason fields from entities.
return {sensitiveips = result}
if result.entities = {}then
for id, entityData in pairs(result.entities) do
entityData.reason = entityData.reason or 'political'
end
end
 
-- Wrap the result in an outer layer like the MediaWiki Action API does.
returnresult = {sensitiveips = result}
 
if options.format == 'json' then
return mw.text.jsonEncode(result)
else
return result
end
end
 
Line 422 ⟶ 434:
-------------------------------------------------------------------------------
 
local function makeSummaryTable(options)
options = options or {}
local data = query{entities={'all'}}
if data['error'] then
Line 445 ⟶ 458:
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
root:tag('tr')
root:tag('tdtr')
:tag('td')
:wikitext(entityData.ipv4Ranges
and table.concat(entityData.ipv4Ranges, ', ')
or nil
)
:done()
:tag('td')
:wikitext(entityData.ipv6Ranges
and table.concat(entityData.ipv6Ranges, ', ')
or nil
)
:done()
:tag('td')
:wikitext(entityData.description or entityData.name)
end
end